detect the major color in an image- red or blue ?

I am doing a project to detect mangoes from a video. Also, I have to differentiate between ripe and unripe mangoes. After detecting mangoes, I'm cropping them. So basically, in the cropped image I have to detect whether the major colour is green or red.
Can anyone help me?
I have tried colour quantisation but it has a high failure rate. Remember I do not have to detect the major colour, just whether it is red or green.

 Accepted Answer

A very trivial approach is calculating the average color and compare the red and green channel:
image = rand(100, 100, 3); % Test data
meanRGB = mean(reshape(image, [], 3), 1);
isRed = meanRGB(1) > meanRGB(2);
I'd prefer calculating the distnace to red and green in the HSV color space:
meanHSV = rgb2hsv(meanRGB);
redHSV = rgb2hsv([1, 0, 0]);
greenHSV = rgb2hsv([0, 1, 0]);
isRed = abs(meanHSV(1)-redHSV(1)) < ...
abs(meanHSV(1)-greenHSV(1));
This is less susceptible for the saturation. There are more advanced methods also, but the failure rate of this simmple approach could be satisfying already.

1 Comment

thanks a lot. Actually I had done the mean thing myself, then saw the same thing here.
About the distance, I was thinking of the euclidean distance in the rgb color cube. Anyway, the mean thing is accurate enough. :)

Sign in to comment.

More Answers (1)

The question here is surely how to define what you mean by "the major colour is green or red". If you can explain what that means, the solution in Matlab is probably straightforward.
Do you have examples of ripe and unripe mango images? If you do, you could use them to help you find a useful definition, and perhaps to learn the values of any parameters.
Here are a few initial thoughts. Assuming your image is RGB (if not, you can convert it using e.g. ind2rgb), you can pick out the red and green components like this:
rplane = im(:, :, 1);
gplane = im(:, :, 2);
Actually, this is a little naive, and you may do better with more general formula, of the form:
rplane = r1 * im(:,:,1) + r2 * im(:,:,2) + r3 * im(:,:,3);
gplane = g1 * im(:,:,1) + g2 * im(:,:,2) + g3 * im(:,:,3);
where the coefficients r1 etc. define a colour space transformation - look this up for more information about good values for fruit ripeness discrimination.
With a red plane and a green plane, you could then very simply test which has more total intensity:
redder = sum(rplane(:)) > sum(gplane(:));
but this is almost certainly too simple. The next step might be to use a threshold:
redder = (sum(rplane(:)) - sum(gplane(:))) > threshold1;
where threshold1's value should be learnt from examples.
Alternatively, you could count how many pixels have more redness than greenness:
redder = sum(sum(rplane > gplane)) > threshold2
where again threshold2 should learnt.
These are only simple starting points. To go further I would (a) look at the literature and (b) experiment with the images.

Categories

Asked:

on 15 Feb 2011

Community Treasure Hunt

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

Start Hunting!