clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
hasLicenseForToolbox = license('test', 'image_toolbox');
if ~hasLicenseForToolbox
ver
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
return;
end
end
folder = pwd;
baseFileName = 'image.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
rgbImage = imread(fullFileName);
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage > 10;
binaryImage = imclearborder(binaryImage);
subplot(2, 2, 1);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
binaryImage = imfill(binaryImage, 'holes');
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
props = regionprops(binaryImage, 'Solidity', 'Area', 'perimeter');
allAreas = [props.Area]
allSolidities = [props.Solidity]
allCircularities = 4 * pi * allAreas ./ [props.Perimeter] .^ 2
subplot(2, 2, 3);
histogram(allSolidities);
grid on;
title('Histogram of Solidities', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2, 2, 4);
histogram(allCircularities);
grid on;
title('Histogram of circularities', 'FontSize', fontSize, 'Interpreter', 'None');
binaryImage2 = bwpropfilt(binaryImage, 'Solidity', [.85, inf]);
figure;
subplot(2, 1, 1);
imshow(binaryImage2, []);
title('Binary Image2 - high solidity', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
highCircularityIndexes = find(allCircularities > 0.6);
labeledImage = bwlabel(binaryImage);
binaryImage3 = ismember(labeledImage, highCircularityIndexes);
subplot(2, 1, 2);
imshow(binaryImage3, []);
title('Binary Image3 - high circularity', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
Best Answer