Mahotas 简明教程
Mahotas - Weight of Labeled Region
在图像分析的语境中,图像可分割成若干个感兴趣区域。这些区域通常用唯一标识符或标签标记。标记区域的权重提供总和,代表该区域内所有像素的总和。
In the context of image analysis, an image can be segmented into regions of interest. These regions are often labeled with unique identifiers or labels. The weight of a labeled region provides an aggregated value that represents the combined contribution of all the pixels within that region.
换言之,图像中带标签的区域的权重是指该区域内像素强度的总和。
In other words, the weight of a labeled region in an image refers to the sum of pixel intensities within that region.
要计算权重,我们对标记区域内的像素强度求和。像素强度是指与 图像中每个像素相关的值,可以表示亮度或颜色之类的属性。
To calculate the weight, we sum up the pixel intensities within the labeled region. The pixel intensity refers to the value associated with each pixel in the image, which could represent properties such as brightness or color.
通过对像素强度求和,我们获得一个表示标记区域内总体“质量”或“数量”的 单一值。
By summing the pixel intensities, we obtain a single value that represents the overall "mass" or "quantity" within the labeled region.
Weight of Labeled Region in Mahotas
在 Mahotas 中,可以用 mahotas.labeled_sum() 函数计算标记区域的权重。
In Mahotas, the weight of a labeled region can be calculated using the mahotas.labeled_sum() function.
Mahotas 中带标签的区域由标记图像表示,其中每个像素分配一个唯一标签,对应其所属区域。
A labeled region in mahotas is represented by a labeled image, where each pixel is assigned a unique label that corresponds to the region it belongs to.
为了清楚了解,我们考虑一个灰度图像,其中每个像素表示标量强度值。带标签的图像 通过将此灰度图像分割成不同区域并为每个区域分配唯一标签而获得。
To understand it in a clear way, let us consider a grayscale image where each pixel represents a scalar intensity value. The labeled image is obtained by segmenting this grayscale image into different regions and assigning a unique label to each region.
The mahotas.labeled_sum() function
mahotas.labeled_sum() 函数获取标记求和函数,获取输入数组和标记数组。根据标记数组中相应标签,它计算输入数组中每个标记区域的像素值的总和。
The mahotas.labeled_sum() function takes the labeled_sum function takes an input array and a labeled array. It calculates the sum of pixel values for each labeled region in the input array based on the corresponding labels in the labeled array.
结果数组包含每个标记区域的像素值总和,数组中的每个元素对应唯一标签。
The resulting array contains the sum of pixel values for each labeled region, with each element of the array corresponding to a unique label.
以下是 mahotas
中 labeled_sum()
函数的基本语法 −
Following is the basic syntax of the labeled_sum() function in mahortas −
mahotas.labeled_sum(array, labeled, minlength=None)
其中,
Where,
-
array − A NumPy array representing the image or the data.
-
labeled − A labeled array with the same shape as array, where each region is assigned a unique integer label.
-
minlength (optional) − An integer specifying the minimum length of the resulting array. If provided, the function pads the resulting array with zeros to reach this length.
以下是一个基本的示例,用于确定图像的标记区域的权重 −
Following is the basic example to determine the weight of labeled region of an image −
import mahotas as mh
import numpy as np
from pylab import imshow, show
# Generate a grayscale image
image = mh.imread('tree.tiff')
# Generate a labeled array
labeled = mh.imread('sea.bmp')
# Calculate the sum of pixel values for each labeled region
result = mh.labeled_sum(image, labeled)
print(result)
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
[ 30 115 58 157 226 154 169 24 63 48 124 123 159 146 44 163 202 174
208 30 39 109 100 221 245 101 16 162 42 0 214 71 46 31 110 197
91 137 118 192 104 119 139 23 198 176 219 192 60 1 218 143 67 122
249 14 165 70 159 16 18 204 135 185 74 175 110 39 8 98 208 238
86 169 42 21 39 129 100 146 162 48 217 228 204 30 54 164 174 80
144 172 232 115 48 165 136 234 37 147 195 242 2 227 75 6 95 100
92 230 200 96 93 59 30 28 60 122 213 65 133 53 58 91 191 36
174 106 95 25 201 70 73 234 59 76 2 207 238 66 87 140 174 222
122 239 37 79 220 57 126 38 150 236 60 37 196 58 236 241 148 207
253 56 103 79 72 71 47 242 169 8 88 19 176 16 195 88 134 188
205 78 248 96 156 86 35 57 69 241 142 203 198 182 165 31 127 36
227 47 195 47 117 217 134 45 50 95 76 47 34 182 21 140 138 192
17 232 158 182 162 136 104 145 229 165 33 107 14 117 185 115 73 129
217 105 244 0 63 124 0 0 109 56 0 107]
Weight of Labeled Regions with a Minimum Length
为了计算最小长度的标记区域的权重,我们需要将 'minlength' 参数传递给 mahotas
中的 labeled_sum() 函数。
To calculate the weight of labeled regions with minimum length, we need to pass the 'minlength' parameter to the labeled_sum() function in mahotas.
通过设置最小长度值,只有长度等于或大于指定最小长度的区域才会在权重计算过程中被考虑。
By setting the minimum length value, only regions with a length equal to or greater than the specified minimum length will be taken into account during the weight calculation process.
此功能使我们能够过滤掉较小的区域,并关注较大且可能更重要的区域。
This feature allows us to filter out smaller regions and focus on the larger and potentially more significant regions.
如果任何区域不满足最小长度要求,则该区域的权重被指定为“0”。
If any region doesn’t meet the minimum length requirement, the weight of that region is assigned as '0'.
Example
在此,我们通过指定最小长度来计算标记区域的权重 −
Here, we are calculating the weight of labeled regions by specifying a minimum length −
import mahotas as mh
import numpy as np
image = mh.imread('tree.tiff')
# Perform labeling to obtain the labeled image
labeled_img, n_labels = mh.label(image)
# Calculate the weight of each labeled region with a minimum length of 2
weights = mh.labeled_sum(image, labeled_img, minlength=2)
# Print the weights of each labeled region
for label, weight in enumerate(weights, start=1):
print(f"Weight of region {label}: {weight}")
上述代码的输出如下:
Output of the above code is as follows −
Weight of region 1: 0
Weight of region 2: 40
Weight of region 3: 86
Weight of region 4: 37
Weight of Labeled Regions with Custom Labels
为了计算具有自定义标签的标记区域的权重,首先生成一个自定义标记图像,其中每个区域被分配一个特定标签。
To calculate the weight of labeled regions with custom labels, first, generate a custom labeled image, where each region is assigned a specific label.
标记图像将与原始图像具有相同的尺寸,每个像素根据其所属区域分配相应的标签值。然后,计算标记区域的权重。
The labeled image will have the same dimensions as the original image, with each pixel assigned the corresponding label value based on the region it belongs to. Then, calculate the weight of labeled regions.
Example
现在,我们尝试计算具有自定义标签的标记区域的权重 −
Now, we are trying to calculate the weight of labeled regions with custom labels −
import mahotas as mh
import numpy as np
# Generate a binary image
binary_img = np.array([[0, 1, 0, 0],[1, 1, 1, 0],[0, 0, 0, 1]])
# Generate a custom labeled image with specific labels
labeled_img = np.array([[0, 1, 0, 0],[2, 2, 2, 0],[0, 0, 0, 3]])
# Calculate the weight of each labeled region
weights = mh.labeled_sum(binary_img, labeled_img)
# Print the weights of each labeled region
for label, weight in enumerate(weights, start=1):
print(f"Weight of region {label}: {weight}")
以下是上面代码的输出: -
Following is the output of the above code −
Weight of region 1: 0
Weight of region 2: 1
Weight of region 3: 3
Weight of region 4: 1