@prev_tutorial{tutorial_hough_lines} @next_tutorial{tutorial_remap}
| | | | -: | :- | | Original author | Ana Huamán | | Compatibility | OpenCV >= 3.0 |
In this tutorial you will learn how to:
In the line detection case, a line was defined by two parameters \f$(r, \theta)\f$. In the circle case, we need three parameters to define a circle:
\f[C : ( x{center}, y{center}, r )\f]
where \f$(x{center}, y{center})\f$ define the center position (green point) and \f$r\f$ is the radius, which allows us to completely define a circle, as it can be seen below:
For sake of efficiency, OpenCV implements a detection method slightly trickier than the standard Hough Transform: The Hough gradient method, which is made up of two main stages. The first stage involves edge detection and finding the possible circle centers and the second stage finds the best radius for each candidate center. For more details, please check the book Learning OpenCV or your favorite Computer Vision bibliography
@add_toggle_cpp The sample code that we will explain can be downloaded from here. A slightly fancier version (which shows trackbars for changing the threshold values) can be found here. @include samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp @end_toggle
@add_toggle_java The sample code that we will explain can be downloaded from here. @include samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java @end_toggle
@add_toggle_python The sample code that we will explain can be downloaded from here. @include samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py @end_toggle
The image we used can be found here
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp load @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py load @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java load @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp convert_to_gray @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py convert_to_gray @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java convert_to_gray @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp reduce_noise @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py reduce_noise @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java reduce_noise @end_toggle
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp houghcircles @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py houghcircles @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java houghcircles @end_toggle
with the arguments:
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp draw @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py draw @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java draw @end_toggle
You can see that we will draw the circle(s) on red and the center(s) with a small green dot
@add_toggle_cpp @snippet samples/cpp/tutorial_code/ImgTrans/houghcircles.cpp display @end_toggle
@add_toggle_java @snippet samples/python/tutorial_code/ImgTrans/HoughCircle/hough_circle.py display @end_toggle
@add_toggle_python @snippet samples/java/tutorial_code/ImgTrans/HoughCircle/HoughCircles.java display @end_toggle
The result of running the code above with a test image is shown below: