0
Red object detection in video
After done with basics now let us try some programming.
Here is code written by me to recognize the red object in live video.
I wrote this code for my project freestyle.
MatlabR2009 code:
clc
clear
vid=videoinput('winvideo',1,'YUY2_640x480');
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=3;
clear
vid=videoinput('winvideo',1,'YUY2_640x480');
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=3;
bc=zeros(2,100);
while(vid.FramesAcquired<=100)%user defined no. of frames
data = getdata(vid,1);% Get the snapshot of the current frame
copy=data;
r=data(:,:,1);
g=data(:,:,2);
b=data(:,:,3);
copy(g>100)=0;
copy(b>100)=0;
copy(r<170)=0;
r=copy(:,:,1);
r=im2bw(r);
data = getdata(vid,1);% Get the snapshot of the current frame
copy=data;
r=data(:,:,1);
g=data(:,:,2);
b=data(:,:,3);
copy(g>100)=0;
copy(b>100)=0;
copy(r<170)=0;
r=copy(:,:,1);
r=im2bw(r);
% Remove all those pixels less than 300px
r = bwareaopen(r,300);
% Label all the connected components in the image.
bw = bwlabel(r, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw,'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bc(:,i) = stats(object).Centroid;
plot(bc(1,i),bc(2,i), '-m+')
a=text(bc(1,i)+15,bc(2,i), strcat('X: ', num2str(round(bc(1,i))), ' Y: ', num2str(round(bc(2,i)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
i=i+1;
end
hold off
end
% Stop the video acquisition
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
delete(vid)
% Flush all the image data stored in the memory buffer.
flushdata(vid);
delete(vid)
clear all
This will help you guys 2 get started....
Thanks....
Don't forget to like my facebook page....