clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
folder = pwd;
baseFileName = 'worm.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
[grayImage, storedColorMap] = imread(fullFileName);
if ~isempty(storedColorMap)
grayImage = ind2rgb(grayImage, storedColorMap);
end
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
hFig = figure;
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig.WindowState = 'maximized';
drawnow;
binaryImage = imbinarize(grayImage);
binaryImage = bwareafilt(binaryImage, 1);
binaryImage = imfill(binaryImage, 'holes');
subplot(1, 2, 1);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
skelImage = bwskel(binaryImage);
MinBranchLength = round(sum(skelImage(:))/2)
skelImage = bwskel(binaryImage, 'MinBranchLength', MinBranchLength);
subplot(1, 2, 2);
imshow(skelImage, []);
title('Skeleton Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
endpointImage = bwmorph(skelImage, 'endpoints');
[rows, columns] = find(endpointImage)
hold on;
plot(columns, rows, 'r-', 'LineWidth', 2);
spineLength = sum(skelImage(:))
straightLineDistance = sqrt((columns(2) - columns(1))^2 + (rows(2) - rows(1))^2)
tortuosity = spineLength / straightLineDistance
caption = sprintf('Skeleton Image. Tortuosity = %f', tortuosity);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer