無論我嘗試什么,我似乎都無法讓 cv2.contourArea 正常作業,它總是會回傳錯誤:
(-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'contourArea'
我的代碼試圖在影像中找到最大的輪廓并使用以下代碼洗掉其余部分:
def find_biggest_contour(image):
# Copy to prevent modification
image = image.copy()
_,contours = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
#get contour sizes and return the biggest contour
max_area = -1
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if area>max_area:
biggest_contour = contours[i]
max_area = area
#create an empty mask
mask = np.zeros(image.shape, np.uint8)
#draw the biggest contour on it
cv2.drawContours(mask, [biggest_contour], -1, 255, -1)
return mask
uj5u.com熱心網友回復:
也許只是回傳值順序顛倒了。
contours, hierarchy = cv2.findContours()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490890.html
上一篇:OpenCV在運行時限制FPS
下一篇:OpenCVC 中的填孔過濾器
