216. Morphology Methods For Preprocessing

Opening Morphology

  1. Erode Image
  2. Dilate Back Image

Code:

import cv2
import numpy as np

img = cv2.imread("img_path")
kernel = np.ones((5,5),np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

Closing Morphology

  1. Dilate Image
  2. Erode Back Image

Code:

import cv2
import numpy as np

img = cv2.imread("img_path")
kernel = np.ones((5,5),np.uint8)
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

Reference:Morphology Methods