opencv腐蚀操作:让圆腐蚀后不变形依旧保持圆形?
代码:
import cv2 as cv
import numpy as np
pie=cv.imread('d:/pic/pie.png')
kernel=np.ones((7,7),np.uint8)
dilate=cv.dilate(pie,kernel,iterations=10)
erosion=cv.erode(pie,kernel,iterations=10)
res=np.hstack((pie,dilate,erosion))
cv.imshow('res',res)
cv.waitKey(0)
cv.destroyAllWindows()
结果:
回答
这个原因是对于圆形的物体使用正方形的核去做膨胀和腐蚀就会变的像四边形,只要修改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()
以上是 opencv腐蚀操作:让圆腐蚀后不变形依旧保持圆形? 的全部内容, 来源链接: utcz.com/a/44663.html