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
In Mahotas, the weight of a labeled region can be calculated using the mahotas.labeled_sum() function.
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
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.
Syntax
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.
Example
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)
Output
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
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.
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
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}")
Output
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