Try this well commented demo. It finds all the blobs, then gets their area, centroid, and list of all the pixels in each blob. It also colorizes each discrete blob so you know what blob is distinct.
clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 16;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
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.jpeg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage);
axis on;
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize);
mask = imbinarize(grayImage);
mask = imclearborder(mask);
subplot(2, 2, 3);
imshow(mask);
axis on;
title('Mask Image', 'FontSize', fontSize);
[labeledImage, numberOfBlobs] = bwlabel(mask);
props = regionprops(mask, 'Area', 'Centroid', 'PixelList');
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 2, 4);
imshow(coloredLabels);
axis('on', 'image');
caption = sprintf('Pseudo colored labels, from label2rgb().\nBlobs are numbered from top to bottom, then from left to right.');
title(caption, 'FontSize', fontSize);
for k = 1 : numberOfBlobs
xy = props(k).PixelList;
fprintf('Blob #%d: Area = %d. Centroid (x, y) = (%.4f, %.4f)\nPixels in blob #%d:\n ', ...
k, props(k).Area, props(k).Centroid(1), props(k).Centroid(2), k);
for k2 = 1 : size(xy, 1)
fprintf('(%d, %d), ', xy(k2, 1), xy(k2, 2));
end
fprintf('\n');
end
In the command window you will see a list of all the measurements:
Blob #1: Area = 6. Centroid (x, y) = (151.1667, 83.1667)
Pixels in blob #1:
(150, 83), (151, 82), (151, 83), (151, 84), (152, 83), (152, 84),
Blob #2: Area = 14. Centroid (x, y) = (163.7143, 83.5000)
Pixels in blob #2:
(162, 83), (162, 84), (163, 82), (163, 83), (163, 84), (163, 85), (164, 82), (164, 83), (164, 84), (164, 85), (165, 82), (165, 83), (165, 84), (165, 85),
Blob #3: Area = 28. Centroid (x, y) = (169.7857, 94.5000)
Pixels in blob #3:
(167, 94), (167, 95), (168, 93), (168, 94), (168, 95), (168, 96), (169, 92), (169, 93), (169, 94), (169, 95), (169, 96), (169, 97), (170, 92), (170, 93), (170, 94), (170, 95), (170, 96), (170, 97), (171, 92), (171, 93), (171, 94), (171, 95), (171, 96), (171, 97), (172, 93), (172, 94), (172, 95), (172, 96),
Blob #4: Area = 6. Centroid (x, y) = (274.8333, 93.8333)
Pixels in blob #4:
(274, 93), (274, 94), (275, 93), (275, 94), (275, 95), (276, 94),
Blob #5: Area = 22. Centroid (x, y) = (281.0909, 83.0909)
Pixels in blob #5:
(279, 82), (279, 83), (279, 84), (280, 81), (280, 82), (280, 83), (280, 84), (280, 85), (281, 81), (281, 82), (281, 83), (281, 84), (281, 85), (282, 81), (282, 82), (282, 83), (282, 84), (282, 85), (283, 82), (283, 83), (283, 84), (283, 85),
Blob #6: Area = 35. Centroid (x, y) = (293.5714, 83.6286)
Pixels in blob #6:
(291, 82), (291, 83), (291, 84), (291, 85), (292, 81), (292, 82), (292, 83), (292, 84), (292, 85), (292, 86), (293, 81), (293, 82), (293, 83), (293, 84), (293, 85), (293, 86), (293, 87), (294, 81), (294, 82), (294, 83), (294, 84), (294, 85), (294, 86), (294, 87), (295, 81), (295, 82), (295, 83), (295, 84), (295, 85), (295, 86), (296, 81), (296, 82), (296, 83), (296, 84), (296, 85),
Blob #7: Area = 59. Centroid (x, y) = (299.6780, 94.6102)
Pixels in blob #7:
(296, 93), (296, 94), (296, 95), (296, 96), (296, 97), (297, 92), (297, 93), (297, 94), (297, 95), (297, 96), (297, 97), (297, 98), (298, 91), (298, 92), (298, 93), (298, 94), (298, 95), (298, 96), (298, 97), (298, 98), (299, 91), (299, 92), (299, 93), (299, 94), (299, 95), (299, 96), (299, 97), (299, 98), (300, 91), (300, 92), (300, 93), (300, 94), (300, 95), (300, 96), (300, 97), (300, 98), (301, 91), (301, 92), (301, 93), (301, 94), (301, 95), (301, 96), (301, 97), (301, 98), (302, 91), (302, 92), (302, 93), (302, 94), (302, 95), (302, 96), (302, 97), (302, 98), (303, 92), (303, 93), (303, 94), (303, 95), (303, 96), (303, 97), (304, 95),
Best Answer