为什么cv2.rectangle返回的是None而不是图像?

4
我正在尝试使用 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)

我尝试添加了一些检查,以确保摄像头的帧不为空。我还试图确保矩形确实适合该框架。
我确定我的相机正常工作,因为我可以成功地显示该帧。
1个回答

12

rectangle函数不返回任何内容。

[编辑:] 在opencv2.4.x版本中,它不会返回图像,但在opencv3.0 / python版本中返回图像。

此外,请注意,非常受欢迎的py_tutorials是针对3.0版本的,因此不要混淆;)


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

  # draw a rectangle.
  cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

  # show it ;)
  cv2.imshow('img', frame)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接