Mahotas – Hit & Miss transform
Last Updated :
10 Jul, 2020
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.hitmiss
method
Syntax : mahotas.hitmiss(img, template)
Argument : It takes two numpy ndarray as argument
Return : It returns ndarray
Below is the implementation
import mahotas as mh
import numpy as np
from pylab import imshow, show
regions = np.zeros(( 10 , 10 ), bool )
regions[ 1 , : 2 ] = 1
regions[ 5 : 8 , 6 : 8 ] = 1
regions[ 8 , 0 ] = 1
print ( "Image" )
imshow(regions, interpolation = 'nearest' )
show()
template = np.array([
[ 0 , 1 , 1 ],
[ 0 , 1 , 1 ],
[ 0 , 1 , 1 ]])
img = mahotas.hitmiss(regions, template)
print ( "Image after hit miss transform" )
imshow(img)
show()
|
Output :
Image
Image after hit miss transform
Another example
import mahotas as mh
import numpy as np
from pylab import imshow, show
regions = np.zeros(( 10 , 10 ), bool )
regions[ 2 : 3 , : 3 ] = 1
regions[ 7 :, 7 :] = 1
print ( "Image" )
imshow(regions, interpolation = 'nearest' )
show()
template = np.array([
[ 0 , 1 , 1 ],
[ 0 , 1 , 1 ],
[ 0 , 1 , 1 ]])
img = mahotas.hitmiss(regions, template)
print ( "Image after hit miss transform" )
imshow(img)
show()
|
Output :
Image
Image after hit miss transform
Share your thoughts in the comments
Please Login to comment...