Mahotas – Hit & Miss 转换
在本文中,我们将了解如何在 mahotas 中执行命中和未命中转换。在数学形态学中,命中或未命中变换是一种使用形态侵蚀运算符和一对不相交的结构元素检测二值图像中给定配置的操作。
In order to do this we will use mahotas.hitmiss
method
Syntax : mahotas.hitmiss(img, template)
Argument : It takes two numpy ndarray as argument
Return : It returns ndarray
下面是实现
# 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[1, :2] = 1
regions[5:8, 6: 8] = 1
regions[8, 0] = 1
# showing the image with interpolation = 'nearest'
print("Image")
imshow(regions, interpolation ='nearest')
show()
# template for hit miss
template = np.array([
[0, 1, 1],
[0, 1, 1],
[0, 1, 1]])
# hit miss transform
img = mahotas.hitmiss(regions, template)
# showing image
print("Image after hit miss transform")
imshow(img)
show()
输出 :
Image
Image after hit miss transform
另一个例子
# 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[2:3, :3] = 1
regions[7:, 7:] = 1
# showing the image with interpolation = 'nearest'
print("Image")
imshow(regions, interpolation ='nearest')
show()
# template for hit miss
template = np.array([
[0, 1, 1],
[0, 1, 1],
[0, 1, 1]])
# hit miss transform
img = mahotas.hitmiss(regions, template)
# showing image
print("Image after hit miss transform")
imshow(img)
show()
输出 :
Image
Image after hit miss transform