Mahotas 简明教程
Mahotas - Filtering Regions
过滤区域是指基于特定标准排除标签图像的特定区域。用于过滤区域的常用标准是基于其大小。通过指定大小限制,可以排除太小或太大的区域以获得干净的输出图像。
Filtering regions refers to excluding specific regions of a labeled image based on certain criteria. A commonly used criteria for filtering regions is based on their size. By specifying a size limit, regions that are either too small or too large can be excluded to get a clean output image.
用于过滤区域的另一个标准是检查区域是否有边界。通过应用这些过滤器,我们可以选择性地移除或保留图像中感兴趣的区域。
Another criterion for filtering regions is to check whether a region is bordered or not. By applying these filters, we can selectively remove or retain regions of interest in the image.
Filtering Regions in Mahotas
在 Mahotas 中,我们可以通过使用 labeled.filter_labeled() 函数转换标签图像的过滤器区域。此函数将过滤器应用于图像的选定区域,同时保持其他区域保持不变。
In Mahotas, we can convert filter regions of a labeled image by using the labeled.filter_labeled() function. This function applies filters to the selected regions of an image while leaving other regions unchanged.
Using the mahotas.labeled.filter_labeled() Function
mahotas.labeled.filter_labeled() 函数接受标签图像作为输入,并基于特定属性移除不需要的区域。它根据图像的标签识别区域。
The mahotas.labeled.filter_labeled() function takes a labeled image as input and removes unwanted regions based on certain properties. It identifies the regions based on the labels of an image.
结果图像仅包含与筛选标准相匹配的区域。
The resultant image consists of only regions that match the filter criterion.
以下是 mahotas 中 filter_labeled() 函数的基本语法−
Following is the basic syntax of the filter_labeled() function in mahotas −
mahotas.labeled.filter_labeled(labeled, remove_bordering=False, min_size=None,
max_size=None)
其中,
where,
-
labeled − It is the array.
-
remove_bordering (optional) − It defines whether to remove regions touching the border.
-
min_size (optional) − It is the minimum size of the region that needs to be kept (default is no minimum).
-
max_size (optional) − It is the maximum size of the region that needs to be kept (default is no maximum).
在以下示例中,我们正在筛选标签图像以移除边框像素。
In the following example, we are filtering a labeled image to remove border pixels.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled,
remove_bordering=True)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered 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 −
Filtering on Regions of Specific Size
我们还可以筛选图像中特定大小的区域。这样做,我们可以从标签图像中移除不在特定大小限制内的区域(太小或太大的区域)。
We can also filter regions of specific size in an image. In this way, we can remove regions from labeled images which do not fall within a specific size limit (regions that are too small or too large).
在 mahatos 中,我们可以通过在 labeled.filter_label() 函数中为可选参数 min_size 和 max_size 指定值来实现此目的。
In mahatos, we can achieve this by specifying values to the optional parameter min_size and max_size in the labeled.filter_label() function.
Example
以下示例显示筛选标签图像以移除特定大小的区域。
The following example shows filtering a labeled image to remove regions of specific size.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting to a labeled image
labeled, num_objects = mh.label(image)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled, min_size=10,
max_size=50000)
# Create a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered 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 −
Filtering on Border Regions and Regions of Specific Size
我们可以筛选图像中特定大小的区域以及边界区域。这样做,移除触及边框的区域和不在特定大小限制内的区域。
We can filter bordered regions along with regions of a specific size in an image. In this, we remove regions which touch the border and regions which do not fall within a specific size limit.
在 mahotas 中,我们可以通过在 labeled.filter_label() 函数中为可选参数 min_size 和 max_size 指定值并设置可选参数 remove_bordering 为 True 来实现此目的。
In mahotas, we can do this by specifying values to the optional parameter min_size and max_size and setting the optional parameter remove_bordering to True in the labeled.filter_label() function.
Example
在此示例中,应用了筛选器来移除标签图像的边框区域和特定大小的区域。
In this example, a filter is applied to remove border regions and regions of specific size of a labeled image.
import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image_rgb = mh.imread('tree.tiff')
image = image_rgb[:,:,0]
# Applying gaussian filtering
image = mh.gaussian_filter(image, 4)
image = (image > image.mean())
# Converting it to a labeled image
labeled, num_objects = mh.label(image)
# Applying filters
filtered_image, num_objects = mh.labeled.filter_labeled(labeled,
remove_bordering=True, min_size=1000, max_size=50000)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image_rgb)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the filtered image
axes[1].imshow(filtered_image)
axes[1].set_title('Filtered Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
下面显示了产生的输出:
The output produced is as shown below −