
腐蚀的api:eroce(img,knernel,iterations=1)
Img: pictures to be corroded
Knernel: the size of convolution core, the bigger the convolution core, the more obvious the corrosion effect
Iterations: corrosion times
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=np.ones((7,7),np.uint8) # 腐蚀图片 dst=cv2.erode(img,kernel=kernel,iterations=5) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Expansion operation
膨胀api:dilate(img,kernel,iterations=1)Img: pictures to be expanded
Knernel: the size of the convolution kernel. The larger the convolution kernel, the more obvious the expansion effect
Iterations: expansion times
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(7,7)) # 膨胀图片 dst=cv2.dilate(img,kernel=kernel,iterations=1) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Open operation
开运算=腐蚀+膨胀The open operation requires two steps, first corrosion and then expansion. We only need to call the open operation api to complete it at one time. The biggest use of open operation is noise reduction.
Open operation api: morphologyEx (img, MORPH_OPEN, kernel)
Img: pictures requiring open operation
MORPH_ OPEN: an open operation representing morphology
Kernel: convolution kernel. If the noise is large, you can select the convolution kernel of large points
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) # 开运算 dst=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Closed operation
闭运算=膨胀+腐蚀The main problem of closed operation is that if there is a lot of noise in the image, the closed operation can eliminate the noise inside.
Closed operation api: morphologyEx (img, MORPH_CL.OSE, kernel)
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) # 闭运算 dst=cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Morphological gradient
梯度=原图-腐蚀The result of gradient calculation is often the edge of a figure.

Gradient api: morphologyEx (img, MORPH_GRADIENT, kernel)
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)) # 梯度 dst=cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Top hat operation
顶帽运算=原图-开运算The role of top hat operation: If there is a large object and some small objects in the original image, you can use top hat operation to get small objects.
Top hat operation api: morphogyEx (img, MORPH_TOPHAT, kernel)
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(13,13)) # 顶帽运算 dst=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)
Black hat operation
黑帽运算=原图-闭运算The main function of black hat operation is to find out the small noise points in the big picture.

Black hat operations api: morphologyEx (img, MORPH_BLACKHAT, kernel)
import cv2 import numpy as np # 导入一张图 img=cv2.imread('C:\\Users\\mk\\Desktop\\1.jpg') # 创建卷积核 kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(13,13)) # 黑帽运算 dst=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel) # 显示图片 cv2.imshow('img',img) cv2.imshow('dst',dst) cv2.waitKey(0)