Click here to Skip to main content
15,791,934 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi. I have this code here. I want to know how do I detect the colour that I want. For example yellow. Thanks.

% Demo macro to very, very simple color detection in RGB color space
% by ImageAnalyst
close all;

% Read standard MATLAB demo image.
rgbImage = imread('onion.png');

% Display the original image.
subplot(3, 4, 1);
title('Original RGB Image');

% Maximize figure.
set(gcf, 'Position', get(0, 'ScreenSize'));

% Split the original image into color bands.
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);

% Display them.
subplot(3, 4, 2);
title('Red band');
subplot(3, 4, 3);
title('Green band');
subplot(3, 4, 4);
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');

% Combine the masks to find where all 3 are "true."
redObjectsMask = uint8(redMask & greenMask & blueMask);
subplot(3, 4, 9);
imshow(redObjectsMask, []);
title('Red Objects Mask');
maskedrgbImage = uint8(zeros(size(redObjectsMask))); % Initialize
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* redObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
subplot(3, 4, 10);
title('Masked Original Image');
Updated 26-May-10 0:31am
Member 12198424 10-Dec-15 6:51am    
The above code is detecting only primary colours. how to detect yellow colour as well as other colours plz suggest me as soon as possible. Thanks in advance

Share this answer
Member 12198424 14-Dec-15 15:03pm    
How can we send picture information detected from the above code can be used to control external appliances like motor using an aurdino board. Ex if I detected red colour I want my motor to move in forward direction and if the colour I detected was green means the motor has rotate In anti clock wise direction(pls tell me about how to send and use picture information in subplots but not about hardware supporting packages)
Do you have any method of reading a resistor value (resistor colour codes) using image processing techniques?
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900