1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| bw=imread ('C:/Program Files/MATLAB/R2007b/te.png')
%bw = ~bw(1107:1194, 17:135);
figure, imshow(bw, 'InitialMagnification', 'fit'),figure
L = bwlabel(bw);
rgb = label2rgb(L, 'jet', [.95 .95 .95], 'shuffle');
imshow(rgb, 'InitialMagnification', 'fit'), figure
s = regionprops(L, {'Centroid', 'PixelIdxList'});
centroids = cat(1, s.Centroid);
[sorted_centroids, sort_order] = sortrows(fliplr(centroids));
s2 = s(sort_order);
% First, make an image with a light gray background instead
% of a black background, so that the numbers will be visible
% on top of it.
I = im2uint8(bw);
I(~bw) = 200;
I(bw) = 240;
imshow(I, 'InitialMagnification', 'fit')
% Now plot the number of each sorted object at the corresponding
% centroid:
hold on
for k = 1:numel(s2)
centroid = s2(k).Centroid;
text(centroid(1), centroid(2), sprintf('%d', k));
end
hold off |