Erosion and dilation
Erosion and dilation are morphological operations. Erosion removes pixels at the boundaries of objects in an image and dilation adds pixels to the boundaries of objects in an image.
How to do it...
- Import the Computer Vision package –
cv2:
import cv2
- Import the numerical Python package –
numpy as np:
import numpy as np
- Read the image using the built-in
imreadfunction:
image = cv2.imread('image_4.jpg')- Display the original image using the built-in
imshowfunction:
cv2.imshow("Original", image) - Wait until any key is pressed:
cv2.waitKey(0)
- Given shape and type, fill it with ones:
# np.ones(shape, dtype) # 5 x 5 is the dimension of the kernel, uint8: is an unsigned integer (0 to 255) kernel = np.ones((5,5), dtype = "uint8")
cv2.erodeis the built-in function used for erosion:
# cv2.erode(image, kernel, iterations) erosion = cv2.erode(image, kernel, iterations = 1)
- Display the image after erosion using the built-in
imshowfunction:
cv2.imshow("Erosion", erosion) - Wait until any...