- OpenCV Python - Digit Recognition
- OpenCV Python - Feature Matching
- OpenCV Python - Feature Detection
- OpenCV Python - Meanshift/Camshift
- OpenCV Python - Face Detection
- OpenCV Python - Video from Images
- OpenCV Python - Images From Video
- OpenCV Python - Play Videos
- OpenCV Python - Capture Videos
- OpenCV Python - Fourier Transform
- OpenCV Python - Image Blending
- OpenCV Python - Image Addition
- OpenCV Python - Image Pyramids
- OpenCV Python - Template Matching
- OpenCV Python - Image Contours
- OpenCV Python - Transformations
- OpenCV Python - Color Spaces
- OpenCV Python - Histogram
- OpenCV Python - Edge Detection
- OpenCV Python - Image Filtering
- OpenCV Python - Image Threshold
- OpenCV Python - Resize and Rotate
- OpenCV Python - Add Trackbar
- OpenCV Python - Mouse Events
- OpenCV Python - Shapes and Text
- OpenCV Python - Bitwise Operations
- OpenCV Python - Image Properties
- OpenCV Python - Using Matplotlib
- OpenCV Python - Write Image
- OpenCV Python - Reading Image
- OpenCV Python - Environment
- OpenCV Python - Overview
- OpenCV Python - Home
OpenCV Python Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 spde 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 appped.
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
Advertisements