## Re: Color Detection Algorithm

Well, that's true, I had not read the thread for a long period of time.
Anyway, probably it's too late, but here some code I wrote for color detection (though, the pseodo code I had given was, in my opinion quite clear and easy to be transposed to matlab code).

Anyway, the following code is a matlab function that takes the following arguments:
1) RGB: a MxNx3 matrix of the RGB values
2) mode: this argument should take the values 1,2 or 3, depending on the color you want to detect (red, green or blue).
3) thres: a threshold that is used to detect the desired color in the following way: if the desired color is LARGER than thres * (each of the other two colors), then the current pixel is labelled as 1 (color found).

Code:
```function I = findColor(RGB, mode, thres)
% function BIN = findColor(RGB, rgbVal, Thres)
% Function for detecting a specific rgb value, within acceptable tollerance

% RGB: the rgb image
% mode: the color to detect:
%       1: red
%       2: green
%       3: blue
% thres: the distance tollerance

[M,N,t] = size(RGB);

switch (mode)
case 1
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,1) > thres * RGB(:,:,2)) ) = 1;
I2( find(RGB(:,:,1) > thres * RGB(:,:,3)) ) = 1;
strTitle = 'Color RED  detected (white areas)';
I = I1 .* I2;
case 2
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,2) > thres * RGB(:,:,1)) ) = 1;
I2( find(RGB(:,:,2) > thres * RGB(:,:,3)) ) = 1;
strTitle = 'Color GREEN  detected (white areas)';
I = I1 .* I2;
case 3
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,3) > thres * RGB(:,:,1)) ) = 1;
I2( find(RGB(:,:,3) > thres * RGB(:,:,2)) ) = 1;
strTitle = 'Color BLUE  detected (white areas)';
I = I1 .* I2;
otherwise
fprintf('WRONG ARGUMENTS'\n);
return;
end

subplot(2,1,1),imshow(RGB); title('Original Image');
subplot(2,1,2),imshow(I,[]); title(strTitle);```