Detect different colors in RGB colorspace
1 次查看(过去 30 天)
显示 更早的评论
Hello, I am doing a project to detect different colors in RGB colorspace. If i will put an image then it should show that whether it is a red color, blue or green. Or it is any other color having combination of those colors. If i will put three or more colors in an image then it should detect those colors. I need help to do it. Please tell me if the code given below is correct. What should i do next if it is correct?
thank u...
rgbImage = imread('image.png');
% Display the original image. subplot(3, 4, 1); imshow(rgbImage); title('Original RGB Image');
% Split the original image into color bands. redBand = rgbImage(:,:, 1); greenBand = rgbImage(:,:, 2); blueBand = rgbImage(:,:, 3);
% Display them. subplot(3, 4, 2); imshow(redBand); title('Red band'); subplot(3, 4, 3); imshow(greenBand); title('Green band'); subplot(3, 4, 4); imshow(blueBand); title('Blue Band');
% Threshold each color band. redthreshold = 68; greenThreshold = 70; blueThreshold = 72; redMask = (redBand > redthreshold); greenMask = (greenBand < greenThreshold); blueMask = (blueBand < blueThreshold);
% Display them. subplot(3, 4, 6); imshow(redMask, []); title('Red Mask'); subplot(3, 4, 7); imshow(greenMask, []); title('Green Mask'); subplot(3, 4, 8); imshow(blueMask, []); title('Blue Mask');
0 个评论
回答(1 个)
Geoff
2012-5-8
Are you trying to detect a specific colour? All you are doing here is thresholding, and your code is going to detect anything that generally has a stronger red component than green or blue. And that'll be just about half the colours in the spectrum.
If you need to find a particular hue, you need to look at the relationship between its red, green and blue components. For example, to find an orangish colour, you might look for pixels that have R = 2*G and B = 0. But it won't be exact. You might want a tolerance of 5%:
% Note: Assume the bands have been converted to double
orangish = abs(R ./ G - 2) / 2 <= 0.05 & B <= R * 0.05;
That will give a logical true value for anything that looks orange.
If you don't know what colours you need to find or how many, you'll have to work out how similar a colour can be before it can be considered "the same", and then do some sort of clustering.
1 个评论
Walter Roberson
2012-5-8
There is no nice grouping that defines the named colors.
See the MATLAB File Exchange contribution by John D'Errico, dealing with Fuzzy Color identification.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!