How to Erode(Erosion) Image using OpenCV Python

Morphological Operations

Morphological transformations are some simple operations based on the image shape. It is normally performed on binary images.

It needs two inputs, one is our original image, the second one is called structuring element or kernel which decides the nature of the operation.

Two basic morphological operators are Erosion and Dilation. Then its variant forms like Opening, Closing, Gradient etc also comes into play. We will see them one-by-one with help of the following image:

Erosion

The basic idea of erosion is just like soil erosion only, it erodes away the boundaries of the foreground object (Always try to keep foreground in white). So what it does? The kernel slides through the image (as in 2D convolution). A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the kernel are 1, otherwise, it is eroded (made to zero).

So what happens is that all the pixels near the boundary will be discarded depending upon the size of the kernel. So the thickness or size of the foreground object decreases or simply the white region decreases in the image. It is useful for removing small white noises (as we have seen in the colorspace chapter), detach two connected objects, etc.

Dilation Tutorial: https://indianaiproduction.com/image-dilation-opencv-python/

Syntax: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
Return: Erode Image

Parameters:

.   @param src input image; the number of channels can be arbitrary, but the depth should be one of
.   CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
.   @param dst output image of the same size and type as src.
.   @param kernel structuring element used for erosion; if `element=Mat()`, a `3 x 3` rectangular
.   structuring element is used. Kernel can be created using #getStructuringElement.
.   @param anchor position of the anchor within the element; default value (-1, -1) means that the
.   anchor is at the element center.
.   @param iterations number of times erosion is applied.
.   @param borderType pixel extrapolation method, see #BorderTypes
.   @param borderValue border value in case of a constant border
.   @sa  dilate, morphologyEx, getStructuringElement

Erosion of Image Practical using OpenCV

# Import Libraries

import cv2
import numpy as np
# Read Image

img_path_road = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\road\road1.jpg"
img_path_girl_eye = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\images\girl-eye.jpg"
img_path_iaip = r"C:\Users\kashz\AI Life\AI Projects - IAIP, PTs (Web + Channel)\02 OpenCV\000 opencv tutorial\data\indian ai production name.png"

img_road = cv2.imread(img_path_road, 0)
img_iaip = cv2.imread(img_path_iaip, 0)
img_girl_eye = cv2.imread(img_path_girl_eye, 0)

img_road = cv2.resize(img_road, (600, 400))
img_girl_eye = cv2.resize(img_girl_eye, (600,400))

cv2.imshow("Image road", img_road)
cv2.imshow("Image iaip", img_iaip)
cv2.imshow("Image GIrl Eye", img_girl_eye)

cv2.waitKey(0)
cv2.destroyAllWindows()
# Create Kernel

kernel = np.ones((5,5), dtype = "uint8")
kernel

# Erode girl eye image

erode_img = cv2.erode(img_girl_eye, kernel, iterations=1)

org_img_erode_img = np.hstack((img_girl_eye, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Erode road image

erode_img = cv2.erode(img_road, kernel, iterations=1)

org_img_erode_img = np.hstack((img_road, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Erode text image

erode_img = cv2.erode(img_iaip, kernel, iterations=2)

org_img_erode_img = np.vstack((img_iaip, erode_img))

cv2.imshow("Image", org_img_erode_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

REF: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html

One Reply to “How to Erode(Erosion) Image using OpenCV Python”

Leave a Reply

Top