OpenCV:腐蚀、膨胀
import cv2
import numpy as np
img = cv2.imread('数字.jpg')
img = cv2.resize(img, (300, 460))
# 转成灰度图片
img_H = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret,img_E=cv2.threshold(img_H, 100, 255, cv2.THRESH_BINARY) #阈值范围100-255
# k_0 = np.ones((3, 3), np.uint8)
k_1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)) #3*3的矩形框,全为1,与13行相同
img_F = cv2.erode(img, k_1, iterations=1) #腐蚀一次
img_FE = cv2.erode(img_E, k_1, iterations=1)
'''cv2.erode(src, kernel, anchor, iterations)
src:输入图像
kernel:腐蚀内核,一次的涵盖范围
anchor:腐蚀对像素点,默认内核中心点
iterations:腐蚀次数
'''
img_P = cv2.dilate(img, k_1, iterations=1) #膨胀1次
img_PE = cv2.dilate(img_E, k_1, iterations=1)
'''cv2.dilate(src, kernel, iterations)
src:输入图像
kernel:膨胀内核,一次的涵盖范围
iterations:膨胀次数
'''
cv2.imshow('原图', img)
cv2.imshow('腐蚀', img_F)
cv2.imshow('膨胀', img_P)
cv2.imshow('二值化原图', img_E)
cv2.imshow('二值化腐蚀', img_FE)
cv2.imshow('二值化膨胀', img_PE)
cv2.waitKey(0)
cv2.destroyAllWindows()
https://www.bilibili.com/video/BV16U4y1D7iP/?spm_id_from=333.1007.top_right_bar_window_history.content.click&vd_source=b89ce2a9b55dab86caa0ee60b66c9f86