Change language

Python OpenCV | cv2.erode () method

OpenCV-Python — is a Python bindings library for solving computer vision problems.  cv2.erode () is used for image erosion. The basic idea of ​​erosion is only similar to soil erosion, it blurs the boundaries of the foreground object (always try to keep the foreground white). This is usually done on binary images. It requires two inputs, one of which is our original image, the other is called a structuring element or core, which determines the nature of the operation. A pixel in the original image (either 1 or 0) will be considered 1 only if all pixels under the kernel are 1, otherwise it is blurred (zeroed out).

Syntax: cv2.erode (src, kernel [, dst [, anchor [, iterations [, borderType [, borderValue]]]]])
Parameters:
src: It is the image which is to be eroded.
kernel: A structuring element used for erosion. If element = Mat (), a 3 x 3 rectangular structuring element is used. Kernel can be created using >.
dst: It is the output image of the same size and type as src.
anchor: It is a variable of type integer representing anchor point and it’s default value Point is (-1, -1) which means that the anchor is at the kernel center.
borderType: It depicts what kind of border to be added. It is defined by flags like cv2.BORDER_CONSTANT , cv2.BORDER_REFLECT , etc.
iterations: It is number of times erosion is applied.
borderValue: It is border value in case of a constant border.
Return Value: It returns an image.

The image is used for all the examples below:

Example # 1:

Output:

Example # 2:

# Python program to explain the cv2.erode () method

 
# cv2 import

import cv2

 
# import numpy

  import numpy as np

 
# path

path = r ’C: UsersRajnishDesktoppythonengineeringgeeks.png’

  
# Read image in default mode

image = cv2.imread (path) 

  
# Name of the window in which the image is displayed

window_name = ’Image’

  
# Building the kernel

kernel = np.ones (( 5 , 5 ), np.uint8)

 
# Using the cv2.erode () method

image = cv2.erode (image, kernel) 

 
# Displaying an image
cv2.imshow (window_name, image) 

# Python program to explain the cv2.erode () method

 
< code class = "comments"> # cv2 import

import cv2

 
# numpy imports

import numpy as np

 
# path

path = r ’C: UsersRajnishDesktoppythonengineeringgeeks.png’

  
# Read image in default mode

image = cv2.imread (path) 

 
# Name of the window in which the image is displayed

window_name = ’Image’

  
# Building the kernel

kernel = np.ones (( 6 , 6 ), np.uint8)

  
# Using the cv2. erode ()

image = cv2. erode (image, kernel, cv2.BORDER_REFLECT) 

 
# Display image
cv2.imshow (window_name, image) 

Output:

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically