Mahotas 简明教程
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
在 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 提供 mahotas.ellipse_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
Mahotas 中的 ellipse_axes() 函数用于检测图像中的椭圆。此函数接受二进制图像作为输入,并返回长轴和短轴的长度。
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.
以下是使用 Mahotas 在图像中查找椭圆轴的基本语法 −
Following is the basic syntax to find the ellipse axes of an image in Mahotas −
mahotas.features.ellipse_axes(bwimage)
其中, 'bwimage' 是图像的单通道数组,解释为布尔值。
where, 'bwimage' is the single channel array of image, interpreted as Boolean.
在以下示例中,我们将学习如何在 mahotas 中查找图像椭圆轴 −
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)
以下是上面代码的输出: -
Following is the output of the above code −
739.0056545212358 336.5943563176811
Fitting an Ellipse to a Set of Points
我们还可以通过在图像内生成随机点将椭圆拟合到特定感兴趣点。随机点使用 NumPy 库中的 np.random.rand() 函数在 0 到 1 之间的均匀分布中生成。每个随机数表示特定轴上的坐标值。
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.
生成的点表示为 (x, y) 坐标,其中 x 表示列索引,y 表示图像的行索引。
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 of the above code is as follows −
50.226155204899634 1.0
Fitting an Ellipse to a Grayscale Image Using ROI
我们可以通过生成随机感兴趣点使用感兴趣区域 (ROI) 将椭圆拟合到灰度图像中。然后,我们需要创建一个二值图像,其中点设置为白色(像素值 255)而背景设置为黑色(像素值 0)。
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)
在执行上述代码时,我们得到以下输出——
While executing the above code, we get the following output −
722.1261184969184 479.52790970346524