I want to detect different colors from image , i am use this code
hr=M(:,:,1); %red
% image(hr)
% figure('Name','blue')
hb=M(:,:,3); %blue
% image(hb)
but , if I want to detect color rather than red , green , blue
i want to merge the green and blue for define yellow , but how can I modify this code for do that ?
thanks a lot
% Demo macro to very, very simple color detection
% by ImageAnalyst
clc;
close all;
figuresc(0.9, 0.8);
% Read standard MATLAB demo image.
rgbImage = imread('onion.png');
% Display the original image.
subplot(3, 4, 1);
imshow(rgbImage);
title('Original RGB Image');
% Split 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.
% first set up low and high thresholds.
lowRed = 68;
highRed = 255;
lowGreen = 0;
highGreen = 70;
lowBlue = 0;
highBlue = 72;
% Then get the binary image of where the color band
% falls within that threshold range.
redMask = (redBand > lowRed) & (redBand < highRed);
greenMask = (greenBand > lowGreen) & (greenBand < highGreen);
blueMask = (blueBand > lowBlue) & (blueBand < highBlue);
% 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."
% This will be where the "red" objects are.
redObjectsMask = redMask & greenMask & blueMask;
subplot(3, 4, 9);
imshow(redObjectsMask, []);
title('Combined Mask');
% Fill in holes, and display the image.
redObjects = uint8(imfill(redObjectsMask, 'holes'));
subplot(3, 4, 10);
imshow(redObjects, []);
title('Filled Mask');
% Multiply this mask times the original RGB image
% to get an image that has the red objects only.
maskedImageR = redObjects .* redBand;
maskedImageG = redObjects .* greenBand;
maskedImageB = redObjects .* blueBand;
maskedImage = cat(3, maskedImageR, maskedImageG, maskedImageB);
% Display it.
subplot(3, 4, 11);
imshow(maskedImage, []);
title({'Filled Mask times Original =';'Red Objects Only'});
figuresc(0.9, 0.8);
???
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
if
M(i,j,1:3) = hr[255 0 0 ]; %red
image(M)
figure('Name','red')
else
M(i,j,1:3) = hr[0 255 255]; %yellow
image(M)
figure('Name','yellow')
end
end
end
the error in if
(Expression or statement is incomplete or incorrect.)
can you tel me where the error in code ?
thanks a lot
The else is optional of course.
Your problem was that you had no condition whatsoever.
Secondly you did
M(i,j,1:3) = hr[255 0 0 ];
What is hr[255 0 0 ]? What is hr? Is it a function or an array or a
scalar? If hr is a scalar you need to have a * in between it and the
bracket to do the multiply. If it's a function or array, you need to
have parentheses instead of brackets and use commas between the
numbers. If that is actually the condition of the if, then you also
need to put it on the same line as the if, get rid of the semicolon,
and use == instead of =.
And then review the Getting Started section of the help.
The small chunk of code I gave you was not large at all - far from
it. My typical "real world" MATLAB application is about 2000 lines of
code, so I think anything less than about 500 lines is tiny. But
nonetheless, feel free to adapt it or simplify it as you see fit.
Regards,
ImageAnalyst