Apply Opening Operation to an Image using OpenCV

Apply Opening Operation to an Image using OpenCV

Opening is a morphological image processing operation when erosion operation is applied to an image and then dilation operation. Opening operation allows removing small objects while preserving the size of larger objects.

The morphologyEx function with MORPH_OPEN parameter can be used to apply the opening operation to an image.

Python
C++
Java
import cv2 import numpy as np inputImg = cv2.imread('test.jpg') inputImg = cv2.cvtColor(inputImg, cv2.COLOR_BGR2GRAY) _, inputImg = cv2.threshold(inputImg, 0, 255, cv2.THRESH_OTSU) kernel = np.ones((4, 4)) outputImg = cv2.morphologyEx(inputImg, cv2.MORPH_OPEN, kernel) cv2.imshow('Input image', inputImg) cv2.imshow('Output image', outputImg) cv2.waitKey(0) cv2.destroyAllWindows()
Opening operation applied to an image using OpenCV

Leave a Comment

Cancel reply

Your email address will not be published.