How can I tell how many different colors are in an image and differentiate some shapes that overlaping.
Show older comments

I have an image of m&ms I already converted into binary so I can count how many they are. I'm suppose to program it so whenever I analyze a picture it tells me how many m&ms are in total, some of them have overlapping chocolates. I was going to try with imfindcircles but telling the radius range wont work if every picture is taken from different distances. Is there any other way I can solve this? Also, I need to tell how many m&ms of each color are and how many different colors. How can I do this.
Answers (1)
Image Analyst
on 24 Aug 2015
To count, simply call bwlabel:
[labeledImage, numMandMs] = bwlabel(binaryImage);
To find the number of different colors, it would help if you could have a set of known colors. Are you able to take a photo of all the different colors in one photo? Like one red, one green, one blue, etc.? If so, you can simply convert the images to LAB color space, get the mean LAB for each region (candy piece), then calculate the delta E color difference between each candy and all the other reference colors. If the delta E is less than some number, it's a match for that color. Post your image if you want more help. Also look into the function rgb2lab(). Delta E is the distance between the two colors in LAB color space (see my avatar to the left for a representation of LAB color space).
2 Comments
Sara Alvarez
on 24 Aug 2015
Edited: Sara Alvarez
on 24 Aug 2015
Image Analyst
on 24 Aug 2015
Yes. So give it a try. Try to do the steps I listed. Basically they're:
- read in reference image
- convert into LAB color space
- separate into individual channel: L, A, and B
- threshold
- clean up - fill holes and remove noise
- Measure mean L, mean A, and mean B of the blobs
Then do the same for the "test" image.
Do a loop over each blob computing deltaE to each of the ref colors. See my File Exchange demo on that: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
for k1 = 1 : numTestBlobs
minDeltaE = inf;
for k2 = 1 : numRefColors
deltaE = sqrt(.....
if deltaE < minDeltaE
% This is the closest color so far
closestColor(k1) = k2;
minDeltaE = deltaE;
end
end
end
And so on. I'm sure you can fill in the missing parts.
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!