我在raspberry pi上使用OpenCV并用Python构建。尝试制作一个简单的物体跟踪器,通过对图像进行阈值化并找到轮廓来定位质心,从而使用颜色来找到物体。当我使用以下代码时:
image=frame.array
imgThresholded=cv2.inRange(image,lower,upper)
_,contours,_=cv2.findContours(imgThresholded,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnt=contours[0]
Moments = cv2.moments(cnt)
Area = cv2.contourArea(cnt)
我得到以下错误。在
^{pr2}$我尝试了一些其他设置,但得到了相同的错误或
ValueError: too many values to unpack
我用的是皮卡默拉。有什么建议来确定质心位置吗?在
谢谢
Z
错误1:
简单地说,
^{pr2}$cv2.findContours()
方法在给定图像中没有找到任何轮廓,因此建议在访问轮廓之前进行一次健全性检查,如下所示:错误2
此错误是由于
_,contours,_ = cv2.findContours
引起的,因为cv2.findContours
只返回2个值、轮廓和层次结构,因此很明显,当您试图从cv2.findContours
返回的2个元素元组中解压3个值时,它将引发上述错误。在另外,
cv2.findContours
会改变输入mat的位置,因此建议将cv2.findContours
调用为:相关问题 更多 >
编程相关推荐