Hi,
I wrote code to track colors in MATLAB. The code plots three circles (Red, Blue, and Green) that follow the motion of colors moving on in front of the web cam (R, B, G). Red and blue work perfectly, but green does not. Any ideas? Here is my code after I set up the video acquisition:
% Get snapshot of current frame
snap = getsnapshot(vid);
% Extract red by subtracting red from grayscale
snap_red = imsubtract(snap(:,:,1), rgb2gray(snap));
snap_blue = imsubtract(snap(:,:,3), rgb2gray(snap));
snap_green = imsubtract(snap(:,:,2), rgb2gray(snap));
% Filter out noise
snap_red = medfilt2(snap_red, [3 3]);
snap_blue = medfilt2(snap_blue, [3 3]);
snap_green = medfilt2(snap_green, [3 3]);
% Convert new snapshot to binary
snap_red = im2bw(snap_red,0.18);
snap_blue = im2bw(snap_blue,0.18);
snap_green = im2bw(snap_green,0.18);
% Remove pixles less than 300px
snap_red = bwlabel(snap_red, 8);
snap_blue = bwlabel(snap_blue, 8);
snap_green = bwlabel(snap_green, 8);
% Label all connected components
bw_red = bwlabel(snap_red,8);
bw_blue = bwlabel(snap_blue,8);
bw_green = bwlabel(snap_green,8);
% Properties for each labeled region
stats_red = regionprops(bw_red, 'BoundingBox','Centroid');
stats_blue = regionprops(bw_blue, 'BoundingBox','Centroid');
stats_green = regionprops(bw_green, 'BoundingBox','Centroid');
% Display new image
drawnow
% Puts red objects in rectangular box
for object_red = 1:length(stats_red)
bb_red = stats_red(object_red).BoundingBox;
bc_red = stats_red(object_red).Centroid;
plot(bc_red(1),-bc_red(2),'or')
axis([0 300 -300 0])
end
hold on
% Puts blue objects in rectangular box
for object_blue = 1:length(stats_blue)
bb_blue = stats_blue(object_blue).BoundingBox;
bc_blue = stats_blue(object_blue).Centroid;
plot(bc_blue(1),-bc_blue(2),'ob')
axis([0 300 -300 0])
end
% Puts blue objects in rectangular box
for object_green = 1:length(stats_green)
bb_green = stats_green(object_green).BoundingBox;
bc_green = stats_green(object_green).Centroid;
plot(bc_green(1),-bc_green(2),'og')
axis([0 300 -300 0])
end
hold off

2 commentaires

Vikash Varma
Vikash Varma le 12 Juil 2017
can anyone explain why we have to remove all those pixels less than 300 from a binary image which contains only either 0 or 1 ?
Do you actually mean "remove all those blobs less than 300 pixels"? If so, do this
binaryImage = bwareaopen(binaryImage, 300);

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 16 Mai 2012

0 votes

Subtracting from the grayscale version of the image is a bad way to find the green, or any color for that matter. You need to look at the difference between the color you want and the average of the other two. For example this might give a better green signal:
greenDifference = abs(greenChannel - (redChannel + blueChannel)/2);
Then threshold on that. Cast them all to double first so you don't get any clipping.

6 commentaires

Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran le 27 Déc 2013
can you explain that why we should take average of two channels here
Image Analyst
Image Analyst le 27 Déc 2013
If you want to know the "greenness" of an image you want to know how much greener each pixel is than something. Well, what should that something be? Maybe you want to know how much more the green intensity is than the red intensity? Well that sounds reasonable. Maybe you want to know how much more the green intensity is than the blue intensity? Well that also sounds reasonable. Which one should you pick? How about seeing how much more intensity it has in the green than the average of the red and blue? Maybe that would be better than just picking one or the other.
Marco
Marco le 6 Jan 2014
@Image Analyst: where could i find some reference about your solution ?
Image Analyst
Image Analyst le 6 Jan 2014
You can use me as a reference. This kind of analysis has been going on for decades and it's kind of ad hoc and intuitive so I don't know where to find textbooks or papers on it.
KDN
KDN le 8 Nov 2015
How can I detect white color and and it's brightness.
hsvImage = rgb2hsv(double(rgbImage));
h = hsvImage(:,:,1);
s = hsvImage(:,:,2);
v = hsvImage(:,:,3);
whitePixels = s < 0.2 & v > 0.8;
or something like that.
whitePixels is a binary image, or "mask". Feel free to modify the parameters.

Connectez-vous pour commenter.

Plus de réponses (2)

Andrea
Andrea le 15 Fév 2013

0 votes

Hello, to detect green should change a parameter in this condition:
%%snap_green = im2bw(snap_green,0.18);%%
0.18 is to detect objects with a light green colored and sometimes do not want to detect. I advise you to try with a strong green and put it to 0.09 parameter. I hope you still serve. Andrea
daher
daher le 27 Déc 2013

0 votes

Hi, i have a question.. assuming that we have to track the first identified blue object that appears in a video (O1) Next, another object (O2) with the same color (using HSV presentation) appears
how to distinguish between them and to keep tracking O1 no matter what will be identified later in the video without referring to geometry features of each object.. Any suggestions?

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by