先进行腐蚀再进行膨胀就叫做开运算,主要用于消除图像上的噪声节点。
# 开运算测试代码
# https://www.cnblogs.com/Undo-self-blog/p/8436389.html
import cv2
import numpy as np
def cv_imwrite(filename,img):
'''
:param filename: 要写出的文件名
:param img: 要写出的图像数据
:return: 无
'''
cv2.imencode(ext='.jpg',img=img)[1].tofile(filename)
def cv_imread(file_path):
'''
:param file_path: 读入的图像文件路径
:return: 读入的图像数据
'''
cv_img=cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),flags=cv2.IMREAD_GRAYSCALE)
return cv_img
if __name__ == '__main__':
src = cv_imread(file_path="开运算.jpg")
openOperator = cv2.morphologyEx(src=src, op=cv2.MORPH_OPEN, kernel=np.ones(shape=[10, 10], dtype=np.uint8),
iterations=1)
cv2.imshow(winname="raw