(-5:Bad argument) in function ‘rectangle’ – Can’t parse ‘pt1’. Sequence item with index 0 has a wrong type

The problem is that you are passing tuples with floats into the function’s parameters as the points. Here is the error reproduced:

import cv2
import numpy as np

img = np.zeros((600, 600), 'uint8')

c1 = 50.2, 12.4
c2 = 88.8, 40.8

cv2.rectangle(img, c1, c2, (255, 0, 0), -1)

Output:

Traceback (most recent call last):
  File "C:/Users/User/Desktop/temp.py", line 9, in <module>
    cv2.rectangle(img, c1, c2, (255, 0, 0), -1)
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'
> Overload resolution failed:
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
>  - Can't parse 'rec'. Expected sequence length 4, got 2
>  - Can't parse 'rec'. Expected sequence length 4, got 2

And to fix it, simply use the int() wrapper around the coordinates:

import cv2
import numpy as np

img = np.zeros((600, 600), 'uint8')

c1 = 50.2, 12.4
c2 = 88.8, 40.8

cv2.rectangle(img, (int(c1[0]), int(c1[1])), (int(c2[0]), int(c2[1])), (255, 0, 0), -1)

Leave a Comment