<jupyter_start><jupyter_text>Circular and Elliptical Hough TransformsThe Hough transform in its simplest form is a `method to detectstraight lines `__but it can also be used to detect circles or ellipses.The algorithm assumes that the edge is detected and it is robust againstnoise or missing points. Circle detectionIn the following example, the Hough transform is used to detectcoin positions and match their edges. We provide a range ofplausible radii. For each radius, two circles are extracted andwe finally keep the five most prominent candidates.The result shows that coin positions are well-detected. Algorithm overviewGiven a black circle on a white background, we first guess itsradius (or a range of radii) to construct a new circle.This circle is applied on each black pixel of the original pictureand the coordinates of this circle are voting in an accumulator.From this geometrical construction, the original circle centerposition receives the highest score.Note that the accumulator size is built to be larger than theoriginal picture in order to detect centers outside the frame.Its size is extended by two times the larger radius.<jupyter_code>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()<jupyter_output><empty_output><jupyter_text>Ellipse detectionIn 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 problemto solve is much more difficult because five parameters have to bedetermined, instead of three for circles. Algorithm overviewThe algorithm takes two different points belonging to the ellipse. Itassumes that it is the main axis. A loop on all the other points determineshow much an ellipse passes to them. A good match corresponds to highaccumulator values.A full description of the algorithm can be found in reference [1]_. References.. [1] <NAME>, and <NAME>. "A new efficient       ellipse detection method." Pattern Recognition, 2002. Proceedings.       16th International Conference on. Vol. 2. IEEE, 2002<jupyter_code>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 a major axis.
# The value is chosen in order to get a single high accumulator.
# The threshold eliminates low accumulators
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()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07433035714285714, 'fraction_numerical': 0.023214285714285715, 'mean_word_length': 4.47799511002445, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '579419', 'license': 'MIT', 'n_tokens_mistral': 1416, 'n_tokens_neox': 1311, 'n_words': 535, 'path': 'digital-image-processing/notebooks/edges/plot_circular_elliptical_hough_transform.ipynb', 'repo_name': 'sinamedialab/courses', 'size': '4489'}