📜  Mahotas – 标记区域的权重

📅  最后修改于: 2022-05-13 01:54:29.955000             🧑  作者: Mango

Mahotas – 标记区域的权重

在本文中,我们将看到如何在 mahotas 中获得标记区域的权重。标记图像是整数图像,其中值对应于不同区域。即,区域 1 是具有值 1 的所有像素,区域 2 是具有值 2 的像素,依此类推。按照惯例,区域 0 是背景,通常以不同方式处理。我们可以借助 mahotas.label 方法创建一个带标签的区域。
我们可以借助 mahotas.label_size 方法获得每个区域的大小,这个大小简单地衡量为每个区域中的像素数。我们可以改为测量每个区域的总重量。
为此,我们将使用 mahotas.label_size 方法

示例 1:

Python3
# importing required libraries
import mahotas as mh
import numpy as np
from pylab import imshow, show
 
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
 
# setting 1 value to the region
regions[:3, :3] = 1
regions[6:, 6:] = 1
 
# getting labeled function
labeled, nr_objects = mh.label(regions)
 
# showing the image with interpolation = 'nearest'
imshow(labeled, interpolation ='nearest')
show()
 
     
# random array of region shapes   
array = np.random.random_sample(regions.shape)
 
# getting sum i.e area
sums = mh.labeled_sum(array, labeled)
 
 
# printing the sums values
for i in range(len(sums)):
    print("Sum of region " + str(i) + " : " + str(sums[i]))


Python3
# importing required libraries
import mahotas as mh
import numpy as np
from pylab import imshow, show
 
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
 
# setting 1 value in the region
regions[1, 1] = 1
regions[6, 6] = 1
regions[4, 4] = 1
regions[9, 9] = 1
 
# getting labeled function
labeled, nr_objects = mh.label(regions)
 
# showing the image with interpolation = 'nearest'
imshow(labeled, interpolation ='nearest')
show()
 
     
# random array of region shapes   
array = np.random.random_sample(regions.shape)
 
# getting sum i.e area
sums = mh.labeled_sum(array, labeled)
 
 
# printing the sums values
for i in range(len(sums)):
    print("Sum of region " + str(i) + " : " + str(sums[i]))


输出 :

Sum of region 0 : 38.81707025816505
Sum of region 1 : 5.627375253802732
Sum of region 2 : 8.224633573583985

示例 2:

Python3

# importing required libraries
import mahotas as mh
import numpy as np
from pylab import imshow, show
 
# creating region
# numpy.ndarray
regions = np.zeros((10, 10), bool)
 
# setting 1 value in the region
regions[1, 1] = 1
regions[6, 6] = 1
regions[4, 4] = 1
regions[9, 9] = 1
 
# getting labeled function
labeled, nr_objects = mh.label(regions)
 
# showing the image with interpolation = 'nearest'
imshow(labeled, interpolation ='nearest')
show()
 
     
# random array of region shapes   
array = np.random.random_sample(regions.shape)
 
# getting sum i.e area
sums = mh.labeled_sum(array, labeled)
 
 
# printing the sums values
for i in range(len(sums)):
    print("Sum of region " + str(i) + " : " + str(sums[i]))

输出 :

Sum of region 0 : 47.05475409838963
Sum of region 1 : 0.07129110100184632
Sum of region 2 : 0.9333006775043519
Sum of region 3 : 0.8322767370999588
Sum of region 4 : 0.1700224211466459