Ball detection is pretty easy on OpenCV. So to start with lets describe what steps we will go through.
LINK TO THE CODE
1.Load an image / start a video capture
2.Convert image from RGB space to HSV space . HSV(hue saturation value) space gives us better results while doing color based segmentation.
3.Seperate Image into its 3 component images(i.e H S V each of which is a one dimensional image or intensity image)
4.Use a condition for intensity values in the image and get a Binary image.
i.e let say we taken H intensity image .If our ball is red color .Then in this image we will find that the values of the pixel where the ball is present , lies in a specific range. so we define a condition for every pixel . if (pixel > threshold_min & pixel < threshold_max)= pixel of o/p image is 1 else it is zero.
NOTE:
FOR THE PURPOSE OF CALIBRATION WE HAVE 2 SLIDERS ON EACH COMPONENT IMAGE TO SET THE LOWER AND UPPER LIMIT OF PIXEL VALUES.
We do this for all components i.e for S and V.
5.Now we have three binary images( only black and only white) . Which has the region of ball as 1's and every thing else which has the intensity values greater(less) than threshold .The pixels that do not pass this conditions will be zero.
6.We then combine all the above three Binary images (i.e we AND them all). All the pixels that are white in the three images will be white in the output of this step.So there will be regions too which will have 1's but with lower areas and of random shapes.
7.Now we use houghs transform on the output of last operation to find the regions which are circular in shape.
8.Then we draw the marker on the detected circles as well as display the center and radius of the circles
LINK TO THE CODE
1.Load an image / start a video capture
2.Convert image from RGB space to HSV space . HSV(hue saturation value) space gives us better results while doing color based segmentation.
H component |
S component |
V component |
4.Use a condition for intensity values in the image and get a Binary image.
i.e let say we taken H intensity image .If our ball is red color .Then in this image we will find that the values of the pixel where the ball is present , lies in a specific range. so we define a condition for every pixel . if (pixel > threshold_min & pixel < threshold_max
NOTE:
FOR THE PURPOSE OF CALIBRATION WE HAVE 2 SLIDERS ON EACH COMPONENT IMAGE TO SET THE LOWER AND UPPER LIMIT OF PIXEL VALUES.
H component after condition |
We do this for all components i.e for S and V.
S component after condition |
V component after condition |
6.We then combine all the above three Binary images (i.e we AND them all). All the pixels that are white in the three images will be white in the output of this step.So there will be regions too which will have 1's but with lower areas and of random shapes.
Combined image |
8.Then we draw the marker on the detected circles as well as display the center and radius of the circles
Hi,
ReplyDeleteThanks for this tutorial what about white ball detection
Can I please see the python code for this tutorial
ReplyDeleteCode link is mentioned at the very top of the page
DeleteVery nice idea ! thanks a lot !
ReplyDeleteGood job man ! Really useful and stable, Thanks
ReplyDeleteCan you provide android application code?
ReplyDeleteHello please upload python code too.
ReplyDeleteI am facing problem when applying condition for s component could you please help me with that part.I mean to say could you please give some practical values for codition.
ReplyDeletecan i use this in the javascript for tracking ball speed and spin for table tennis
ReplyDeleteWhat is the smallest size this code can trace?
ReplyDeletehow can i detect multiple objects
ReplyDeleteThis comment has been removed by the author.
ReplyDeletegreat article
ReplyDeletevery helpful for my project
So if I have two balls can it track unique objects, IE assign I'd 1 and 2 to each ball and show the ball moving with the same object id
ReplyDeletesource code is the king
ReplyDeleteKrishnafurniture offers
ReplyDeletehttps://krishnafurniture.com/mattresses
https://krishnafurniture.com/study-chairs/study-chair
https://krishnafurniture.com/sofa/sofa-sets