clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 22;
folder = pwd;
baseFileName = 'Q4_4.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 = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 3, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized';
drawnow;
binaryImage = ~imbinarize(grayImage);
binaryImage = bwareaopen(binaryImage, 200);
binaryImage = imclearborder(binaryImage);
subplot(2, 3, 2);
imshow(binaryImage, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
drawnow;
props = regionprops(binaryImage, 'Centroid', 'Area');
xy = vertcat(props.Centroid)
allAreas = [props.Area];
subplot(2, 3, 3);
histogram(allAreas, 10);
grid on;
title('Histogram of Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Area', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Count', 'FontSize', fontSize, 'Interpreter', 'None');
[labeledImage, numBlobs] = bwlabel(binaryImage);
smallIndexes = find(allAreas < 2500);
largeIndexes = find(allAreas >= 2500);
smallBlobs = ismember(labeledImage, smallIndexes);
subplot(2, 3, 4);
imshow(smallBlobs);
title('Small Blobs', 'FontSize', fontSize, 'Interpreter', 'None');
largeBlobs = ismember(labeledImage, largeIndexes);
subplot(2, 3, 5);
imshow(largeBlobs);
title('Large Blobs', 'FontSize', fontSize, 'Interpreter', 'None');
Best Answer