Image Processing: Morphological Operations with Python
Original Source Here
Image Processing: Morphological Operations with Python
To remove noises from the images
Morphological Methods
When images are pre-processed for enhancement and performance operations like threshold, then the image has a chance to get some noise. As a result, improper balance in the pixel information exists in the image.
The operation of morphological is to remove the noise that mainly affects the shape and information of images. Morphological operations are very useful in image segmentation to get the noiseless binary image.
The basic morphological operations are erosion and dilation. The explanation of these two operations is discussed below:
Dilation
In the dilation operation if the object is white then the pixel around the white pixel grows. The area to which it increases depends on the shape of the object’s pixels. The dilation process increases the number of pixels of the object and decreases the number of pixels of non-object.
Python code for Dilation with different kernel sizes and iterations
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('thumb.png') #reads the image#cv2.imwrite('Input_image.jpg',image)#Resizing the image
scale_percent = 70
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize the input image
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)kernel = np.ones((1,1), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 1)
cv2.imwrite('dilation.jpg', dilation)kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 1)
cv2.imwrite('dilation.jpg', dilation)kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 3)
cv2.imwrite('dilation.jpg', dilation)kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 5)
cv2.imwrite('dilation.jpg', dilation)kernel = np.ones((3,3), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 2)
cv2.imwrite('dilation.jpg', dilation)
Erosion
The erosion function is just the reverse of the dilation working function. The erosion function makes the object small in size. The erosion process increases the non-object of pixels and decreases the object pixels.
Python code for Erosion with different kernel sizes and iterations
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('thumb.png')#cv2.imwrite('Input_image.jpg',image)#Resizing the image
scale_percent = 70
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize the input image
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)kernel = np.ones((1,1), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 1)
cv2.imwrite('erosion.jpg', erosion)kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 2)
cv2.imwrite('erosion.jpg', erosion)kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 3)
cv2.imwrite('erosion.jpg', erosion)kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 5)
cv2.imwrite('erosion.jpg', erosion)kernel = np.ones((5,5), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 2)
cv2.imwrite('erosion.jpg', erosion)
Opening
This method is useful in removing noise from the image. The working function of this method is doing erosion and then dilation to keep the originality of the object pixel and removing the small noise from the background.
Python code for Opening
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('11.png')kernel = np.ones((5,5), dtype = "uint8")/9
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imwrite('opening.jpg', opening)
Closing
This method is useful in removing noise from the image. The working function of this method is doing dilation and then erosion to keep the originality of the object pixel and removing the small noise inside the thumb.
Python code for Closing
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('thumb.png')kernel = np.ones((9,9), dtype = "uint8")/9
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('closing.jpg', closing)
Morphological Gradient
This method is a difference of dilation technique and erosion technique.
Python code for Morphological Gradient
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('g1.png')kernel = np.ones((6,6), dtype = "uint8")/9
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imwrite('gradient.jpg', gradient)
Conclusion
These operations are a very simple method to play with binary images and a part of pre-processing in image processing applications.
I hope you like the article. Reach me on my LinkedIn and twitter.
Recommended Articles
1. NLP — Zero to Hero with Python
2. NumPy: Linear Algebra on Images
3. Exception Handling Concepts in Python
4. Principal Component Analysis in Dimensionality Reduction with Python
5. Fully Explained K-means Clustering with Python
6. Fully Explained Linear Regression with Python
7. Fully Explained Logistic Regression with Python
8. Differences Between concat(), merge() and join() with Python
9. Data Wrangling With Python — Part 1
10. Confusion Matrix in Machine Learning
AI/ML
Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot
via WordPress https://ramseyelbasheer.io/2021/07/28/image-processing-morphological-operations-with-python/