Mahotas – 与二值图像的距离
在本文中,我们将了解如何在 mahotas 中获取二值图像的距离图。距离变换,也称为距离图或距离场,是数字图像的派生表示。术语的选择取决于对所讨论对象的观点:初始图像是否被转换为另一种表示,或者它只是被赋予了额外的地图或场。
为此,我们将使用 mahotas.distance 方法
Syntax : mahotas.distance(img)
Argument : It takes image object which should be binary as argument
Return : It returns image object
注意:输入图像应该是二进制图像,也可以标记,图像应该被过滤或加载为灰色以使其成为二进制
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令
image = image[:, :, 0]
示例 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'
print("Image")
imshow(labeled, interpolation ='nearest')
show()
# getting distance map
dmap = mahotas.distance(labeled)
# showing image
print("Distance Map")
imshow(dmap)
show()
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("Image")
# showing the gaussian filter
imshow(labeled)
show()
# getting distance map
dmap = mahotas.distance(labeled)
# showing image
print("Distance Map")
imshow(dmap)
show()
输出 :
示例 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("Image")
# showing the gaussian filter
imshow(labeled)
show()
# getting distance map
dmap = mahotas.distance(labeled)
# showing image
print("Distance Map")
imshow(dmap)
show()
输出 :