Erosion and Dilation
The most basic morphological operations are two: Erosion and Dilation
Basics of Erosion:
- Erodes away the boundaries of the foreground object
- Used to diminish the features of an image.
Working of erosion:
- A kernel(a matrix of odd size(3,5,7) is convolved with the image.
- 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).
- Thus 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.
Basics of dilation:
- Increases the object area
- Used to accentuate features
Working of dilation:
- A kernel(a matrix of odd size(3,5,7) is convolved with the image
- A pixel element in the original image is ‘1’ if at least one pixel under the kernel is ‘1’.
- It increases the white region in the image or the size of the foreground object increases
Example: Python OpenCV Erosion and Dilation
Python3
# Python program to demonstrate erosion and # dilation of images. import cv2 import numpy as np # Reading the input image img = cv2.imread( 'geeks.png' , 0 ) # Taking a matrix of size 5 as the kernel kernel = np.ones(( 5 , 5 ), np.uint8) # The first parameter is the original image, # kernel is the matrix with which image is # convolved and third parameter is the number # of iterations, which will determine how much # you want to erode/dilate a given image. img_erosion = cv2.erode(img, kernel, iterations = 1 ) img_dilation = cv2.dilate(img, kernel, iterations = 1 ) cv2.imshow( 'Input' , img) cv2.imshow( 'Erosion' , img_erosion) cv2.imshow( 'Dilation' , img_dilation) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
Output
Getting Started with Python OpenCV
Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos.
Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis.
In this article, we will discuss Python OpenCV in detail along with some common operations like resizing, cropping, reading, saving images, etc with the help of good examples.