How to count the number of colored objects in a picture?
6 views (last 30 days)
Show older comments
Hi Everyone I am just starting image processing with matlab, I need to find how many colored pins in the picture attached and how many of each color and then find and count individual colors, then detect the white and transparent pins can someone help please and keep in mind I never did any image processing before what so ever.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160383/image.jpeg)
2 Comments
dusi jayasree
on 28 Aug 2017
i m a newbie..and wanted to know if we can count the colors in as image directly by any process or count the colors present in image by segmentation....is it possible to count colors after an image segmentation..???plz help me wit th is question
Image Analyst
on 28 Aug 2017
Edited: Image Analyst
on 28 Aug 2017
I'd probably convert to HSV color space and threshold on saturation. Then I'd convert to lab colorspace with rgb2lab() and get the lab mean for each blob (pushpin). Any blobs that are within a certain distance (Delta E color difference) of each other, I'd consider them to be the same color pushpin. Then count the number of clusters you have after this merging operation. This should work for most colored pushpins, though the white and clear ones will be problematic and might have to be detected by a different algorithm.
Accepted Answer
Carl
on 6 Feb 2017
Edited: Carl
on 6 Feb 2017
It looks like you'd like to perform some standard image segmentation. See the following page for an example on doing Color-based Segmentation:
https://www.mathworks.com/help/images/examples/color-based-segmentation-using-k-means-clustering.html?prodcode=IP&language=en
You should also take a look at the following tutorial on image segmentation:
In general, you could convert your image to a L*a*b* colorspace, classify each color with clustering, label your original image, and then count each component or manipulate your image as necessary.
0 Comments
More Answers (1)
Image Analyst
on 6 Feb 2017
See my Color segmentation demos in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
As far as determining how many colors there are you'll have to use some unsupervised clustering methods in the Statistics and Machine Learning Toolbox. Since you don't know how many colors in advance, you can't use easy methods like kmeans or k-nearest neighbor. See http://www.mathworks.com/help/stats/cluster-analysis.html At one extreme, you could consider that every pixel in the image is a different color, so you probably have literally thousands of unique colors, but you don't want that so you have to do cluster analysis.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!