Solving cv2 OpenCV error – Bad argument in function

cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ is an error which occurs when we pass tuples with floats into the parameters of a function.

In this article I am going to show you how to solve this error and comprehend its origin. While also sharing with you other solutions ( If I can Find them ) which have worked for other developers.

Describing cv2 OpenCV error – Bad argument in function

First, we need to reproduce the error, let us start by checking out the message of the error.

Solving cv2 OpenCV error Bad argument in function-4

Bellow is the code which reproduces the error.

Solving cv2 OpenCV error Bad argument in function - 1

Solution : Wrapping the Parameters

Our goal is for the parameters to be accepted by the function, in order to do that we need to wrap the parameters with the following wrapper int().

In this case the following line.

Solving cv2 OpenCV error Bad argument in function-2

Should be replaced with the line bellow.

Solving cv2 OpenCV error Bad argument in function-3

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

The only solution I have found is the solution I explained in the section above, I hope this was enough to solve the issue for you.

Summing-up

Guys, this has been my best attempt at helping you understand and solve the error cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘rectangle’ . I hope you found a solution which suits your needs. Consider helping the blog if you can by donating to our Kofi account.

Thank you for reading, keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/