detect the major color in an image- red or blue ?
Show older comments
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
More Answers (1)
David Young
on 15 Feb 2011
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
Find more on Image Processing Toolbox 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!