loading
Video processing has always been challenging to robot builders as the coding is complex and people who don’t have the knowledge in MATLAB can’t do much, but in this tutorial we will be learning how to program a robot to follow a yellow coloured ball, using image processing, which works much faster than MATLAB programming and is much easier.

• For this I used VIZ bot kit from TRI, or you can make your our board which consist of a driver board or(L293D)that takes inputs from pin 5 and pin 2 of the parallel port.

• You would also need a WEB cam for capturing the video.

• Download and install Robo Realm.

Step 1: Set Up Roborealm

Once you have set the camera, you will be able to see the video, now place an object in front of the screen.

Step 2: Blur!!

Now let’s add filters >Gaussian blur from left column to the command list to reduce noise. When we increase the blur the noise from the background is reduced as shown in the diagram below.

Step 3: Color Filter

Next to filter out the ball, we add colours>RBG filter form the left column, now select the colour you want to filter.

Step 4: COG

Now to calculate the position of the object on the screen we use analysis>centre of gravity, now we can select the option for stop tracking if pixels below 10% as shown below. And click ok.

Step 5: More on COG...

Now we can see the blue box around the object showing the x and y axis values (i.e. at the centre the box). The values are stored in two variables COG_X and COG_Y and the size of the box is COG_BOX_SIZE which we can use in the next module, the VB script module. Modules can even be searched from the search tab available on the left column.

Step 6: VB Module

Open the VB module and type in the code below, after selecting use the following text option.
xaxis=Getvariable("COG_X")
objsize=GetVariable("COG_BOX_SIZE")

if (objsize>10) then

if (xaxis>160) then
SetVariable "pin2",0
SetVariable "pin5",1


Else if(x axis<160) then
Set Variable "pin2",1
Set Variable "pin5",0


end if

Explanation of the code:
Get Variable is used to get the COG_X and COG_BOX_SIZE and store in variables “x axis” and “objsize” now by placing the ball in the centre of the screen we can find out the value of the object on the x-axis and size of the box. Next we have a nested ‘if loop’. The first loop checks box sixe value. If the box size is very big it means the object is very near. So we stop; but if it is a small value we check the next two ‘if conditions’, where if the x axis is greater than 160(approx centre) we declare two new variables pin5 and pin2 and make it 1 and 0 respectively and if lesser than 160 we make pin5 and pin2 as ‘0’ and ‘1’. Now click on reload and run to check if the program you wrote is functioning properly. You can even see the variables you have created on the tables in the right hand. You can now move the ball from left to right and see the variables change.

Step 7: Connecting Your BOT...

Now to control your robot we use a parallel port module found in the path control>others>parallel-port now at pin5 and pin2. Select the variables “pin5” and “pin2” that we created. Then click on ok. Add it. Now connect the VIZ bot to the parallel port.
Alas! You have your own Vision processing robot.


if you dont have a LPT port u can use the serial port to transmit a particular string that can be processed on a Microcontroller...
<div><p>Hello i am using this code to <br>control motor using arduino and matlab. But after running this program <br>output is always stop moving even if i move the ball in all directions. <br>Please help on this. Thanks :)</p><p>clear;<br>clc % Clearing Matlab desktop<br>vid=videoinput('winvideo',1,'YUY2_320x240'); % Defining the video input object<br>set(vid,'FramesPerTrigger',1); % Setting frames per trigger<br>preview(vid); %////// Showing the video of the moving Ball(TO BE USED %<br>% WHILE TESTING)<br>pause(10);% Waiting for a certain time for the system to get initialised<br>rgb_image = getsnapshot(vid); % Storing Image in an array variable<br>[a b c]= size(rgb_image); % Determining the size of the captured frame.<br>y=a;<br>x=b;<br><br><br>% Defining Boundaries<br>x1=x/2-120;<br>x2=x/2+120;<br>y1=y/2-30;<br>y2=y/2+30;<br>ser=serial('COM34'); % Defining the specified COM Port to be used<br>fopen(ser); % starting serial Communication,opening serial port<br><br>while(1)<br>rgb_image = getsnapshot(vid); % storing image in an array variable<br>flushdata(vid); %Flushing the buffer<br>rbar=0;<br>cbar=0;<br>e=0;<br>fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);% Storing RGB components of the image in seperate arrays<br>I=((fR&lt;=70)<br> &amp; (fG&gt;=80) &amp; (fB&lt;=70)); % Converting the RGB Image into <br>binary image///Detecting only the red component<br>% Following are the steps For Detecting the red ball<br>se=strel('disk',20);<br>B=imopen(I,se);<br>final=imclose(B,se);<br>[L,n]=bwlabel(final);<br>%imshow(rgb_image); %////THIS IS TO BE USED ONLY WHILE TESTING<br>%hold on % ////THIS IS TO BE USED ONLY WHILE TESTING<br>for k=1:n<br>[r,c]=find(L==k);<br>rbar=mean(r);<br>cbar=mean(c);<br>%plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B' ,'MarkerSize',20) %////THIS IS TO BE USED ONLY WHILE TESTING<br>e=(((cbar&gt;=x1)*2*2*2) + ((cbar&lt;=x2)*2*2) + ((rbar&gt;=y1)*2) + (rbar&lt;=y2)) % Converting to decimal number<br>end<br>% Decision Making Conditions<br>switch (e)<br>case 5<br>disp('Move left'),fprintf(ser,'L');<br>case 6<br>disp('Move left'),fprintf(ser,'L');<br>case 7<br>disp('Move left'),fprintf(ser,'L');<br>case 9<br>disp('Move right'),fprintf(ser,'R');<br>case 10<br>disp('Move right'),fprintf(ser,'R');<br>case 11<br>disp('Move right'),fprintf(ser,'R');<br>case 13<br>disp('Move forward'),fprintf(ser,'F');<br>case 14<br>disp('Move back'),fprintf(ser,'B');<br>otherwise<br>disp('Stop Moving'),fprintf(ser,'S');<br>end<br>end<br>fclose(ser); % closing serial port</p></div>

About This Instructable

16,058views

33favorites

License:

Bio: DIY, electronics...and loves getting creative...
More by sajingeo:Amazon Echo starts your car Real World Minecraft Bluetooth Speakers using Raspberry Pi 
Add instructable to: