Code covered by the BSD License  

Highlights from
Color Follower Robot

Color Follower Robot

by

 

29 Apr 2010 (Updated )

Code detects the red object in the image captured by webcam and communicates with micro-controller

capture_vdo.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Tejas Varunjikar
%Grad Student, Penn State University
%Project: Image Processing Robot (Mechatronics)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%configure hardware input device
vid=videoinput('winvideo',1); 
%note: use 'imaqhwinfo' command if multiple webcams
%set trigger to manual
triggerconfig(vid,'manual'); 
%Capture one frame per trigger
set(vid,'FramesPerTrigger',1 );
%We are going to trigger and capture image in infinite loop hence we set
%trigger repeat value to infinite
set(vid,'TriggerRepeat', Inf);
start(vid); %start video

%Configure serial port
s=serial('COM11');
%Open serial port object
fopen(s);
%Total pixels captured by camera
total_pixels=240*320; %Number of pixels in the image for each channel

% 0 default value is set. Default value turns 1 if no image captured
default=0;
%M=0 means DC motor is OFF by default
M=0;
%M=1 is used to set DC motor ON

%Capital Letter: DC motor ON
%small letters: DC motor OFF

%Infinite while loop
while(1)
%Trigger webcam    
trigger(vid);
%Get Image
im=getdata(vid,1);
%Display image
imshow(im);
%default value set to zero
default=0;
%Image converted to ycbcr format from rgb since this format separates
%intensity from color
im_new=rgb2ycbcr(im);
%figure;imshow(im_new(:,:,3));
%size of image
sz=size(im_new);
m=sz(1,1);
n=sz(1,2);
%Creating a binary matrix binred. Value of an element is 1 only if color is
%red for that position
binred=zeros(m,n); 
%num is number of pixels
num=0; 
%Center of red object initialized to (I,J)=(0,0)
I=0;J=0;
output='n'; %default output n:No steering, DC motor OFF
%Insider im_new image find red pixels by following loop
for i=1:m 
    for j=1:n 
        %IF statement decides if pixel is only red (& not other color)
        if(im_new(i,j,3)>180 && im_new(i,j,2)<140 )
            %RGB sequence
            binred(i,j)=1; num=num+1; 
            I=I+i;
            J=J+j;
        end 
    end 
end

%Now, let's determine if num is not too SMALL or too LARGE
%Object should be between 0.2% to 25% on the frame for DC motor to be ON
%This piece of code decides whether to run DC motor or NOT
if((num<0.002*total_pixels)||(num>0.25*total_pixels))
    M=0; %DC Motor OFF
else M=1;%DC Motor ON
end

%IF no red object is detected
if (num==0)
    num=1;
    %disp('error: num is one');
    default=1;    %default is set to 1 which automatically turns mode 
                  %    to neutral subsequently
end
%Find center of object
I2=I/num;
J2=J/num;
%COnvert it to int
I3=int16(I2);
J3=int16(J2);
%For no red object
if((I3==0)||(J3==0))
    I3=1;J3=1;
    %disp('error: Attempted to access binred(0,0); index must be a positive integer or logical');
end

%Let's create a window where object is considered to be in the center
%Let + or - 5% from center be considered to be still at center
%Increase this limit to 10% if needed. 
%Assumption: Object is not very far in the frame

%If X coordinate of center is to the right
if(J3>(n/2+0.05*n))
    %L: turn right with DC motor ON
    if(M==1)
        output='R';
        disp('R');
    end
    %L: turn right with DC motor OFF
    if(M==0)
        disp('r');
         output='r';
    end
end

%If X coordinate of center is to the left
if(J3<(n/2-0.05*n))
   %L: turn left with DC motor ON
    if(M==1)
        output='L';
        disp('L');
    end
    %l: turn left with DC motor OFF
    if(M==0)
         output='l';
         disp('l');
    end 
   
end

if ((J3>(n/2-0.05*n)) && (J3<(n/2+0.05*n)) )
%Keep on going straight with no steeing (DC motor ON)
if(M==1)
        output='N';
        disp('N');
end
%Stay at current position: No steering, DC motor OFF
    if(M==0)
         output='n';
         disp('n');
    end

end

if(default==1) %Overriding the output if no red object in image as such
        %Stay at current position: No steering, DC motor OFF 
        output='n';
         disp('n');
end
%Send final output to the serial port object
fprintf(s,output);
%disp(output);


end
%BE sure to run following commands separately if you manually break
%infinite while loop
%These two commands clear video & serial object
fclose(s);delete(s);clear s;
stop(vid),delete(vid),clear vid;

Contact us