Mahotas – 获取标记的最大数组
在本文中,我们将看到如何获得 in mahotas 的标记最大数组。标记图像是整数图像,其中值对应于不同区域。即,区域 1 是具有值 1 的所有像素,区域 2 是具有值 2 的像素,依此类推。按照惯例,区域 0 是背景,通常以不同方式处理。我们可以借助 mahotas.label 方法创建一个带标签的区域。
我们可以借助 mahotas.label_size 方法获得每个区域的大小,这个大小简单地衡量为每个区域中的像素数。我们可以改为测量每个区域的总重量。
为此,我们将使用 mahotas.labeled.labeled_max 方法
Syntax : mahotas.labeled.labeled_max(array, labeled_img)
Argument : It takes two numpy.ndarray object as argument i.e random array of region shape and labeled region
Return : It returns ndarray of numpy.float64 values
示例 1:
Python3
# importing required libraries
import mahotas
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 = mahotas.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 labeled max array
l_array = mahotas.labeled.labeled_max(array, labeled)
print("Labeled Max array :")
# printing the values
for i in range(len(l_array)):
print("Region " + str(i) + " : " + str(l_array[i]))
Python3
# importing required libraries
import numpy as np
import mahotas
from pylab import imshow, show
# loading image
img = mahotas.imread('dog_image.png')
# filtering the image
img = img[:, :, 0]
# setting gaussian filter
gaussian = mahotas.gaussian_filter(img, 15)
# setting threshold value
gaussian = (gaussian > gaussian.mean())
# creating a labeled image
labeled, n_nucleus = mahotas.label(gaussian)
print("Labelled Image")
# showing the gaussian filter
imshow(labeled)
show()
# random array of region shapes
array = np.random.random_sample(labeled.shape)
# getting labeled max array
l_array = mahotas.labeled.labeled_max(array, labeled)
print("Labeled Max array :")
# printing the values
for i in range(len(l_array)):
print("Region " + str(i) + " : " + str(l_array[i]))
输出 :
Labeled Max array :
Region 0 : 0.9980513142322492
Region 1 : 0.624390200202312
Region 2 : 0.9210927640926101
示例 2:
Python3
# importing required libraries
import numpy as np
import mahotas
from pylab import imshow, show
# loading image
img = mahotas.imread('dog_image.png')
# filtering the image
img = img[:, :, 0]
# setting gaussian filter
gaussian = mahotas.gaussian_filter(img, 15)
# setting threshold value
gaussian = (gaussian > gaussian.mean())
# creating a labeled image
labeled, n_nucleus = mahotas.label(gaussian)
print("Labelled Image")
# showing the gaussian filter
imshow(labeled)
show()
# random array of region shapes
array = np.random.random_sample(labeled.shape)
# getting labeled max array
l_array = mahotas.labeled.labeled_max(array, labeled)
print("Labeled Max array :")
# printing the values
for i in range(len(l_array)):
print("Region " + str(i) + " : " + str(l_array[i]))
输出 :
Labeled Max array :
Region 0 : 0.9999995478951564
Region 1 : 0.9999944289534851
Region 2 : 0.999718434740755
Region 3 : 0.9996236088210476
Region 4 : 0.9861670123032187