Last Updated : 17 Jun, 2025
Python OpenCV Morphological operations are one of the Image processing techniques that processes image based on shape. This processing strategy is usually performed on binary images. Morphological operations based on OpenCV are as follows:
For all the above techniques the two important requirements are the binary image and a kernel structuring element that is used to slide across the image.
Images used for demonstration: Images used Images used ErosionErosion is a morphological operation that shrinks the white foreground regions in a binary image (values 0 and 255). It removes noise and thins object boundaries. The extent of erosion depends on the size and shape of the kernel, typically defined using np.ones(), though custom kernels can also be used based on specific needs.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread(r"Downloads\test (2).png", 0) # Load grayscale image
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # Binarize
k = np.ones((5, 5), np.uint8) # Define 5x5 kernel
inv = cv2.bitwise_not(bin)
out = cv2.erode(inv, k, 1)
plt.imshow(out, cmap='gray'), plt.axis('off'), plt.show()
Output:
The output should be a thinner image than the original one.
ErosionExplanation:
Dilation is a morphological operation that expands the white foreground regions in a binary image (values 0 and 255). It thickens object boundaries and fills small holes or gaps. Like erosion, the effect depends on the kernel’s size and shape, usually defined using np.ones(). Dilation is the opposite of erosion it grows white areas instead of shrinking them.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread(r"Downloads\test (2).png", 0) # Load grayscale image
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # Binarize
k = np.ones((3, 3), np.uint8) # Define 3x3 kernel
inv = cv2.bitwise_not(bin)
out = cv2.dilate(inv, k, 1)
plt.imshow(out, cmap='gray'), plt.axis('off'), plt.show()
Output
The output should be a thicker image than the original one.
Dilated imageExplanation:
Opening involves erosion followed by dilation in the outer surface (the foreground) of the image. All the above-said constraints for erosion and dilation applies here. It is a blend of the two prime methods. It is generally used to remove the noise in the image.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread(r"Downloads\test (2).png", 0) # Load grayscale
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # Binarize
k = np.ones((3, 3), np.uint8) # 3x3 kernel
opened = cv2.morphologyEx(bin, cv2.MORPH_OPEN, k)
plt.imshow(opened, cmap='gray'), plt.axis('off'), plt.show()
Output
Opening ImageExplanation:
Closing involves dilation followed by erosion in the outer surface (the foreground) of the image. All the above-said constraints for erosion and dilation applies here. It is a blend of the two prime methods. It is generally used to remove the noise in the image.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread(r"Downloads\test (2).png", 0)
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
k = np.ones((3, 3), np.uint8)
closed = cv2.morphologyEx(bin, cv2.MORPH_CLOSE, k) # Apply closing
plt.imshow(closed, cmap='gray'), plt.axis('off'), plt.show()
Output
Closing ImageExplanation:
Morphological gradient is slightly different than the other operations, because, the morphological gradient first applies erosion and dilation individually on the image and then computes the difference between the eroded and dilated image. The output will be an outline of the given image.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread(r"path to your image", 0) # Load grayscale image
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # Binarize
k = np.ones((3, 3), np.uint8) # Define 3x3 kernel
inv = cv2.bitwise_not(bin)
out = cv2.morphologyEx(inv, cv2.MORPH_GRADIENT, k)
plt.imshow(out, cmap='gray'), plt.axis('off'), plt.show()
Output
Morphological gradient ImageExplanation:
Top Hat is yet another morphological operation where Opening is performed on the binary image and the output of this operation is a difference between the input image and the opened image.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread("path", 0)
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
k = np.ones((13, 13), np.uint8)
top = cv2.morphologyEx(bin, cv2.MORPH_TOPHAT, k)
plt.imshow(top, cmap='gray'), plt.axis('off'), plt.show()
Output
Top hat ImageExplanation:
The black-hat operation is used to do the opposite, enhancing dark objects of interest on a bright background. The output of this operation is the difference between the closing of the input image and the input image.
Python
import cv2, numpy as np, matplotlib.pyplot as plt
img = cv2.imread("path", 0)
bin = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
inv = cv2.bitwise_not(bin)
k = np.ones((5, 5), np.uint8)
bh = cv2.morphologyEx(inv, cv2.MORPH_BLACKHAT, k)
plt.imshow(bh, cmap='gray'), plt.axis('off'), plt.show()
Output
Black Hat imageExplanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4