OpenCV: Prevent HoughCircles method from using Canny Detection
Asked Answered
H

2

10

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?

Hyperthyroidism answered 24/11, 2013 at 23:25 Comment(0)
K
11

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:

Kneecap answered 25/11, 2013 at 2:20 Comment(3)
The HoughCircles method inside of OpenCV does perform Canny internally. docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/… I'd like to be able to shut that off. The ball I'm looking for isn't a single color, nor do we know the ball ahead of time. I've read the question you're referring to before, but it doesn't answer the question that I've asked here though.Hyperthyroidism
Thanks, that's what I was thinking about doing, but was hoping there would be a way around it. Do you know of any other open source implementations of Hough Circles that are more simple that I can adjust how it works? Some tweaks I want to make to the implementation to speed it up.Hyperthyroidism
By the way, here's an interesting Java implementation. Feel free to up vote my answer if it helped you or select it as the official problem solver. By doing these doing you are helping future visitors.Kneecap
E
1

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))

Eben answered 9/9, 2018 at 20:20 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.