.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/edges/plot_circular_elliptical_hough_transform.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_edges_plot_circular_elliptical_hough_transform.py: ======================================== Circular and Elliptical Hough Transforms ======================================== The Hough transform in its simplest form is a `method to detect straight lines `__ but it can also be used to detect circles or ellipses. The algorithm assumes that the edge is detected and it is robust against noise or missing points. Circle detection ================ In the following example, the Hough transform is used to detect coin positions and match their edges. We provide a range of plausible radii. For each radius, two circles are extracted and we finally keep the five most prominent candidates. The result shows that coin positions are well-detected. Algorithm overview ------------------ Given a black circle on a white background, we first guess its radius (or a range of radii) to construct a new circle. This circle is applied on each black pixel of the original picture and the coordinates of this circle are voting in an accumulator. From this geometrical construction, the original circle center position receives the highest score. Note that the accumulator size is built to be larger than the original picture in order to detect centers outside the frame. Its size is extended by two times the larger radius. .. GENERATED FROM PYTHON SOURCE LINES 37-71 .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from skimage import data, color from skimage.transform import hough_circle, hough_circle_peaks from skimage.feature import canny from skimage.draw import circle_perimeter from skimage.util import img_as_ubyte # Load picture and detect edges image = img_as_ubyte(data.coins()[160:230, 70:270]) edges = canny(image, sigma=3, low_threshold=10, high_threshold=50) # Detect two radii hough_radii = np.arange(20, 35, 2) hough_res = hough_circle(edges, hough_radii) # Select the most prominent 3 circles accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii, total_num_peaks=3) # Draw them fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(10, 4)) image = color.gray2rgb(image) for center_y, center_x, radius in zip(cy, cx, radii): circy, circx = circle_perimeter(center_y, center_x, radius, shape=image.shape) image[circy, circx] = (220, 20, 20) ax.imshow(image, cmap=plt.cm.gray) plt.show() .. image-sg:: /auto_examples/edges/images/sphx_glr_plot_circular_elliptical_hough_transform_001.png :alt: plot circular elliptical hough transform :srcset: /auto_examples/edges/images/sphx_glr_plot_circular_elliptical_hough_transform_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 72-96 Ellipse detection ================= In this second example, the aim is to detect the edge of a coffee cup. Basically, this is a projection of a circle, i.e. an ellipse. The problem to solve is much more difficult because five parameters have to be determined, instead of three for circles. Algorithm overview ------------------- The algorithm takes two different points belonging to the ellipse. It assumes that these two points form the major axis. A loop on all the other points determines the minor axis length for candidate ellipses. The latter are included in the results if enough 'valid' candidates have similar minor axis lengths. By valid, we mean candidates for which the minor and major axis lengths fall within the prescribed bounds. A full description of the algorithm can be found in reference [1]_. References ---------- .. [1] Xie, Yonghong, and Qiang Ji. "A new efficient ellipse detection method." Pattern Recognition, 2002. Proceedings. 16th International Conference on. Vol. 2. IEEE, 2002 .. GENERATED FROM PYTHON SOURCE LINES 96-141 .. code-block:: Python import matplotlib.pyplot as plt from skimage import data, color, img_as_ubyte from skimage.feature import canny from skimage.transform import hough_ellipse from skimage.draw import ellipse_perimeter # Load picture, convert to grayscale and detect edges image_rgb = data.coffee()[0:220, 160:420] image_gray = color.rgb2gray(image_rgb) edges = canny(image_gray, sigma=2.0, low_threshold=0.55, high_threshold=0.8) # Perform a Hough Transform # The accuracy corresponds to the bin size of the histogram for minor axis lengths. # A higher `accuracy` value will lead to more ellipses being found, at the # cost of a lower precision on the minor axis length estimation. # A higher `threshold` will lead to less ellipses being found, filtering out those # with fewer edge points (as found above by the Canny detector) on their perimeter. result = hough_ellipse(edges, accuracy=20, threshold=250, min_size=100, max_size=120) result.sort(order='accumulator') # Estimated parameters for the ellipse best = list(result[-1]) yc, xc, a, b = (int(round(x)) for x in best[1:5]) orientation = best[5] # Draw the ellipse on the original image cy, cx = ellipse_perimeter(yc, xc, a, b, orientation) image_rgb[cy, cx] = (0, 0, 255) # Draw the edge (white) and the resulting ellipse (red) edges = color.gray2rgb(img_as_ubyte(edges)) edges[cy, cx] = (250, 0, 0) fig2, (ax1, ax2) = plt.subplots( ncols=2, nrows=1, figsize=(8, 4), sharex=True, sharey=True ) ax1.set_title('Original picture') ax1.imshow(image_rgb) ax2.set_title('Edge (white) and result (red)') ax2.imshow(edges) plt.show() .. image-sg:: /auto_examples/edges/images/sphx_glr_plot_circular_elliptical_hough_transform_002.png :alt: Original picture, Edge (white) and result (red) :srcset: /auto_examples/edges/images/sphx_glr_plot_circular_elliptical_hough_transform_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.411 seconds) .. _sphx_glr_download_auto_examples_edges_plot_circular_elliptical_hough_transform.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-image/scikit-image/v0.23.2?filepath=notebooks/auto_examples/edges/plot_circular_elliptical_hough_transform.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_circular_elliptical_hough_transform.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_circular_elliptical_hough_transform.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_