
- OpenCV Python Tutorial
- OpenCV Python - Home
- OpenCV Python - Overview
- OpenCV Python - Environment
- OpenCV Python - Reading Image
- OpenCV Python - Write Image
- OpenCV Python - Using Matplotlib
- OpenCV Python - Image Properties
- OpenCV Python - Bitwise Operations
- OpenCV Python - Shapes and Text
- OpenCV Python - Mouse Events
- OpenCV Python - Add Trackbar
- OpenCV Python - Resize and Rotate
- OpenCV Python - Image Threshold
- OpenCV Python - Image Filtering
- OpenCV Python - Edge Detection
- OpenCV Python - Histogram
- OpenCV Python - Color Spaces
- OpenCV Python - Transformations
- OpenCV Python - Image Contours
- OpenCV Python - Template Matching
- OpenCV Python - Image Pyramids
- OpenCV Python - Image Addition
- OpenCV Python - Image Blending
- OpenCV Python - Fourier Transform
- OpenCV Python - Capture Videos
- OpenCV Python - Play Videos
- OpenCV Python - Images From Video
- OpenCV Python - Video from Images
- OpenCV Python - Face Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Feature Detection
- OpenCV Python - Feature Matching
- OpenCV Python - Digit Recognition
- OpenCV Python Resources
- OpenCV Python - Quick Guide
- OpenCV Python - Resources
- OpenCV Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
OpenCV Python - Morphological Transformations
Simple operations on an image based on its shape are termed as morphological transformations. The two most common transformations are erosion and dilation.
Erosion
Erosion gets rid of the boundaries of the foreground object. Similar to 2D convolution, a kernel is slide across the image A. The pixel in the original image is retained, if all the pixels under the kernel are 1.
Otherwise it is made 0 and thus, it causes erosion. All the pixels near the boundary are discarded. This process is useful for removing white noises.
The command for the erode() function in OpenCV is as follows −
cv.erode(src, kernel, dst, anchor, iterations)
Parameters
The erode() function in OpenCV uses following parameters −
The src and dst parameters are input and output image arrays of the same size. Kernel is a matrix of structuring elements used for erosion. For example, 3X3 or 5X5.
The anchor parameter is -1 by default which means the anchor element is at center. Iterations refers to the number of times erosion is applied.
Dilation
It is just the opposite of erosion. Here, a pixel element is 1, if at least one pixel under the kernel is 1. As a result, it increases the white region in the image.
The command for the dilate() function is as follows −
cv.dilate(src, kernel, dst, anchor, iterations)
Parameters
The dilate() function has the same parameters such as that of erode() function. Both functions can have additional optional parameters as BorderType and borderValue.
BorderType is an enumerated type of image boundaries (CONSTANT, REPLICATE, TRANSPERANT etc.)
borderValue is used in case of a constant border. By default, it is 0.
Example
Given below is an example program showing erode() and dilate() functions in use −
import cv2 as cv import numpy as np img = cv.imread('LinuxLogo.jpg',0) kernel = np.ones((5,5),np.uint8) erosion = cv.erode(img,kernel,iterations = 1) dilation = cv.dilate(img,kernel,iterations = 1) cv.imshow('Original', img) cv.imshow('Erosion', erosion) cv.imshow('Dialation', dilation)
Output
Original Image

Erosion

Dilation
