Knowledge Base‎ > ‎General Gyaan‎ > ‎Tutorials‎ > ‎

Ball detection using Matlab

posted Sep 17, 2016, 7:08 AM by Rohit Bhaskar

BALL DETECTION USING MATLAB 

Category: IMAGE PROCESSING 


Description: Detecting a ball involves image processing. The best way to do so can be writing your algorithms on MATLAB or openCV, where we can load our image on the data base and do required math’s on the image. This project involves detecting a ball in real time and continuously noting the ball coordinates. The coordinates obtained can be used to control motors to follow the ball (ball follower) or to control your camera to move along its 2 axis with the moving ball. Thus the input to the algorithm is image and output is x and y coordinates of the ball. 

Requirements: MATLAB, WEBCAM. 

Coding:
 The code is written in MATLAB. To understand the code one must go through GETTING STARTED of matlab help. 

CODE: 

imaqhwinfo 
info = imaqhwinfo('winvideo') 
dev_info = imaqhwinfo('winvideo',1) 
vid = videoinput('winvideo'); 
set(vid,'TriggerRepeat',Inf); 
vid.FrameGrabInterval = 1; 
vid.FramesPerTrigger=20; 


figure; % Ensure smooth display 

set(gcf,'doublebuffer','on'); 
start(vid) 
while(vid.FramesAcquired<=1000) 
I = getdata(vid,1); 
fR=I(:,:,1); 


MY=fR >180; 



MY = bwareaopen(MY,5); 

se = strel('disk',4); 
MY = imclose(MY,se); 
MY = imfill(MY,'holes'); 


J=rgb2hsi(I); 

H=J(:,:,1); 
MY1=H < 0.5; 


F=MY & MY1; 



F = bwareaopen(F,5); 

se = strel('disk',4); 
F = imclose(F,se); 
F = imfill(F,'holes'); 


imshow(F) 



[B,L] = bwboundaries(F,'noholes'); 



s = regionprops(L, 'centroid'); 

cor = cat(1, s.Centroid); 


disp(cor); 



%pause(0.05); 

flushdata(vid); 
end 
stop(vid) 




Author:
 ANIKET .A . TATIPAMULA
Mobile : 9969982534.

Video linkshttp://www.youtube.com/watch?v=_FIU2rLs2mA 
Comments