0% found this document useful (0 votes)
46 views

Object Follower Code

The document is a MATLAB code that uses image processing and color detection to continuously track a ball and control a robot based on the ball's position. It initializes a video input, takes frames, converts to grayscale, detects connected pixel regions, identifies the largest region as the ball, calculates its centroid position, and sends movement commands to a serial port based on the centroid to move the robot left, right, forward or back.

Uploaded by

s2sarath
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Object Follower Code

The document is a MATLAB code that uses image processing and color detection to continuously track a ball and control a robot based on the ball's position. It initializes a video input, takes frames, converts to grayscale, detects connected pixel regions, identifies the largest region as the ball, calculates its centroid position, and sends movement commands to a serial port based on the centroid to move the robot left, right, forward or back.

Uploaded by

s2sarath
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

NEX Robotics Pvt. Ltd. e-mail: [email protected] web: http://www.nex-robotics.

com

% Image Processing with Matlab % Using Fire Bird to continuously track a ball Totalframes = 100; vid=videoinput('winvideo',1,'RGB24_320x240'); set(vid,'FramesPerTrigger',1 ); set(vid,'TriggerRepeat', Inf); config = triggerinfo(vid); triggerconfig(vid,config(2)); ser= serial('COM1','BaudRate',9600,'DataBits',8); fopen(ser); ser.Timeout=10; %default value=10 sec start(vid); % camera warm up for loop=1:10 trigger(vid); imrgb=getdata(vid,1); end for loop=1:Totalframes trigger(vid); imrgb=getdata(vid,1); [height width comp]=size(imrgb); imgray=zeros(height, width); % Color detection for i=1:height for j=1:width if ((imrgb(i,j,1)>150 && imrgb(i,j,1)<200) && (imrgb(i,j,2)>75 && imrgb(i,j,2)<=125) && (imrgb(i,j,3)>=0 && imrgb(i,j,3)<50)) imgray(i,j)=255; end end end imshow(imgray); L = bwlabel(imgray,8); %Calculating the connected components/objects object_count=max(max(L)); % Number of connected components /objects % find size of all objects pixels=zeros(1,object_count); for i=1:object_count [rows cols] = find(L==i); rc = [rows cols]; [object_size temp]=size(rc); pixels(1,i)=object_size; end pixel_count=max(pixels(1,:));

NEX Robotics Pvt. Ltd. e-mail: [email protected] web: http://www.nex-robotics.com

if pixel_count>100 % Proceed if no. of connected pixels>100 % identify index of the largest object index = 0; for i=1:object_count if pixels(1,i)==pixel_count index=i; end end % find co-ordinates of largest object pixels [rows cols] = find(L==index); rc = [rows cols]; [object_size temp]=size(rc); % store the extracted object in an array n1=zeros(height,width); for i=1:object_size x1=rc(i,1); y1=rc(i,2); n1(x1,y1)=255; end cent=centroid(n1); % Center of Gravity of the ball % IDENTIFICATION OF SEGMENT % Motion according to the location of center of gravity if cent(1,1)<=120 display('left'); % left fwrite(ser,84,'async'); % left elseif cent(1,1)>=200 display('right'); % right fwrite(ser,83,'async'); % right elseif cent(1,2)<=200 display('forward'); % forward fwrite(ser,81,'async'); % forward elseif cent(1,2)>=220 display('back'); % back fwrite(ser,82,'async'); % back else display('stop'); % stop end end % Proceed if no. of connected pixels>100 end % end of for loop % Close video stop(vid); delete(vid); clear vid; % Close serial port fclose(ser); delete(ser); clear ser;

NEX Robotics Pvt. Ltd. e-mail: [email protected] web: http://www.nex-robotics.com

% CENTROID calculation function [cent]= centroid(im) s=size(im); cx=0;cy=0; n=1; for i=1:s(1,1) for j=1:s(1,2) if im(i,j)==255 n=n+1; cx=cx+i; cy=cy+j; end end end cxavg=cx/n; cyavg=cy/n; [cent]=[cyavg,cxavg];

You might also like