I want to count number of Objects in an Image identify by each color.
Example: I want to count yellow rices or green rices in the image.
Would you please give me some tip to do it?
I want to count number of Objects in an Image identify by each color.
Example: I want to count yellow rices or green rices in the image.
Would you please give me some tip to do it?
You've taken this image from the rice demonstration that ships with MATLAB's Image Processing Toolbox. If you read through the demo, you already have some code that will isolate each rice grain individually - that's how the image was created.
As for counting the number of grains that have a particular color such as yellow or green, again read through the demo: it's clear that the rice grains are not colored with a small set of discrete colors such as yellow or green (or orange or pink). Rather, the rice grain colors have been specifically created so that they are equally spaced throughout the colormap spring
.
So to proceed with your task you're going to have to provide some definition of 'yellow' or 'green', perhaps in terms of being within a particular range of RGB values.
Having done this, you can then use the variable labeled
(that is constructed for you in the demo) together with the regionprops
command, to give you a list of the pixels that are within each rice grain boundary. Simply compare those pixels to your definitions of yellow or green, and you're there.
assuming you have an image in matrix a
(sized m*n*3
), and you want to find the number of objects with color [r,g,b]
.
first, select only pixels with the correct color:
bb = (a(:,:,1) == r & a(:,:,2) == g &a(:,:,3) == b);
Than:
[~,num] = bwlabel(bb,8)
num
is the number of objects.
This is best done by working in a different colorspace than the RGB one (think HSV, Lab*, ..).
Steve Eddins posted a series of articles showing an example how to segment objects of certain color from an image (green M&Ms in his case):