clc;
close all;
workspace;
format long g;
format compact;
fontSize = 18;
folder = pwd;
baseFileName = 'gear.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
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
if numberOfColorChannels > 1
grayImage = rgbImage(:, :, 2);
else
grayImage = rgbImage;
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
subplot(2, 2, 2);
histogram(grayImage, 256);
title('Histogram of image', 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
drawnow;
mask = grayImage > 45;
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
subplot(2, 2, 3);
imshow(mask, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
msgbox('Done!');
Best Answer