5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺

import cv2 #opencv读取的格式是BGR
import numpy as np
import matplotlib.pyplot as plt#Matplotlib是RGB
%matplotlib inline 

# 读取
img=cv2.imread('lena.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 图像二值化
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)

# 图像腐蚀操作
# 首先设置卷积核大小,3x3,值置1,格式为numpy中的整数uint8,0到255.
kernel = np.ones((3,3),np.uint8) 
# 腐蚀操作,可以设置迭代次数,改动iterations看结果,0为不腐蚀
erosion = cv2.erode(thresh1,kernel,iterations = 0)

cv2.imshow('erosion', erosion)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 集合对比
kernel = np.ones((3,3),np.uint8)
erosion_0 = cv2.erode(thresh1,kernel,iterations = 0)
erosion_1 = cv2.erode(thresh1,kernel,iterations = 1)
erosion_2 = cv2.erode(thresh1,kernel,iterations = 2)
erosion_3 = cv2.erode(thresh1,kernel,iterations = 3)
erosion_4 = cv2.erode(thresh1,kernel,iterations = 100)
res = np.hstack((erosion_0,erosion_1,erosion_2,erosion_3,erosion_4))
cv2.imshow('res', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

0

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第1张图片

1

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第2张图片

2

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第3张图片

 3

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第4张图片

 100,100轮,腐蚀没了

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第5张图片

 对比

5.opencv-python;cv2库;形态学;图像腐蚀;去毛刺_第6张图片

你可能感兴趣的:(opencv,opencv)