OpenCV Python不支持的数组类型错误

时间:2013-12-27 18:29:07

标签: python opencv

我是Python的新手(但不是openCV的新手),我很确定所有内容都安装正确,我已经测试了一些程序并且似乎工作正常,但是当我想要在图像上绘图时,例如此代码取自Python openCV教程:

import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img,(0,0),(511,511),(255,0,0),5)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我收到以下错误:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupported array type) in cvGetMat, file /build/buildd/opencv-2.3.1/modules/core/src/array.cpp, line 2482
Traceback (most recent call last):
File "/home/dccv/rec 2.py", line 17, in <module>
cv2.imshow('img',img)
cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/array.cpp:2482: error: (-206)
Unrecognized or unsupported array type in function cvGetMat

任何帮助将不胜感激,我在Windows和ubuntu上都得到了相同的错误。

1 个答案:

答案 0 :(得分:4)

line函数返回None,因此您尝试显示None

修复(第6行)是不将img变量设置为返回值,而只是忽略返回值:

import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(511,511),(255,0,0),5)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()