Morphological operations: Processing based on shapes
Work on: Binary images (black/white) or grayscale
Structuring element (kernel): Defines neighborhood shape
Applications:
Erosion: Shrinks bright regions
Dilation: Expands bright regions
Formula:
min(neighborhood)
max(neighborhood)
import cv2import numpy as np# binary image_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)# create structuring element#ans: kernel = np.ones((5, 5), np.uint8)#ans: can also use cv2.getStructuringElement()# erosion#ans: eroded = cv2.erode(binary, kernel, iterations=1)#ans: iterations: number of times to apply# effect: shrinks white regions, removes small noise#ans: larger kernel or more iterations = more erosion
# dilation#ans: dilated = cv2.dilate(binary, kernel, iterations=1)# effect: expands white regions, fills small holes#ans: larger kernel or more iterations = more dilation# multiple iterationsdilated = cv2.dilate(binary, kernel, iterations=3)#ans: equivalent to applying 3 times
Opening: Erosion followed by dilation
Effect:
Use case: Noise removal without shrinking objects
kernel = np.ones((5, 5), np.uint8)# opening = erosion + dilation#ans: opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)# equivalent to:#ans: eroded = cv2.erode(binary, kernel)#ans: opening = cv2.dilate(eroded, kernel)# removes small white noise, preserves large shapes
Closing: Dilation followed by erosion
Use case: Fill holes in objects, connect broken parts
# closing = dilation + erosion#ans: closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)# equivalent to:#ans: dilated = cv2.dilate(binary, kernel)#ans: closing = cv2.erode(dilated, kernel)# fills small black holes in white regions
Gradient: Difference between dilation and erosion
Effect: Outlines edges of objects
Formula: dilation(img) - erosion(img)
dilation(img) - erosion(img)
# morphological gradient#ans: gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel)# equivalent to:#ans: dilated = cv2.dilate(binary, kernel)#ans: eroded = cv2.erode(binary, kernel)#ans: gradient = dilated - eroded# highlights object boundaries
Top hat: Difference between image and opening
Effect: Extracts small bright features
Formula: img - opening(img)
img - opening(img)
# top hat#ans: tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)# extracts bright regions smaller than kernel#ans: useful for extracting bright objects on dark background
Black hat: Difference between closing and image
Effect: Extracts small dark features
Formula: closing(img) - img
closing(img) - img
# black hat#ans: blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)# extracts dark regions smaller than kernel#ans: useful for extracting dark objects on bright background
# rectangular kernel#ans: rect = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))# elliptical kernel#ans: ellipse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))# cross-shaped kernel#ans: cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))# custom kernelcustom = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]], np.uint8)#ans: cross-shaped, equivalent to MORPH_CROSS
# what is erosion?#ans: shrinks bright regions, sets pixel to neighborhood minimum# what is dilation?#ans: expands bright regions, sets pixel to neighborhood maximum# what is opening?#ans: erosion then dilation, removes noise# what is closing?#ans: dilation then erosion, fills holes# what is morphological gradient?#ans: dilation minus erosion, outlines edges
# when to use opening?#ans: remove small white noise, preserve object shape# when to use closing?#ans: fill small holes, connect broken parts# what is top hat?#ans: img - opening, extracts small bright features# what is black hat?#ans: closing - img, extracts small dark features# effect of larger kernel?#ans: more aggressive operation, affects larger structures
# erosion with 3x3 kernelkernel = np.ones((3, 3), np.uint8)#ans: eroded = cv2.erode(binary, kernel, iterations=1)# dilation with 5x5 kernelkernel = np.ones((5, 5), np.uint8)#ans: dilated = cv2.dilate(binary, kernel, iterations=1)# openingkernel = np.ones((5, 5), np.uint8)#ans: opening = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
# closingkernel = np.ones((5, 5), np.uint8)#ans: closing = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)# morphological gradient#ans: gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel)# elliptical kernel#ans: ellipse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7, 7))#ans: result = cv2.morphologyEx(binary, cv2.MORPH_OPEN, ellipse)
# top hat with 9x9 kernelkernel = np.ones((9, 9), np.uint8)#ans: tophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel)# black hat#ans: blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)# multiple erosionskernel = np.ones((3, 3), np.uint8)#ans: eroded = cv2.erode(binary, kernel, iterations=3)# cross-shaped structuring element#ans: cross = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))#ans: result = cv2.morphologyEx(binary, cv2.MORPH_OPEN, cross)
Google tag (gtag.js)