clc;
close all;
clear;
workspace;
format short g;
format compact;
fontSize = 25;
baseFileName = 'image1.png';
folder = [];
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0, 1, 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
binaryImage = grayImage == 0;
binaryImage = imclearborder(binaryImage);
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
axis image;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
[labeledImage, numBlobs] = bwlabel(binaryImage);
subplot(2, 2, 2);
imshow(binaryImage, []);
axis on;
axis image;
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 2, 3);
imshow(coloredLabels);
axis on;
axis image;
caption = sprintf('Pseudo colored labels, from label2rgb().');
title(caption, 'FontSize', fontSize);
drawnow;
props = regionprops(labeledImage, 'BoundingBox');
subplot(2, 2, 4);
imshow(binaryImage, []);
axis on;
axis image;
title('Binary Image with Boxes', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
for k = 1 : numBlobs
thisBoundingBox = props(k).BoundingBox
rectangle('Position', thisBoundingBox, ...
'EdgeColor', 'c', 'LineWidth', 2);
end
Best Answer