Mahotas 简明教程
Mahotas - Distance Transform
距离变换是一种计算每个像素与其最近背景像素之间距离的技术。距离变换采用距离度量,确定如何计算空间中两点之间的距离。
Distance transformation is a technique that calculates the distance between each pixel and the nearest background pixel. Distance transformation works on distance metrics, which define how the distance between two points is calculated in a space.
在图像上应用距离变换会创建距离图。在距离图中,深色阴影分配给边界附近的像素,表示较短的距离,浅色阴影分配给较远处的像素,表示到最近背景像素的较大距离。
Applying distance transformation on an image creates the distance map. In the distance map, dark shades are assigned to the pixels near the boundary, indicating a short distance, and lighter shades are assigned to pixels further away, indicating a larger distance to the nearest background pixel.
Distance Transform in Mahotas
在 Mahotas 中,我们可以使用 mahotas.distance() 函数对图像执行距离变换。它采用迭代方法来创建一个距离图。
In Mahotas, we can use the mahotas.distance() function to perform distance transformation on an image. It uses an iterative approach to create a distance map.
该函数首先初始化图像中所有像素的距离值。将距离值无穷大分配给背景像素,将距离值零分配给前景像素。
The function first initializes the distance values for all pixels in the image. The background pixels are assigned a distance value of infinity, while the foreground pixels are assigned a distance value of zero.
然后,该函数根据相邻像素的距离更新每个背景像素的距离值。这会一直持续到计算出所有背景像素的所有距离值为止。
Then, the function updates the distance value of each background pixel based on the distances of its neighboring pixels. This occurs until all the distance value of all the background pixels has been computed.
The mahotas.distance() function
mahotas.distance() 函数以图像作为输入,并以距离图作为输出进行返回。距离图是一幅图像,其中包含输入图像中每个像素与最近背景像素之间的距离。
The mahotas.distance() function takes an image as input and returns a distance map as output. The distance map is an image that contains the distance between each pixel in the input image and the nearest background pixel.
以下是 Mahotas 中 distance() 函数的基本语法-
Following is the basic syntax of the distance() function in mahotas −
mahotas.distance(bw, metric='euclidean2')
其中,
Where,
-
bw − It is the input image.
-
metric (optional) − It specifies the type of distance used to determine the distance between a pixel and a background pixel (default is euclidean2).
在以下示例中,我们正在使用 mh.distance() 函数对图像执行距离变换。
In the following example, we are performing distance transformation on an image using the mh.distance() function.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Finding distance map
distance_map = mh.distance(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the distance transformed image
axes[1].imshow(distance_map)
axes[1].set_title('Distance Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
以下是上面代码的输出: -
Following is the output of the above code −
Using Labeled Image
我们也可以使用标签图像执行距离变换。标签图像是指将不同的标签分配给不同区域以将图像分割成不同区域的图像。
We can also perform distance transformation using a labeled image. A labeled image refers to an image where distinct regions are assigned unique labels for segmenting the image into different regions.
在 Mahotas 中,我们可以通过首先使用 mh.gaussian_filter() 函数降低其噪音,对输入图像应用距离变换。然后,我们使用 mh.label() 函数将前景区域与背景区域分离开来。
In mahotas, we can apply distance transformation on an input image by first reducing its noise using the mh.gaussian_filter() function. Then, we use the mh.label() function to separate the foreground regions from the background regions.
然后,我们可以使用 mh.distance() 函数创建距离图。这会计算前景区域像素与背景区域像素之间的距离。
We can then create a distance map using the mh.distance() function. This will calculate the distance between the pixels of the foreground regions and the pixels of the background region.
Example
在下面提到的示例中,我们正在查找经过滤的标签图像的距离图。
In the example mentioned below, we are finding the distance map of a filtered labeled image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
gauss_image = mh.gaussian_filter(image, 3)
gauss_image = (gauss_image > gauss_image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.label(gauss_image)
# Finding distance map
distance_map = mh.distance(labeled)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the distance transformed image
axes[1].imshow(distance_map)
axes[1].set_title('Distance Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
上述代码的输出如下:
Output of the above code is as follows
Using Euclidean Distance
在图像上执行距离变换的另一种方法是使用欧几里得距离。欧几里得距离是坐标系中两点之间的直线距离。它是通过计算坐标之间的平方差之和的平方根来计算的。
Another way of performing distance transformation on an image is using euclidean distance. Euclidean distance is the straight-line distance between two points in a coordinate system. It is calculated as the square root of the sum of squared differences between the coordinates.
例如,假设有两个点 A 和 B,坐标值分别为 (2, 3) 和(5, 7)。那么,x 坐标和 y 坐标的平方差将为 (5−2)2 = 9,(7−3)2 = 16。平方的和将为 9 + 16 = 25,其平方根将为 5,这是点 A 和点 B 之间的欧几里得距离。
For example, let’s say there are two points A and B with coordinate values of (2, 3) and (5, 7) respectively. Then the squared difference of x and y coordinate will be (5−2)2 = 9 and (7−3)2 = 16. Sum of the square will be 9 + 16 = 25 and square root of this will be 5, which is the euclidean distance between point A and point B.
在 Mahotas 中,我们可以使用 euclidean 距离而不是默认的 euclidean2 作为距离度量。为此,我们将值“euclidean”传递给 metric 参数。
In mahotas, we can use euclidean distance instead of the default euclidean2 as the distance metric. To do this, we pass the value 'euclidean' to the metric parameter.
Note − 由于度量参数的数据类型是字符串,因此应将欧几里得写为“euclidean”(用单引号引起来)。
Note − The euclidean should be written as 'euclidean' (in single quotes), since the data type of metric parameter is string.
Example
在此示例中,我们对图像的距离变换使用欧几里得距离类型。
In this example, we are using euclidean distance type for distance transformation of an image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Applying gaussian filtering
gauss_image = mh.gaussian_filter(image, 3)
gauss_image = (gauss_image > gauss_image.mean())
# Finding distance map
distance_map = mh.distance(gauss_image, metric='euclidean')
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the distance transformed image
axes[1].imshow(distance_map)
axes[1].set_title('Distance Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −