當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python OpenCV cv2.erode()用法及代碼示例


OpenCV-Python是旨在解決計算機視覺問題的Python綁定庫。cv2.erode()方法用於對圖像進行腐蝕。侵蝕的基本思想就像僅是土壤侵蝕一樣,它侵蝕了前景物體的邊界(始終嘗試使前景保持白色)。通常在二進製圖像上執行。它需要兩個輸入,一個是我們的原始圖像,第二個是決定操作性質的結構元素或內核。僅當內核下的所有像素均為1時,原始圖像中的像素(1或0)才被視為1,否則它將被侵蝕(設為零)。

用法: cv2.erode(src, kernel[, dst[, anchor[, iterations[, borderType[, borderValue]]]]])
參數:
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 getStructuringElement.
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.
返回值: It returns an image.

用於以下所有示例的圖像:

範例1:

# Python program to explain cv2.erode() method  
  
# importing cv2  
import cv2 
  
# importing numpy  
import numpy as np 
  
# path  
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
  
# Reading an image in default mode  
image = cv2.imread(path)  
  
# Window name in which image is displayed  
window_name = 'Image'
  
# Creating kernel 
kernel = np.ones((5, 5), np.uint8) 
  
# Using cv2.erode() method  
image = cv2.erode(image, kernel)  
  
# Displaying the image  
cv2.imshow(window_name, image) 

輸出:

範例2:

# Python program to explain cv2.erode() method  
  
# importing cv2  
import cv2 
  
# importing numpy  
import numpy as np 
  
# path  
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
  
# Reading an image in default mode  
image = cv2.imread(path)  
  
# Window name in which image is displayed  
window_name = 'Image'
  
# Creating kernel 
kernel = np.ones((6, 6), np.uint8) 
  
# Using cv2.erode() method  
image = cv2.erode(image, kernel, cv2.BORDER_REFLECT)  
  
# Displaying the image  
cv2.imshow(window_name, image) 

輸出:



相關用法


注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Python OpenCV | cv2.erode() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。