Morphological transformation It's a simple operation based on the shape of the image .
It is usually performed on binary images . It needs two inputs , One is our original image , The other is Structural elements (structuring element) or nucleus (kernel), It determines the nature of the operation .
The two basic morphological operators are Erosion and Dilation. And then its variant form , Such as Opening, Closing, Gradient And so on .
####################################################################################################
# Morphological transformation
def lmc_cv_morphological_transformations(index):
"""
The functionality : Morphological transformation .
Morphological transformation is a simple operation based on image shape .
It is usually performed on binary images . It needs two inputs , One is our original image , The other is the structural element (structuring element) Or nuclear (kernel), It determines the nature of the operation .
The two basic morphological operators are Erosion and Dilation. And then its variant form , Such as Opening, Closing, Gradient And so on .
"""
# Read images
image = lmc_cv.imread('D:/99-Research/Python/Image/j.png')
# image = lmc_cv.cvtColor(image, lmc_cv.COLOR_RGB2BGR)
# Create core
kernel = np.ones((9, 9), np.uint8)
# Create core :Rectangular Kernel
if 0 == index:
kernel = lmc_cv.getStructuringElement(lmc_cv.MORPH_RECT, (3, 3))
# Create core :Elliptical Kernel
if 1 == index:
kernel = lmc_cv.getStructuringElement(lmc_cv.MORPH_ELLIPSE, (5, 5))
# Create core :Cross-shaped Kernel
if 2 == index:
kernel = lmc_cv.getStructuringElement(lmc_cv.MORPH_CROSS, (7, 7))
# Erosion
erosion_image = lmc_cv.erode(image, kernel, anchor=(-1, -1), iterations=1, borderType=lmc_cv.BORDER_DEFAULT)
# Dilation
dilation_image = lmc_cv.dilate(image, kernel, anchor=(-1, -1), iterations=1, borderType=lmc_cv.BORDER_DEFAULT)
# Opening
opening_image = lmc_cv.morphologyEx(image, lmc_cv.MORPH_OPEN, kernel, anchor=(-1, -1), iterations=1,
borderType=lmc_cv.BORDER_DEFAULT)
# Closing
closing_image = lmc_cv.morphologyEx(image, lmc_cv.MORPH_CLOSE, kernel, anchor=(-1, -1), iterations=1,
borderType=lmc_cv.BORDER_DEFAULT)
# Morphological Gradient
gradient_image = lmc_cv.morphologyEx(image, lmc_cv.MORPH_GRADIENT, kernel, anchor=(-1, -1), iterations=1,
borderType=lmc_cv.BORDER_DEFAULT)
# Top Hat
tophat_image = lmc_cv.morphologyEx(image, lmc_cv.MORPH_TOPHAT, kernel, anchor=(-1, -1), iterations=1,
borderType=lmc_cv.BORDER_DEFAULT)
# Black Hat
blackhat_image = lmc_cv.morphologyEx(image, lmc_cv.MORPH_BLACKHAT, kernel, anchor=(-1, -1), iterations=1,
borderType=lmc_cv.BORDER_DEFAULT)
# Display images
pyplot.figure('Image Display')
images = [image, erosion_image, dilation_image, opening_image, closing_image, gradient_image, tophat_image,
blackhat_image]
titles = ['Original', 'Erosion', 'Dilation', 'Opening', 'Closing', "Morphological Gradient", 'Top Hat', 'Black Hat']
for i in range(8):
pyplot.subplot(2, 4, i + 1)
pyplot.imshow(images[i], 'gray')
pyplot.title(titles[i])
pyplot.xticks([])
pyplot.yticks([])
pyplot.show()
# Save images based on user input
if ord("q") == (lmc_cv.waitKey(0) & 0xFF):
# Destruction of the window
pyplot.close()
return