opencv: Incorrect error message for non-integer points in rectangle draw function: TypeError: function takes exactly 4 arguments (2 given)
System information (version)
- OpenCV => 4.1
- Operating System / Platform => Ubuntu 18.04 64 Bit
- Compiler => ❔
Detailed description
The rectangle function is meant to take integer tuples for p1 and p2, however when floats are used instead, a TypeError: function takes exactly 4 arguments (2 given)
error is given, instead of a TypeError: integer argument expected, got float
, which is the response given by the line function. The error given is very misleading, and I believe it would be more useful to give the same error that other drawing functions give when non-integers are used.
Steps to reproduce
import numpy as np
import cv2
img = cv2.rectangle(np.ones((10, 10)),(384.2,0),(510,128),1)
This throws the error, TypeError: function takes exactly 4 arguments (2 given)
, while
img = cv2.line(np.ones((10, 10)),(384.2,0),(510,128),1)
throws TypeError: integer argument expected, got float
.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 23
- Comments: 25 (5 by maintainers)
I got the same issue. I resolved it by converting all the coordinate of the bounding box into int instead of float. You are facing the problem because of one coordinate in float
@MLDLCo The issue isn’t that ints are required, rather that the wrong error message is being given for this problem. It should give
TypeError: integer argument expected, got float.
instead ofTypeError: function takes exactly 4 arguments (2 given)
.The root cause of the issue is current implementation of the overload resolution in the generated Python bindings. First of all it tries to parse arguments for
but it fails (because floats are not convertible to integers as was already stated above) - it sets error message, but in C++ interface there is an overload, that clears previously set error message:
that accepts tuple with 4 elements as the second argument. First it is checking the tuple length - you provide tuple with 2 elements instead of 4 - error message as reported to user as the last set.
In my opinion, to solve the problem with inappropriate/misleading message the following steps should be done:
PyArg_ParseTuple
it is not possible).OverloadError
type with previously collected messages.The main drawback of the suggested way is a small overhead increasing for functions calls.
This is cause because you tuples contain floats. OpenCV takes integers within those tuples.
This could be a integer overrun.
i recommande to install the version python 3.4.8.29 pip install -U opencv-python==3.4.8.29
OpenCV should get more tolerant of various data types when it expects exactly tuples of integers. it would be great if OpenCV accepted floats in addition to ints, and if it accepted numpy arrays in addition to tuples. it would be great if that was part of the bindings generation so all OpenCV functions benefit from more tolerance in accepting arguments. drawing functions (taking a
shift
argument) would benefit from a special case of accepting floats.I had the same error which ended up being caused by my color tuple being a tuple of numpy.int32 values which I thought were allowed in cv2. I had to manually convert the items to the stock python int() type.
This error message definitely needs to be fixed since it leads you down the wrong rabbit hole for a long time before you realize what the actual problem is.
@MLDLCo Thank You.
@MLDLCo thank you very much! Your solution solves my problem.
The reason of this issue is I set two locations as float type, but I think this is quite confusing 😅
在 2019-10-14 14:13:07,Ajith notifications@github.com 写道:
i think it is because of the reason that you are executing a python2 code in python3 but i am not sure of it
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.