Need some help regarding color detection and it's position

I want to detect the color and it's position in an image. I don't get the output. I would be really happy if you could guide me.
Example: Take a red box in a white background at some position. I want to detect the red color and it's position (i.e. co-ordinates) in the image.
Thanks in advance.

 Accepted Answer

Hi, i Venky
from my previous post, I've improved the code.
This picture below is test image :
Here the code :
Irgb = imread('multiplecolors.jpg');
Ihsv = rgb2hsv(Irgb);
low = 0;
high = 0.04;
h = (Ihsv(:,:,1) >= low) & (Ihsv(:,:,1) <= high);
s = (Ihsv(:,:,2) >= 0.9) & (Ihsv(:,:,2) <= 1);
v = (Ihsv(:,:,3) >= 0) & (Ihsv(:,:,3) <= 1);
mask = uint8(h & s & v);
mask = imclose(mask, strel('disk', 5));
mR = mask .* Irgb(:,:,1);
mG = mask .* Irgb(:,:,2);
mB = mask .* Irgb(:,:,3);
Icolor = cat(3, mR, mG, mB);
Igray = rgb2gray(Icolor);
Ibw = im2bw(Igray,graythresh(Igray));
Ilabel = bwlabel(Ibw);
stat = regionprops(Ilabel,'boundingbox','centroid');
imshow(Irgb); hold on;
for x = 1 : length(stat)
bb = stat(x).BoundingBox;
xc = stat(x).Centroid(1);
yc = stat(x).Centroid(2);
txt = sprintf('x : %.2f\ny : %.2f',xc,yc);
rectangle('position',bb,'edgecolor','b','linewidth',2);
plot(xc,yc,'bo');
text(xc,yc+30,txt,'color','y','fontweight','normal');
end
And the result shown below :
The pink colored object is not detected anymore.
It is so simple like I said. Just adjusting the values of hue, saturation, intensity.

9 Comments

It's simple if you have computer graphics. Not so simple if you have real world colors from, say, a snapshot of some real world scene such as a circus or hot air balloon festival or NASCAR race or whatever. And getting red is kind of tricky since it comprises two ranges in hsv, both less than ~0.1 and greater than ~0.9, because h jumps from 1 back down to 0 at the "+a" axis where reddish things lie.
Yes, I understand.
As the problems I'm facing right now.
I am currently working on a program of detection of traffic signs.
However the red color on any traffic signs are not the same.
But we do not yet know what the object is used by the OP.
Whether it's computer graphics or a snapshot of real-world scene.
Hey thanks man. Now what are the values for green, blue, yellow and black.
for yellow
low = 0.15;
high = 0.2;
for green
low = 0.3;
high = 0.35;
for blue
low = 0.65;
high = 0.7;
No, you need to be defining what those values mean for YOU. What one of us means by "blue" is not necessarily the same thing that YOU mean by "blue".
What do you see when you look out the window at the sky during daytime? What color is the sky? Answer: Everyone knows the sky is blue! But does that mean that the sky is always the same Hue, Saturation, and Intensity? No -- not even close!!
Thank you Chandra Kurniawan. The code is working. Now I am trying to move my bot which is in some point in the image. Assume that the bot is the pink colored circle in your plot. How would you move that bot to the red point?
I would be really happy if you could guide me
Thanks in advance.
That is a very different topic that should have its own Question.
hey thank u very much then what are the values for other different colors
Extract them, then enter in a location and find out:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);

Sign in to comment.

More Answers (1)

You have already been guided in your previous copy of this question.
In order to be guided further, you need to answer the question there as to how exactly you define each color.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 13 Jan 2012

Edited:

DGM
on 14 Feb 2023

Community Treasure Hunt

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

Start Hunting!