Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

merge different color

3 views
Skip to first unread message

memo mohd

unread,
Jan 2, 2010, 5:28:02 PM1/2/10
to
hello for all ,,

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

ImageAnalyst

unread,
Jan 2, 2010, 5:53:48 PM1/2/10
to
On Jan 2, 5:28 pm, "memo mohd" :
Here's a demo I've been working on. It's still in progress but you
should EASILY be able to adapt it to find the yellow objects instead
of the red objects. Just adjust the thresholds - very simple. This
is a very elementary color classification method performed in rgb
space. If you want, you could do it in hsv color space as well - the
hsv way might better compensate for exposure differences. But this
could help you get started.

% 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'});

memo mohd

unread,
Jan 2, 2010, 6:28:03 PM1/2/10
to
thanks image analyst
but there are error here when implemente the code

figuresc(0.9, 0.8);

???

ImageAnalyst

unread,
Jan 2, 2010, 9:07:53 PM1/2/10
to
Sorry - that was a function to enlarge the figure.
Delete it and replace it with this:

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

memo mohd

unread,
Jan 3, 2010, 1:00:06 PM1/3/10
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <220367f6-7e19-
thanks , it is work done
but it is large code , can i use simple than it ?
see this code but there are errors also
clear
M=(imread('D:\f4.jpg'));
figure('Name','Original')
image(M)
[row,col]=size(M);
for i=1:row
for j=1:col

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

ImageAnalyst

unread,
Jan 3, 2010, 9:33:00 PM1/3/10
to
memo:
No doubt you've found the error by now.
You didn't give any condition on your "if" line. It needs to say
if (condition)
% Stuff to do if condition == true
else
% Stuff to do if condition == false
end

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

0 new messages