【问题标题】:Why does cv2.rectangle return None instead of an image?为什么 cv2.rectangle 返回 None 而不是图像?
【发布时间】: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 帧。

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    rectangle 不返回任何内容

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

    还要注意,非常流行的 py_tutrorias 是针对 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)
    

    【讨论】:

      猜你喜欢
      • 2019-12-26
      • 2011-10-19
      • 2013-05-30
      • 2019-05-08
      • 2021-03-21
      • 2011-11-10
      • 2020-10-17
      相关资源
      最近更新 更多