为什么cv2.rectangle没有返回图像?

2024-05-16 10:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在我用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帧。


Tags: toimportnoneimgifnotcv2frame
1条回答
网友
1楼 · 发布于 2024-05-16 10:54:32

rectangle不返回任何内容

[编辑:]在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)

相关问题 更多 >