Image Processing in Python with OpenCV — Morphological Operations
Let’s learn morphological image processing techniques and examine how to use them with examples.
Import the Libraries
Before the start, we need to import OpenCV, NumPy, and MatplotLib libraries to do some operations.
import cv2
import numpy as np
import matplotlib.pyplot as plt
1 — Erosion
This method erodes pixels to clean noise. All of these operations perform as mathematical. Let’s see how it happens.
Code in Python
# Read image
img = plt.imread("alphabet.jpeg")
# Set kernel matrix for operations
kernel = np.ones((5,5), dtype = np.uint8)
# Erosion: allows to erode the boundaries of the object in front.
erosion = cv2.erode(img, kernel, iterations = 3)
plt.figure()
plt.title("Erosion")
plt.axis("OFF")
plt.imshow(erosion)
As the number of iterations increases, erosion also increases. As a result white area decreases.
2 — Dilation
In short, dilation is the opposite of erosion. It’s useful to merge broken parts in the image.
Code in Python
# Read image
img = plt.imread("alphabet.jpeg")
# Set kernel matrix for operations
kernel = np.ones((5,5), dtype = np.uint8)
# Dilation: opposite of erosion. Increases the white area in the image.
dilate = cv2.dilate(img, kernel, iterations = 3)
plt.figure()
plt.title("Dilate")
plt.axis("OFF")
plt.imshow(dilate)
As the number of iterations increases, dilation and white areas also increase.
3 — Opening
It is applying erosion first and then dilation. We’ll use another image to figure out opening and closing methods.
Code in Python
# Import noised image to fix with Opening
img_noise = plt.imread("car.png")
img_noise = cv2.cvtColor(img_noise, cv2.COLOR_BGR2RGB)
plt.figure()
plt.title("Original")
plt.axis("OFF")
plt.imshow(img_noise)
# Opening = Erosion + Dilation; It's using erosion and dilation in order
# It's useful to prevent noise
opening = cv2.morphologyEx(img_noise,cv2.MORPH_OPEN,kernel)
plt.figure()
plt.title("Opening")
plt.axis("OFF")
plt.imshow(opening)
4 — Closing
It is applying dilation first and then erosion.
Code in Python
# Import noised image to fix with Opening
img_noise = plt.imread("car.png")
img_noise = cv2.cvtColor(img_noise, cv2.COLOR_BGR2RGB)
plt.figure()
plt.title("Original")
plt.axis("OFF")
plt.imshow(img_noise)
# Closing: opposite of opening operation. We can use that to fix little black dots.
closing = cv2.morphologyEx(img_noise,cv2.MORPH_CLOSE,kernel)
plt.figure()
plt.title("Closing")
plt.axis("OFF")
plt.imshow(closing)
5 — Gradient
Gradient operation is the difference between dilation and erosion operations. It is useful to determine the borders of an object.
6 — Sobel X and Sobel Y
Sobel X and Sobel Y operations use to find borders located on axis-x and axis-y.
Code in Python
sudoku = cv2.imread("sudoku.png", 0)
# Sobel X Operation: it's method for finding x borders of image
sobelx = cv2.Sobel(sudoku, ddepth = cv2.CV_16S, dx = 1, dy = 0, ksize = 5)
plt.figure()
plt.title("Sobel X")
plt.axis("OFF")
plt.imshow(sobelx, cmap="gray")
# Sobel Y Operation: it's method for finding y borders of image
sobely = cv2.Sobel(sudoku, ddepth = cv2.CV_16S, dx = 0, dy = 1, ksize = 5)
plt.figure()
plt.title("Sobel Y")
plt.axis("OFF")
plt.imshow(sobely, cmap="gray")
7 — Laplacian
It uses to find all of the borders on an image.
Code in Python
# Laplacian Gradian: It's another method for finding borders
laplacian = cv2.Laplacian(sudoku, ddepth = cv2.CV_16S)
plt.figure()
plt.title("Laplacian")
plt.axis("OFF")
plt.imshow(laplacian, cmap="gray")
Therefore we have explained the methods basically. If you want to access documents and code examples you can click here.