Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: Prevent HoughCircles method from using Canny Detection

I am using HoughCircles to detect a ball in real-time, but running Canny on my gray-scale image stream isn't creating all of the edges as it should. To remedy that, I am splitting the rgb image into it's separate channels, performing Canny on each one, then using bitwise or to merge the edges together. This is working quite well, but if I feed that edge image to HoughCircles, it will perform Canny again on the edge image. Is there a way to prevent this, or to forgo the rgb split Canny detection that I am performing while still catching all of the edges?

like image 869
Asher Johnson Avatar asked Nov 24 '13 23:11

Asher Johnson


2 Answers

Indeed! Canny is executed internally by HoughCircles and there's no way to call cv::HoughCircles() and prevent it from invoking Canny.

However, if you would like to stick with your current approach, one alternative is to copy the implementation of cv::HoughCircles() available on OpenCV's source code and modify it to suit your needs. This will allow you to write your own version of cv::HoughCircles().

If you follow this path, it's important to realize that the C++ API of OpenCV is built upon the C API. That means that cv::HoughCircles() is just a wrapper around cvHoughCircles(), which is implemented at opencv-2.4.7/modules/imgproc/src/hough.cpp after line 1006.

Take a look at this function (line 1006) and notice the call done to icvHoughCirclesGradient() at line 1064. This is the function responsible for invoking cvCanny(), which is done at line 817.

Another approach, if the ball is single-colored, could be implemented by using cv::inRange() to isolate a specific color, and this will provide a much faster detection. Also, this subject has been extensively discussed on this forum. One very interesting thread is:

  • Writing robust (color and size invariant) circle detection with OpenCV (based on Hough Transform or other features)
like image 100
karlphillip Avatar answered Oct 03 '22 08:10

karlphillip


For people who are looking for using a custom edge detection with circle detection in Python, you can use OpenCV's Canny edge detection function and pass it to scikit-image's (skimage) hough_circle function (http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.hough_circle).

Skimage's hough_circle function doesn't internally perform Canny edge detection, thus giving you a chance to implement your own. Below is an example:

hough_results = hough_circle(cv2.Canny(IMAGE, LOWER_THRESHOLD, UPPER_THRESHOLD), np.arrange(MIN_RADIUS, MAX_RADIUS,1))

like image 44
Uzay Macar Avatar answered Oct 03 '22 06:10

Uzay Macar