我正在上一堂有关OpenCV的课程,以了解相机上是否有东西移动。当我测试它时,它不起作用,并显示:
TypeError:UMat()缺少必需的参数“范围”(位置2)
我已经引用了这篇文章,但是无法解决我的问题:OpenCV TypeError: Expected cv::UMat for argument 'src' - What is this?
import cv2
def fram_diff(prev_frame, cur_frame, next_frame):
diff_frames_1=cv2.absdiff(next_frame, cur_frame)
diff_frames_2=cv2.absdiff(cur_frame, prev_frame)
return cv2.bitwise_and(diff_frames_1, diff_frames_2)
def get_frame(cap, scaling_factor):
frame = cap.read()
frame = cv2.resize(cv2.UMat(frame), None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
return gray
if __name__=='__main__':
cap = cv2.VideoCapture(0)
scaling_factor=0.5
prev_frame = get_frame(cap, scaling_factor)
cur_frame = get_frame(cap, scaling_factor)
next_frame = get_frame(cap, scaling_factor)
while True:
cv2.imshow('Object Movement', fram_diff(prev_frame,cur_frame, next_frame))
prev_frame=cur_frame
cur_frame=next_frame
next_frame=get_frame(cap, scaling_factor)
key=cv2.waitKey(10)
if key==27:
break
cv2.destroyAllWindows()
我的错误是:
line 19, in <module>
prev_frame = get_frame(cap, scaling_factor)
line 12, in get_frame
frame = cv2.resize(cv2.UMat(frame), None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
TypeError: UMat() missing required argument 'ranges' (pos 2)
我刚刚发现了问题所在!它在函数get_frame中。
要更正我的代码,我已替换行:
frame = cap.read()
按行
ret, frame = cap.read()