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

时间:2013-12-08 10:14:08

标签: python opencv

我正在尝试在我使用opencv2在python中从笔记本电脑的相机录制的帧上绘制矩形。

但是,从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画面。

1 个答案:

答案 0 :(得分:10)

rectangle不会返回任何内容

[edit:]在opencv2.4.x中,但它 返回opencv3.0 / python中的图像

还要注意,非常受欢迎的py_tutrorials是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)