In our previous blog, we talked about contours in OpenCV. In this blog, we take this thread of computer vision forward and try to identify a circle in a binary image.
We need robots to identify circles as it's a very basic shape found in our surroundings.
Fortunately, we don't need to start from scratch here as OpenCV provides built-in function for using the Hough Circles algorithm using HoughCircles() function as below -
Python: cv2.HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) → circles
(Source : http://docs.opencv.org/2.4/modules/imgproc/doc/feature_detection.html)
image – 8-bit, single-channel, grayscale input image.
circles – Output vector of found circles. Each vector is encoded as a 3-element floating-point vector (x, y, radius).
circle_storage – In C function this is a memory storage that will contain the output sequence of found circles.
method – Detection method to use. Currently, the only implemented method is CV_HOUGH_GRADIENT , which is basically 21HT , described in [Yuen90].
dp – Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
minDist – Minimum distance between the centres of the detected circles. If the parameter is too small, multiple neighbour circles may be falsely detected in addition to a true one. If it is too large, some circles may be missed.
param1 – First method-specific parameter. In case of CV_HOUGH_GRADIENT , it is the higher threshold of the two passed to the Canny() edge detector (the lower one is twice smaller).
param2 – Second method-specific parameter. In the case of CV_HOUGH_GRADIENT , it is the accumulator threshold for the circle centres at the detection stage. The smaller it is, the more false circles may be detected. Circles, corresponding to the larger accumulator values, will be returned first.
minRadius – Minimum circle radius.
maxRadius – Maximum circle radius.
If this sounds confusing have a look at one worked out function as -
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array(), 100, 30, 1, 35)
One word of trouble shooting from us, please tweak minDist parameter if you miss a circle or get a false circle. The documentation of OpenCV tells that a higher value may cause a circle to be missed and a lower value would detect false circles.
We have use HoughCircles in robots as SID2 to identify a ball by its shape because a ball in 2 d is a circle. So please go ahead an try to code a python program to find a circle. In our next blog we will share a code for you to use.