0

Red object detection in video

Posted by SUYOG PATIL on 11:36 PM in , , ,

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; 
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);

    % 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)
 clear all



Code is self explanatory and easy to understand....

This will help you guys 2 get started....

Thanks....

Don't forget to like  my facebook page....

















|

0 Comments

Post a Comment

Copyright © 2009 ALL ABOUT ROBOTICS!! All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.