【发布时间】:2013-12-25 11:24:08
【问题描述】:
我正在尝试使用 python 中的 opencv2 在我从笔记本电脑的摄像头记录的帧顶部绘制矩形。
但是,从 cv2.rectangle 函数返回的图像是 None。为什么?
import numpy as np
import cv2
# details of rectangle to be drawn.
x, y, h, w = (493, 305, 125, 90)
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
if not ret or not frame:
# camera didn't give us a frame.
continue
# attempt to draw a rectangle.
img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# This prints out None every time! why?
print str(img)
if not img:
continue
# We never get here since img is None. :/
cv2.imshow('img', img)
我已尝试添加检查,如您所见,相机的帧是否为空。我还尝试确保矩形实际上适合框架。
我确定我的相机可以正常工作,因为我可以成功地imshow 帧。
【问题讨论】: