Counting the individual objects in binary image without built-in functions
3 views (last 30 days)
Show older comments
Eren Kivanc
on 27 Jun 2022
Commented: Dyuman Joshi
on 22 Sep 2023
I have preprocessed an image to obtain following binary image of overlapping stem cells. How do I count the individual cells on the image WITHOUT using built-in functions but only morphological algorithms?
I tried watershed transform but got weird outputs and still can't figure out the "counting the cells" part.
Here is what I tried:
binary = imread("1A_Binary_Image.png")
% distance transform
D = bwdist(~binary);
imshow(D,[])
title('distance transform')
% complement of distance transform
D = -D;
% watershed transform
L = watershed(D);
L(~binary) = 0;
rgb = label2rgb(L,'jet',[.5 .5 .5]);
imshow(rgb)
title('Watershed Transform')
1 Comment
Dyuman Joshi
on 22 Sep 2023
You don't want to use builtin functions but you have used toolbox functions?
Accepted Answer
VINAYAK LUHA
on 22 Sep 2023
Hi Eren,
It is my understanding that you have a binary image containing several overlapping cells and wish to segregate overlapping cells into individual cells and count their numbers using only Morphological Transformations.
To prevent the over segmentation you have been facing, remove the shallow minima from the image by using the “imhmin” function before calling the watershed function.
As for the counting part, you can use Depth first search(DFS) or Breadth First Search(BFS) algorithm to count the number of connected components i.e. group of ones in the image. Refer to the solution tab on the following website for more information https://leetcode.com/problems/number-of-islands/solutions/ .
In case you wish to settle with builtin functions here’s the improved code for your reference based on the suggestions –
img = imread("Image Path");
D = bwdist(~img);
D = -D;
D = imhmin(D, 2);
L = watershed(D);
L(~img) = 0;
L = imbinarize(L);
L = bwareaopen(L, 500);
RGB = label2rgb(L, 'jet', 'k', 'shuffle');
imshow(RGB);
CC = bwconncomp(L);
S = regionprops(CC, 'Centroid');
title("Number of Cells: "+ num2str(numel(S)));
hold on;
for k = 1:numel(S)
centroid = S(k).Centroid;
plot(centroid(1), centroid(2), 'r+', 'MarkerSize', 10, 'LineWidth', 2);
end
hold off;
Result
Regards
Vinayak Luha
0 Comments
More Answers (0)
See Also
Categories
Find more on Feature Detection and Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!