Write at the beginning:开运算和闭运算并不是相互可逆的。
Open operation:
- Operation process: open operation is first corrosion and then expansion
- Function: Separate objects and eliminate small areas. Features: Eliminate noise and remove small interference blocks without affecting the original image
Close operation:
- Specific operation: first expansion and then corrosion
- Function: It is to eliminate/"close" the hole in the object. Features: It can fill the closed area.
Method call:
cv.morphologyEx(要处理的图像, cv.MORPH_OPEN/cv.MORPH_CLOSE,核结构 kernel)
Specific code:
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False # 用来正常显示负号
# 载入图片
img0 = cv.imread("img/img4.png")
img1 = cv.imread("img/img5.png")
# 创建核结构
kernel = np.ones((10,10),np.uint8)
# 图像开闭运算
# 开运算:消除噪点,去除小的干扰块,不影响原来的图像
cvOpen = cv.morphologyEx(img0,cv.MORPH_OPEN,kernel)
# 闭运算:消除/闭合物体里面的孔洞,可以填充闭合区域
cvClose = cv.morphologyEx(img1,cv.MORPH_CLOSE,kernel)
# 图像展示
fig, axes = plt.subplots(nrows=2,ncols=2,figsize=(10,8))
axes[0,0].imshow(img0)
axes[0,0].set_title("原图")
axes[0,1].imshow(cvOpen)
axes[0,1].set_title("开运算结果")
axes[1,0].imshow(img1)
axes[1,0].set_title("原图")
axes[1,1].imshow(cvClose)
axes[1,1].set_title("闭运算结果")
plt.show()
Running result:
cv Xiaobai’s notes, just notes, please give advice