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



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

错误:

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()

从文档中可以看出函数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()

相关内容

  • 没有找到相关文章

最新更新