clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
folder = [];
baseFileName = 'original image.JPG';
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 = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
hFig1.Name = 'Demo by Image Analyst';
subplot(2, 3, 2);
imhist(grayImage);
grid on;
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
mask = grayImage < 75;
mask(:, 1 : 762) = false;
mask(:, 2615 : end) = false;
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'BoundingBox');
allBB = vertcat(props.BoundingBox);
widths = allBB(:, 3);
wideBlobs = find(widths > 1500);
mask = ismember(labeledImage, wideBlobs);
subplot(2, 3, 3);
imshow(mask, []);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo();
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 3, 4);
imshow(coloredLabelsImage);
title('Labeled Blobs', 'FontSize', fontSize, 'Interpreter', 'None');
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
leftEdges = zeros(1, columns);
rightEdges = zeros(1, columns);
widths = zeros(columns, numberOfBlobs);
for blob = 1 : numberOfBlobs
thisBlob = ismember(labeledImage, blob);
subplot(2, 3, 5);
imshow(thisBlob, []);
axis('on', 'image');
caption = sprintf('Mask of Blob #%d of %d', blob, numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
for col = 1 : columns
thisCol = thisBlob(:, col);
t = find(thisCol, 1, 'first');
if ~isempty(t)
leftEdges(col) = t;
rightEdges(col) = find(thisCol, 1, 'last');
end
end
widths(:, blob) = (rightEdges - leftEdges)';
subplot(2, 3, 6);
plot(widths, '-', 'LineWidth', 1);
hold on;
grid on;
drawnow;
legendStrings{blob} = sprintf('Blob #%d', blob);
end
grid on;
title('Widths', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Row', 'FontSize', fontSize);
ylabel('Width in Pixels', 'FontSize', fontSize);
legend(legendStrings);
subplot(2, 3, 5);
imshow(mask, []);
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;
hp = impixelinfo();
fprintf('Done running %s.m\n', mfilename);
Best Answer