Mahotas - Image Ellipse Axes



An ellipse is a geometric shape which is defined as a curve in a plane surrounding two focal points, such that the sum of the distances to the two focal points is constant for every point on the curve.

The major axis of an ellipse refers to the longest diameter, which passes through the two farthest points on the ellipse. The minor axis, on the other hand, is the shortest diameter and is perpendicular to the major axis, intersecting it at the center of the ellipse.

These axes provide information about the size, orientation, and aspect ratio of the object or region.

Image Ellipse Axes in Mahotas

When dealing with ellipses in Mahotas, the axes are specified as a tuple of two values− the lengths of the semi−major and semi−minor axes.

Mahotas provides the mahotas.ellipse_axes() function to easily detect ellipses in images and obtain their major and minor axes lengths.

Using the ellipse.axes() Function

The ellipse_axes() function in Mahotas is used to detect ellipse within images. This function accepts a binary image as input and returns the lengths of the major and minor axes.

Syntax

Following is the basic syntax to find the ellipse axes of an image in Mahotas −

mahotas.features.ellipse_axes(bwimage)

where, 'bwimage' is the single channel array of image, interpreted as Boolean.

Example

In the following example, we will learn how to find image ellipse axes in mahotas −

import mahotas as mh
import numpy as np
image=mh.imread('nature.jpeg', as_grey = True)
smajor,sminor = mh.features.ellipse_axes(image)
print(smajor,sminor)
Output

Following is the output of the above code −

739.0056545212358 336.5943563176811

Fitting an Ellipse to a Set of Points

We can also fit an ellipse to a specific points of interest by generating random points within the image. The random points are generated in a uniform distribution between 0 and 1 using the np.random.rand() function from the NumPy library. Each random number represents a coordinate value on a particular axis.

To ensure that the generated points fall within the image boundaries −

  • we multiply the randomly generated values by the shape of the image.
  • The shape of the image represents the dimensions of the image as a tuple (height, width).
  • By multiplying the randomly generated values by the image's shape, we effectively scale the points to match the dimensions of the image.

The resulting points are represented as (x, y) coordinates, where x represents the column index and y represents the row index of the image.

Example

In here, we are trying to fit an ellipse to a set of given points −

import numpy as np
import mahotas as mh
image = mh.imread('tree.tiff', as_grey=True)
# Generating a set of random points
np.random.seed(0)
points = np.random.rand(87, 2) * image.shape[:2]
# Fitting an ellipse to the set of points
# calculating the axes
major_axis, minor_axis = mh.features.ellipse_axes(points)
print(major_axis, minor_axis)

Output

Output of the above code is as follows −

50.226155204899634 1.0

Fitting an Ellipse to a Grayscale Image Using ROI

We can fit an ellipse to a grayscale image using Region Of Interest (ROI) by generating random points of interest. Then, we need to create a binary image, where the points are set to white (pixel value 255), and the background is black (pixel value 0).

We can accomplish this by −

  • Initializing an array of zeros with the same shape as the original image.
  • Then, setting the indices of the points of interest within the array to 255, effectively marking those locations as points of interest.
  • This binary image allows us to isolate and focus on the specific points we want to analyze when fitting the ellipse, enabling us to estimate the estimate the ellipse's parameters accurately based on the chosen points.

Example

Here, we are fitting an ellipse to a grayscale image using Region Of Interest −

import numpy as np
import mahotas as mh
image = mh.imread('sun.png', as_grey=True)
# Generating a binary image with points of interest
np.random.seed(0)
points = np.random.rand(100, 2) * image.shape[:2]
points = points.astype(int)
binary_image = np.zeros(image.shape, dtype=np.uint8)
binary_image[points[:, 0], points[:, 1]] = 255
# Fitting an ellipse to the points of interest
major_axis, minor_axis = mh.features.ellipse_axes(binary_image)
print(major_axis, minor_axis)

Output

While executing the above code, we get the following output −

722.1261184969184 479.52790970346524
Advertisements