我正在尝试使用 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)
我尝试添加了一些检查,以确保摄像头的帧不为空。我还试图确保矩形确实适合该框架。
我确定我的相机正常工作,因为我可以成功地显示该帧。