当我尝试关于动作捕捉的代码时,由于此错误,我无法成功运行。
Traceback (most recent call last):
File "G:\machine learning\CV\Video Capture\motion capture with square.py", line 21, in <module>
x,y,w,h=cv2.boundingRect(thresh)
error: ..\..\..\..\opencv\modules\imgproc\src\contours.cpp:1895: error: (-215) points.checkVector(2) >= 0 && (points.depth() == CV_32F || points.depth() == CV_32S) in function cv::boundingRect
事实上,我认为数据类型可能有误,所以我将类型更改为&#39; float32&#39;和&#39; int32&#39;。但它无济于事,所以我不知道。
这是我的代码:
import cv2
import numpy as np
camera=cv2.VideoCapture(0)
firstframe=None
while True:
ret,frame = camera.read()
#cv2.imshow("frame", frame)
if not ret:
break
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(21,21),0)
if firstframe is None:
firstframe=gray
continue
frameDelta = cv2.absdiff(firstframe,gray)
thresh = cv2.threshold(frameDelta, 25, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
#(cnts,_)= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h=cv2.boundingRect(thresh)
frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("frame", frame)
cv2.imshow("Thresh", thresh)
cv2.imshow("frame2", frameDelta)
key = cv2.waitKey(1)&0xFF
if key == ord("q"):
break
camera.release()
cv2.destroyAllWindows()
答案 0 :(得分:2)
由于cv2.boundingRect()
需要一组特殊格式的点(x, y)
坐标来计算边界矩形,因此您的输入图像不是一组(x, y)
点。该方法并不意味着直接应用于图像。您必须找到给定二进制掩码的contours,然后您可以迭代所有轮廓并在各个轮廓上调用cv2.boundingRect()
:
cnts, hierarchy= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# Iterate over all the contours.
for contour in cnts:
print cv2.boundingRect(contour)