Related concepts
Morphological operations-corrosion
parameter:
- img: the image to be processed
- kernel: nuclear structure
- iteration: the number of corrosions, the default is 1
Morphological operations - dilation
parameter:
- img : the image to be processed
- kernel: nuclear structure
- iteration: number of times of expansion, default is 1
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei']
#读取图像
img = cv.imread("lena.png")
#创建核结构
kernal = np.ones((5,5),np.uint8)
#图像的腐蚀和膨胀
erosion = cv.erode(img,kernal) #腐蚀
dilate = cv.dilate(img,kernal)
#图像的显示
#使用 Matplotlib 库创建一个包含 1 行 3 列的子图网格(Grid),并设置整个图形的大小为 5x4 英寸,分辨率为 100 dpi
fig,axes = plt.subplots(nrows=1,ncols=3,figsize=(5,4),dpi=100)
axes[0].imshow(img[:,:,::-1])
axes[0].set_title("原图")
axes[1].imshow(erosion[:,:,::-1])
axes[1].set_title("腐蚀后结果")
axes[2].imshow(dilate[:,:,::-1])
axes[2].set_title("膨胀后结果")
plt .show()