OpenCV Python Tutorial For Beginners 34 - Circle Detection using OpenCV Hough Circle Transform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the next video on open CV tutorial for beginners using Python we have already seen how to use Huff line transform to detect lines in open CV in this video we are going to see how we can use half circle transform to detect circles in an image now as you can see here I have this small example which loads an image and shows it into the I am show window so let me run this code and let me show you how this image looks like so you can see there are so many Smarties here inside this image and all the Smarties are of circle form right they are not the perfect circles but they are in the form of circles and we want to detect all these circles forms inside the image we can use half circle transform for that so let's see how we can use this half circle transform to detect the circles in the image so a circle is represented mathematically by this equation which you see on your screen so here X Center and the Y Center are the coordinates of the center and R here is the radius of the circle so if you know these three parameters then you can draw a circle so those coordinates of the circle and the radius of the circle we need to detect so now let's see how Hough circle method is applied using open CV so you might observe here that I have created a copy of this original image which I have read using this I am a read method in the next step I'm going to just convert this image into a grayscale image so I'm going to just write gray is equal to C V dot C BT color which is to take two parameters first is the source and second is the method so we are going to convert the color BGR to Cree now in the next step because our half-circle method works better with the blurred images so we are going to create this blurred image using median blur so I'm going to just say gray so we are going to overwrite this gray variable with CB 2 dot median love which is going to take few arguments first is the image itself so we are going to pass in gray here and the second is the key size or the kernel size so we are going to initially provide the kernel size of 5 here now we are going to apply our half-circles method so I'm going to declare this circles variable and then I'm going to just call this method which is called CV dot half-circles method so this is the method and you can see it takes few parameters we are going to give these parameters one by one first is the obvious one which is the image so we are going to provide the gray scale image here which is already blurred so the second parameter here is the method which we want to use currently the only implemented method is 1/2 gradient method so the choice is very simple here we are going to just provide this CV dot 1/2 gradient method the third parameter here will be a DP DP is the inverse ratio of accumulator resolution to the image resolution so for example when DP is equal to 1 the accumulator has the same resolution as the input image and if the DP is equal to 2 then accumulator has the half as big as width and the height so we are going to take this DP value as 1 the next parameter here will be min dist it is the minimum distance between the center of the detected circles okay so here we are going to give initially the value of 20 and later we will adjust this value if the circles are very near to each other the next parameter which we are going to give here is the value of parameter 1 and parameter 2 or param 1 or param two the param 1 is the first method specific parameter in case of 1/2 gradient it is higher threshold of the 2 passed to the canny edge detector param 2 is the second method specific to the method which we have provided here which is the half gradient method it is the accumulator threshold for the circle centers at the detection stage so we are going to provide the value of the paramon and param 2 here so let's start with the param 1 value and we are going to provide param 1 value is equal to 50 and param 2 value is equal to let's say 30 so those param 1 and param two parameters are specific to this method which we are using the next parameter which we are going to pass here is the min radius and the minimum radius is the minimum circle radius and we are going to just start with the 0 so we are going to say that anything which is greater than 0 we are going to just draw it and then we are going to provide the max radius if this max radius is greater than or equal to zero it uses the maximum image dimension if it's only greater than zero it returns Center without finding the radius so this also we are going to start with zero let me just break this function so you can see all the parameters here so this huff circle method is going to give us the circle vector which we can iterate upon but first of all we need to convert those circle parameters which we got using this circles variable that is X and y coordinate and the radius into an integer so to do that we are going to just declare a parameter called detected circles and then we are going to use numpy to convert them into an integer so I'm going to just say NP dot you in 16 and then in the parentheses I'm going to just use NP dot around and we are going to pass our circles parameter which we got using the half circles method now in the next step we are going to iterate over those detected circles so we are going to just say 4 and because this circle vector is going to give us X Y and the radius we can directly just extract those values so we are going to just say X comma Y comma R and then in our detected circles and those circles will be at this index so 0 comma colon and then inside this for loop we are going to first of all draw the circle and also we are going to draw the center so to draw the circle we already know that we have this circle method available which takes pew parameter first is the image so we are going to pass the copy of this image here so let's pass this copy which is output the second argument here will be the center which are the coordinate of the center which we already got in the form of X comma Y so we will give them in the form of tuple the third argument is the radius so radius is extracted in the R parameter here so we are going to pass the radius here and then the next parameter will be the color so let's start with let's say green color and then the thickness so we are going to give the thickness of let's say three here similarly when we use the same circle method and we want to draw the center then we know that this is the center so these are the coordinates of the centre and if the radius is very small let's say - then it's going to just draw a small point right so that's why I have given very a small value for example - here so it's going to just draw a very small circle which will look like a dot on the circle that's why this value is very small and we are going to just say that this will be also three and let's change the color of this dot let's say this will be this color okay so we are just drawing those circle on the copy of the image which is called the output so let's run this code and let's see what happens when we run this code and you can see this dot is first of all drawn on each circle which is detected which is in the form of yellow and also in the form of green all the circles are drawn so you can see this circle is drawn so every circular shape is you know and closed by the detected circle we also strangely detected that this circle somehow because open C we think that this is also a circle I have one more image which is shapes dot jpg so we are going to just see that also so I'm going to just say shapes dot jpg let me show you this image first of all so it looks like this so it has only one circle and some other shapes right so we are going to just run this code once again and you can see it just detect this circle and it just draws a small dot on the center and all the other shapes are undetected so this is how you can detect the circles inside an image using half circle transform I hope you enjoyed this video and I will see you in the next video you
Info
Channel: ProgrammingKnowledge
Views: 51,413
Rating: undefined out of 5
Keywords: OpenCV Tutorial, Python (Programming Language), Python 3.6, Python, Python Tutorial, Python course, Online Course, OpenCV, OpenCV Tutorial for Beginners, Computer Vision, Computer Vision Basics, Computer Vision Tutorial, Windows, Image Processing, OpenCV Python Tutorial, OpenCV Python, blurring images, Gaussian Pyramid, drawContours, Python Tutorial for Beginners, Self-Driving Cars, Finding Lane Lines, Simple Lane Detection, Hough Circle Transform
Id: dp1r9oT_h9k
Channel Id: undefined
Length: 12min 5sec (725 seconds)
Published: Wed Sep 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.