Skip to content
Home » Error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’

Error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’

To solve Tflite detext error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ error follow below methods.

How to solve error: cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ ?

The issue is because you’re using tuples with floats as the points in the function’s parameters. The below code will reproduce the above error. Here’s how to reproduce the error:

import cv2
import numpy as np

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

x1 = 12.2, 14.5
x2 = 90.6, 38.5

cv2.rectangle(img, x1, x2, (255, 0, 0), -1)

ERROR LOG

TypeError                                 Traceback (most recent call last)
<ipython-input-1-47148b49e5ed> in <module>()
----> 9 cv2.rectangle(img, x1, x2, (255, 0, 0), -1)
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'rectangle'

Solution :

To solve this error you need to use int() wrapper around the co-ordinates.

import cv2
import numpy as np

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

x1 = 12.2, 14.5
x2 = 90.6, 38.5

cv2.rectangle(img, (int(x1[0]), int(x1[1])), (int(x2[0]), int(x2[1])), (255, 0, 0), -1)

Output:

array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)

Hope the above solution works.

Also read :

AttributeError: ‘NoneType’ object has no attribute ‘something’
ImportError: attempted relative import with no known parent package