Opencv polylines function in python throws exception Opencv polylines function in python throws exception numpy numpy

Opencv polylines function in python throws exception


The problem in my case was that numpy.array created int64-bit numbers by default. So I had to explicitly convert it to int32:

points = np.array([[910, 641], [206, 632], [696, 488], [458, 485]])# points.dtype => 'int64'cv2.polylines(img, np.int32([points]), 1, (255,255,255))

(Looks like a bug in cv2 python binding, it should've verified dtype)


This function is not enough well documented and the error are also not very useful. In any case, cv2.polylines expects a list of points, just change your line to this:

import cv2import numpy as npimg = np.zeros((768, 1024, 3), dtype='uint8')points = np.array([[910, 641], [206, 632], [696, 488], [458, 485]])cv2.polylines(img, [points], 1, (255,255,255))winname = 'example'cv2.namedWindow(winname)cv2.imshow(winname, img)cv2.waitKey()cv2.destroyWindow(winname)

The example above will print the following image (rescaled):

enter image description here


the error says your array should be of dimension 2. So reshape the array as follows:

points = points.reshape(-1,1,2)

Then it works fine.

Also, answer provided by jabaldonedo also works fine for me.