![Mahotas](/mahotas/images/mahotas-mini-logo.jpg)
- Mahotas Tutorial
- Mahotas - Home
- Mahotas - Introduction
- Mahotas - Computer Vision
- Mahotas - History
- Mahotas - Features
- Mahotas - Installation
- Mahotas Handling Images
- Mahotas - Handling Images
- Mahotas - Loading an Image
- Mahotas - Loading Image as Grey
- Mahotas - Displaying an Image
- Mahotas - Displaying Shape of an Image
- Mahotas - Saving an Image
- Mahotas - Centre of Mass of an Image
- Mahotas - Convolution of Image
- Mahotas - Creating RGB Image
- Mahotas - Euler Number of an Image
- Mahotas - Fraction of Zeros in an Image
- Mahotas - Getting Image Moments
- Mahotas - Local Maxima in an Image
- Mahotas - Image Ellipse Axes
- Mahotas - Image Stretch RGB
- Mahotas Color-Space Conversion
- Mahotas - Color-Space Conversion
- Mahotas - RGB to Gray Conversion
- Mahotas - RGB to LAB Conversion
- Mahotas - RGB to Sepia
- Mahotas - RGB to XYZ Conversion
- Mahotas - XYZ to LAB Conversion
- Mahotas - XYZ to RGB Conversion
- Mahotas - Increase Gamma Correction
- Mahotas - Stretching Gamma Correction
- Mahotas Labeled Image Functions
- Mahotas - Labeled Image Functions
- Mahotas - Labeling Images
- Mahotas - Filtering Regions
- Mahotas - Border Pixels
- Mahotas - Morphological Operations
- Mahotas - Morphological Operators
- Mahotas - Finding Image Mean
- Mahotas - Cropping an Image
- Mahotas - Eccentricity of an Image
- Mahotas - Overlaying Image
- Mahotas - Roundness of Image
- Mahotas - Resizing an Image
- Mahotas - Histogram of Image
- Mahotas - Dilating an Image
- Mahotas - Eroding Image
- Mahotas - Watershed
- Mahotas - Opening Process on Image
- Mahotas - Closing Process on Image
- Mahotas - Closing Holes in an Image
- Mahotas - Conditional Dilating Image
- Mahotas - Conditional Eroding Image
- Mahotas - Conditional Watershed of Image
- Mahotas - Local Minima in Image
- Mahotas - Regional Maxima of Image
- Mahotas - Regional Minima of Image
- Mahotas - Advanced Concepts
- Mahotas - Image Thresholding
- Mahotas - Setting Threshold
- Mahotas - Soft Threshold
- Mahotas - Bernsen Local Thresholding
- Mahotas - Wavelet Transforms
- Making Image Wavelet Center
- Mahotas - Distance Transform
- Mahotas - Polygon Utilities
- Mahotas - Local Binary Patterns
- Threshold Adjacency Statistics
- Mahotas - Haralic Features
- Weight of Labeled Region
- Mahotas - Zernike Features
- Mahotas - Zernike Moments
- Mahotas - Rank Filter
- Mahotas - 2D Laplacian Filter
- Mahotas - Majority Filter
- Mahotas - Mean Filter
- Mahotas - Median Filter
- Mahotas - Otsu's Method
- Mahotas - Gaussian Filtering
- Mahotas - Hit & Miss Transform
- Mahotas - Labeled Max Array
- Mahotas - Mean Value of Image
- Mahotas - SURF Dense Points
- Mahotas - SURF Integral
- Mahotas - Haar Transform
- Highlighting Image Maxima
- Computing Linear Binary Patterns
- Getting Border of Labels
- Reversing Haar Transform
- Riddler-Calvard Method
- Sizes of Labelled Region
- Mahotas - Template Matching
- Speeded-Up Robust Features
- Removing Bordered Labelled
- Mahotas - Daubechies Wavelet
- Mahotas - Sobel Edge Detection
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