首页 > 解决方案 > cv::contourArea 中的 OpenCV 断言失败错误

问题描述

下面给出了用于访问机器学习项目中的网络摄像头的程序。我还分享了运行代码时引发的错误。功能有问题contourArea

为什么会发生这种情况,我该如何解决?

import cv2
from keras.models import load_model
import numpy as np
from collections import deque

cap=cv2.VideoCapture(0)
pred_class=0
pts=deque(maxlen=512)
blackboard=np.zeros((480,640,3), dtype=np.uint8)
digit=np.zeros((200,200,3), dtype=np.uint8)
while(cap.isOpened()):
    ret,img=cap.read()
    img=cv2.flip(img,1)
    imgHSV=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
    cnts=cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
    center=None
    if len(cnts)>=1:
        contour=max(cnts, key=cv2.contourArea)
        if cv2.contourArea(contour)>250:
            ((x,y),radius)=cv2.minEnclosingCircle(contour)
            cv2.circle(img, (int(x), int(y), int(radius), (0,255,255), 2))

上面的代码有错误。

error                                     Traceback (most recent call last)
<ipython-input-5-f7a89704b0bf> in <module>
     17     center=None
     18     if len(cnts)>=1:
---> 19         contour=max(cnts, key=cv2.contourArea)
     20         if cv2.contourArea(contour)>250:
     21             ((x,y),radius)=cv2.minEnclosingCircle(contour)

**error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:315: 
error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 
'cv::contourArea'**

标签: pythonopencvmachine-learningcomputer-visionopencv-contour

解决方案


推荐阅读