Number of Objects in image
2 views (last 30 days)
Show older comments
So I have this code that determines the number of pollen grains in the image. Is it possible to be able to separate the types of grains and obtain the total number for each type?
clear all;
close all;
clc;
I1 = imread ('Pollen1.tif');
ix = 3; iy = 800 * 3 / 1000;
I2 = rgb2gray(I1);
Ibw = im2bw(I2, graythresh(I2));
Ibw = ~Ibw;
I3 = bwareaopen(Ibw,10);
I4 = im2bw(I3,0.2);
I5 = imclearborder(I4);
himage1 = imshow(I4,'XData',[0 ix],'YData',[0 iy]); hold on
set(himage1,'AlphaData',0.4);
himage2 = imshow (imsubtract(I4,I5),'XData',[0 ix],'YData',[0 iy]);
set(himage2,'AlphaData',0.7);
title ('Image Border'), hold off
figure,
[B,L] = bwboundaries(I4,'noholes');
[labeled,numObjects]=bwlabeln(I5,4);
numObjects
graindata=regionprops(labeled,'basic');
grainareas=[graindata(:).Area];
objectareas=3^2*grainareas * 1000^(-2);
max_area = max (objectareas)
min_area = min (objectareas)
mean_area = mean (objectareas)
clf
e =0:0.0005:0.15;
histogram(objectareas,e)
xlabel('Grain Size in Millimeters^2')
ylabel('Number of Grains')
axis([0 0.1 0 30])
D = bwdist(~I5,'cityblock');
D=-D;
D(~I5)=-Inf;
L2 = watershed(D);
figure
subplot (3,2,1);
imshow(I1,'XData',[0 ix],'YData',[0 iy]), title ('Original Image')
subplot (3,2,2);
imshow(I2,'XData',[0 ix],'YData',[0 iy]), title ('Grayscale Image')
subplot (3,2,3);
imshow(I3), title ('Black Background')
subplot (3,2,4);
imshow(I4,'XData',[0 ix],'YData',[0 iy]), title ('Binarize Image')
subplot (3,2,5);
imshow(label2rgb(L,@jet,'w','shuffle'),'XData',[0 ix],'YData',[0 iy]), title ('Define Objects')
subplot (3,2,6);
imshow(label2rgb(L2,@jet,'w','shuffle'),'XData',[0 ix],'YData',[0 iy]), title('Watershed Segmentation')
3 Comments
Adam Danz
on 10 Jun 2021
I saved the PDF as a TIF file which is lower resolution than your image but is still large enough to get very similar results. The steps of downloading the file and converting it is often enough hastle for contributers to select a different question to answer. Just a tip to maximize your chances of getting help: make it very easy for people to run the code. And thanks for providing the code!
I played around with this for a bit but ran out of time and couldn't separate some of the clusters of pollen.
Answers (1)
Image Analyst
on 10 Jun 2021
The watershed returns a labeled image so you should just be able to get the max value of it to count the regions:
numberOfRegions = max(L2(:))
I'm not sure what you mean by "each type". If each region has a different looking object in it then you're going to have to classify them somehow, like by their color, size, shape, or whatever.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!