Mahotas – Hit & Miss transform
In this article we will see how we can perform hit and miss transform in mahotas. In mathematical morphology, hit-or-miss transform is an operation that detects a given configuration in a binary image, using the morphological erosion operator and a pair of disjoint structuring elements.
In order to do this we will use
mahotas.hitmissmethodSyntax : mahotas.hitmiss(img, template)
Argument : It takes two numpy ndarray as argument
Return : It returns ndarray
Below is the implementation
# importing required librariesimport mahotas as mhimport numpy as npfrom pylab import imshow, show # creating region# numpy.ndarrayregions = np.zeros((10, 10), bool) # setting 1 value to the regionregions[1, :2] = 1regions[5:8, 6: 8] = 1regions[8, 0] = 1 # showing the image with interpolation = 'nearest'print("Image")imshow(regions, interpolation ='nearest')show() # template for hit misstemplate = np.array([ [0, 1, 1], [0, 1, 1], [0, 1, 1]]) # hit miss transformimg = mahotas.hitmiss(regions, template) # showing imageprint("Image after hit miss transform")imshow(img)show() |
Output :
Image

Image after hit miss transform

Another example
# importing required librariesimport mahotas as mhimport numpy as npfrom pylab import imshow, show # creating region# numpy.ndarrayregions = np.zeros((10, 10), bool) # setting 1 value to the regionregions[2:3, :3] = 1regions[7:, 7:] = 1 # showing the image with interpolation = 'nearest'print("Image")imshow(regions, interpolation ='nearest')show() # template for hit misstemplate = np.array([ [0, 1, 1], [0, 1, 1], [0, 1, 1]]) # hit miss transformimg = mahotas.hitmiss(regions, template) # showing imageprint("Image after hit miss transform")imshow(img)show() |
Output :
Image

Image after hit miss transform






Please Login to comment...