ss32
ss32

Reputation: 67

OpenCV - Error when drawing line or rectangle

I'm getting an error when trying to run the following code and have no idea why. This is essentially the exact same code that's used in the tutorials.

The error:

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

And the code:

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

Upvotes: 2

Views: 2641

Answers (1)

cyon
cyon

Reputation: 9548

From the docs you can tell that the function cv2.rectangle returns void. So the problem is that you are assigning the return value (which is None) to img.

Do this instead

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

Upvotes: 3

Related Questions