Scikit Image - Elliptical Hough Transform



The Elliptical Hough Transform is an extension of the classic Hough Transform, which is a technique used in image processing and computer vision to detect shapes, particularly lines, ellipses, and other parametric curves, in an image. The Elliptical Hough Transform specifically designed to detect elliptical shapes within images. Which is useful for detecting elliptical patterns, such as objects or structures with elliptical boundaries.

In the scikit-image library, you can perform the Elliptical Hough Transform using the transform.hough_ellipse() function. This function calculates the Elliptical Hough Transform and produces a ndarray with fields containing information about the detected ellipses.

Using the skimage.transform.hough_ellipse() function

The skimage.transform.hough_ellipse() function performs an elliptical Hough transform on an input image.

Syntax

Following is the syntax of this function −

skimage.transform.hough_ellipse(image, threshold=4, accuracy=1, min_size=4, max_size=None)

Parameters

  • image (ndarray): This is the input image on which the elliptical Hough transform will be performed. The image is expected to have nonzero values at locations that represent edges. The shape of the image is (M, N).
  • threshold (int, optional): This parameter determines the threshold value for the accumulator.
  • accuracy (double, optional): This parameter defines the bin size on the minor axis used in the accumulator. It affects the accuracy of ellipse detection.
  • min_size (int, optional): The minimal major axis length of detected ellipses. Ellipses with major axes shorter than this value will be ignored.
  • max_size (int, optional): The maximal minor axis length of detected ellipses. If None, the value is set to half of the smaller image dimension.

Return Value

It returns a structured array with fields containing information about the detected ellipses.

Example

The following example demonstrates how to use the transform.hough_ellipse() function to detect a ellipse in an image.

import numpy as np
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter

# Create a blank image with an ellipse
image = np.zeros((200, 200), dtype=np.uint8)

# Draw an ellipse in the image
rr, cc = ellipse_perimeter(10, 10, 6, 8)
image[cc, rr] = 1

# Perform elliptical Hough transform
result = hough_ellipse(image, threshold=8)

# Print the detected ellipse parameters
for data in result:
    print("Detected Ellipse:")
    print("Center (yc, xc):", data['yc'], data['xc'])
    print("Major Axis (a):", data['a'])
    print("Minor Axis (b):", data['b'])
    print("Orientation:", data['orientation'])

Output

On executing the above program, you will get the following output −

Detected Ellipse:
Center (yc, xc): 10.0 10.0
Major Axis (a): 8.0
Minor Axis (b): 6.0
Orientation: 0.0

Example

The following example demonstrates how to use the transform.hough_ellipse() function to detect an ellipse in an image and visualize the detected ellipses using the ellipse_perimeter() function.

import numpy as np
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter
from skimage.io import imread
from skimage import color
import matplotlib.pyplot as plt

# Create a blank image with an ellipse
image = np.zeros((200, 200, 3), dtype=np.double)

# Draw an ellipse in the image
rr, cc = ellipse_perimeter(100, 100, 60, 40)
image[cc, rr, ] = 1

image_gray = color.rgb2gray(image)

# Perform elliptical Hough transform
result = hough_ellipse(image_gray)
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]

# Plot the original image and the detected circle
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')

# Draw the ellipse on the original image
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation)
image[cy, cx, :] = (1, 0, 0)

axes[1].imshow(image, cmap='gray')
axes[1].set_title('Detected Ellipses')
plt.show()

Output

On executing the above program, you will get the following output −

Advertisements