5

I am new to Python (but not new to openCV) and I am pretty sure everything is installed correctly, I have tested some programs and the seem to work fine, but when ever I want to draw on an image, for example this code taken from a Python openCV tutorial :

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

I get this following error:

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

any help would be appreciated, I get the same error on both windows and ubuntu.

2
  • If I were to read and display an image without adding anything to it, it would work fine! Commented Dec 27, 2013 at 18:37
  • Is the path right? Is this relevant: stackoverflow.com/questions/14155081/… Commented Dec 27, 2013 at 18:40

1 Answer 1

5

line function returns None so you're trying to show None.

The fix (on line 6) is to not set the img variable to the return value, instead just ignore the return value:

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()
Sign up to request clarification or add additional context in comments.

1 Comment

@user3140249, your code is based on upcoming OpenCV 3.x version. But you have installed OpenCV 2.x. In OpenCV 3.x, all drawing functions like cv2.line() return an image, while not in OpenCV 2.x. So if you are referring to tutorials here (opencv-python-tutroals.readthedocs.org/en/latest/index.html), please keep in mind those tutorials are for OpenCV 3.x

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.