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 參數。
以最大數量 - 這是你的葉子。
填充這個最大計數并繪制它?,F在您可以平滑它或執行任何其他形態學操作。
結果:
添加回答
舉報