2 回答
TA贡献1887条经验 获得超5个赞
尝试这个:
import cv2
import numpy as np
input = cv2.imread('source.png')
gray = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY)
#find all contours
img = cv2.pyrDown(gray)
_, threshed = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
contours,_ = cv2.findContours(threshed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#find maximum contour and draw
cmax = max(contours, key = cv2.contourArea)
epsilon = 0.002 * cv2.arcLength(cmax, True)
approx = cv2.approxPolyDP(cmax, epsilon, True)
cv2.drawContours(input, [approx], -1, (0, 255, 0), 3)
cv2.imshow("Contour", input)
width, height = gray.shape
#fill maximum contour and draw
img = np.zeros( [width, height, 3],dtype=np.uint8 )
cv2.fillPoly(img, pts =[cmax], color=(255,255,255))
cv2.imshow("Filtered", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
算法:
FindContours方法。您可以调整 epsilon 参数。
以最大数量 - 这是你的叶子。
填充这个最大计数并绘制它。现在您可以平滑它或执行任何其他形态学操作。
结果:
添加回答
举报