OpenCV - 绘制线条或矩形时出错

时间:2014-04-20 13:03:12

标签: python opencv drawing

我在尝试运行以下代码时遇到错误并且不知道为什么。这基本上与教程中使用的代码完全相同。

错误:

Traceback (most recent call last):
  File "cv_trackbar2.py", line 41, in <module>
    cv2.imshow('frame',img)
cv2.error: /build/buildd/opencv-2.4.5+dfsg/modules/core/src/array.cpp:2482: error: (-206)     Unrecognized or unsupported array type in function cvGetMat

代码:

import cv2
import numpy as np


# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a green rectangle
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

while (True):

    cv2.imshow('draw',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:3)

docs您可以看出函数cv2.rectangle返回void。所以问题是你要将返回值(None)分配给img

改为

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Draw a green rectangle
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

while (True):

    cv2.imshow('draw',img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
cv2.destroyAllWindows()