Separating overlapping edges in an image to get an accurate count

24 views (last 30 days)
Hi,
I am trying to count the big cells in the sample image below.
I was able to reach a point where I was able to delete most of the smaller cells and highlight the big cells as seen below.
However, when I count the objects (using bwlabel and bweuler) it's giving me 6. It is probably because of the overlapping edges of the objects on the leftmost side?
How can I separate these overlaps so I can get an accurate count of 7?
Thank you!

Accepted Answer

Arturo Jr. Ongkeko
Arturo Jr. Ongkeko on 4 May 2021
I was able to figure it out after some time. Here's the code.
%% COUNT BIG CELLS
%convert image to black and white
img_bw=im2bw(img);
figure,imshow(img_bw)
%background will be black and object will be white
inv_img=imcomplement(img_bw);
figure,imshow(inv_img)
%use imfill function to fill the holes in the object
img2=imfill(inv_img,'holes');
figure,imshow(img2)
%remove small objects containing fewer than 1000 pixels in the binary image
%using bwareaopen
img3=bwareaopen(img2,1000);
figure,imshow(img3)
%use bwmorph erode function to further enhance the objects
img4=bwmorph(img3,'erode');
figure,imshow(img4)
%conduct another removal of small objects containing fewer than 500 pixels
%in a binary image
img5=bwareaopen(img4,500);
figure,imshow(img5)
%use the bwlabel function to count the objects
[labeledImage,numberofObject]=bwlabel(img5); %count: 7

More Answers (1)

DGM
DGM on 3 May 2021
This works, but I had trouble trying to get watershed() to work without a little tweaking.
inpict = im2double(imread('cells.jpg'));
% try to extract the more bluish purple part
tol = [0.15 0.2]; % box width
clim = 0.615+[-1 1]*tol(1); % selected color point in C,H
hlim = 0.863+[-1 1]*tol(2);
cmap = mono(inpict,'clch');
hmap = mono(inpict,'hlch');
interior = cmap>clim(1) & cmap<clim(2) & hmap>hlim(1) & hmap<hlim(2);
% this is similar to bwareaopen(), but it does opening and/or closing
interior = despeckle(interior,20);
d = bwdist(~interior);
d(d<(max(d(:))/2)) = 0; % had to give watershed a little help
L = watershed(~d);
L(~interior) = 0; % this is the label array
max(L) % this is the number of labeled objects
imshow2(simnorm(L))
This uses MIMT tools just because I'm lazy and can't be bothered to do it all in base/IPT tools
  2 Comments
Arturo Jr. Ongkeko
Arturo Jr. Ongkeko on 4 May 2021
Thanks, DGM! Appreciate this! I am graduate student though and we are expected to use the IPT. Also we were not taught how to use image manipulation toolbox yet. I will defintely try your appoach for sure in the future. Thanks again very much!
DGM
DGM on 4 May 2021
Well it's not like MIMT is a thing anyone should be taught. It's good you solved it though.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!