这个原因是对于圆形的物体使用正方形的核去做膨胀和腐蚀就会变的像四边形,只要修改kernel变成圆形就好了;
import cv2 as cv
import numpy as np
import math
pie = cv.imread('0000.png')
kernel = np.ones((7, 7), np.uint8)
kernel_re = []
rows, cols = kernel.shape
for i in range(rows):
result = [0 if math.sqrt((i-3)**2+(j-3)**2) > 3 else 1 for j in range(cols)]
kernel_re.append(result)
kernel_re = np.array(kernel_re, np.uint8)
print(kernel_re, kernel)
print(type(kernel_re), type(kernel))
print(kernel_re.shape, kernel.shape)
dilate = cv.dilate(pie, kernel, iterations=10)
erosion = cv.erode(pie, kernel_re, iterations=10)
res = np.hstack((pie, dilate, erosion))
cv.imshow('res', res)
cv.waitKey(0)
cv.destroyAllWindows()