Life2Coding
Erosion and Dilation of Images using OpenCV in Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply it in various aspects using some examples. Then the output will be shown with some comparisons as well.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV 3 with Python 3

Goals:

In this tutorial, I will show you how to erode and dilate images using OpenCV and Python coding.

Documentation:

Python: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst

Erodes an image by using a specific structuring element.

Parameters:          

  • 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.
  • dst – output image of the same size and type as src.
  • kernel – structuring element used for erosion; if element=Mat() , a 3 x 3 rectangular structuring element is used. Kernel can be created using getStructuringElement().
  • anchor – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
  • iterations – number of times erosion is applied.
  • borderType – pixel extrapolation method (see borderInterpolate for details).

Python: cv2.dilate(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]]) → dst

Dilates an image by using a specific structuring element.

Parameters:          

  • 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.
  • dst – output image of the same size and type as src.
  • kernel – structuring element used for dilation; if elemenat=Mat() , a 3 x 3 rectangular structuring element is used. Kernel can be created using getStructuringElement()
  • anchor – position of the anchor within the element; default value (-1, -1) means that the anchor is at the element center.
  • iterations – number of times dilation is applied.
  • borderType – pixel extrapolation method (see borderInterpolate for details).
  • borderValue – border value in case of a constant border

Steps:

  • Load the Original image using cv2.imread()
  • Apply erode and dilate using cv2.erode() and cv2.dialate()
  • Display all the images using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import cv2
import numpy as np


def erodeDilate(imagePath):
    # Read the input image
    frame = cv2.imread('hanif.jpg')

    # kernel is used to control the amount of eroding and dilating
    kernel = np.ones((5, 5), np.uint8)

    # Repeat the erosion and dilation by changing iterations.
    img_erode = cv2.erode(frame, kernel, iterations=2)
    img_dilate = cv2.dilate(frame, kernel, iterations=2)

    cv2.imshow('Input', frame)
    cv2.imshow('Erosion', img_erode)
    cv2.imshow('Dilation', img_dilate)
    cv2.waitKey(0)

def main():
    imagePath='hanif.jpg'
    erodeDilate(imagePath)
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

Output:

erode-dialate Erosion and Dilation of Images using OpenCV in Python

life2coding_icon [] Erosion and Dilation of Images using OpenCV in Python

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.