python - 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'**
解决方案
推荐阅读
- javascript - 单击元素中的某处时防止默认链接行为
- django - Django rest_api 和 React
- django - 从表中的 Django 视图中获取不同的数据
- python - Ubuntu、cx_Freeze 和 multiprocessing.Manager() 在“spawn”类型进程的情况下发生冲突
- c# - 尝试加载 Excel 信息时出错
- swagger - Swagger UI 冻结/崩溃 - Asp.Net Core 3.1。有什么修复吗?
- javascript - JS Promise .then().catch().then() 链接在 IE11 中不起作用
- formatting - Excel 2016 - 使用获取数据导入 CSV 和格式以显示在工作表的不同列中
- marshmallow-sqlalchemy - 使用订单输出参数并使用 marshmallow-sqlalchemy 指定序列化键
- kotlin - Kotlin:注解 (acc: S, T) -> S 是什么意思?