Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: function takes exactly 4 arguments (2 given) in cv2.rectangle function

Tags:

python

opencv

I know all the solutions on the internet say to give integer co-ordinates but that isn't working for me.

def box(x,y,w,h):
    print(x,y,w,h)
    print(type(x),type(y),type(w),type(h))
    cv2.rectangle(image, (int(x),int(y)) , (int(x+w),int(y+h)) , (255,0,0) , 2.0) ----> error

for i in indices.flatten():
    x,y,w,h = boxes[i][0],boxes[i][1],boxes[i][2],boxes[i][3]
    box(int(x),int(y),int(w),int(h))    

Output of debug

414 1308 53 404
<class 'int'> <class 'int'> <class 'int'> <class 'int'>

Python version - 3.7.0 OpenCv version - 4.4.0.42

like image 204
Yash Khasgiwala Avatar asked Sep 04 '20 08:09

Yash Khasgiwala


2 Answers

TypeError: function takes exactly 4 arguments (2 given) happens if any coordinate is not int.

Consider below snippets:

arr = np.zeros([200, 100, 3], np.int8)
_ = cv2.rectangle(arr, (-100, 20), (0, 0), (255, 0, 0), 2)

The above snippet will work. But the following snippets will raise TypeError:

_ = cv2.rectangle(arr, (-100, 20), (0, 0.0), (255, 0, 0), 2)

_ = cv2.rectangle(arr, (-100, 20), (0, 12345678900000000000000001), (255, 0, 0), 2)
like image 155
tpk Avatar answered Nov 18 '22 02:11

tpk


Using int coordinates worked well for me.

(startX, startY, endX, endY) = rect
startX = int(startX * W)
startY = int(startY * H)
endX = int(endX * W)
endY = int(endY * H)

cv2.rectangle(frame, (startX, startY), (endX, endY), (155, 255, 0), 2)

above code worked well. In my case rect contained values from 0 to 1.

like image 12
mayank1513 Avatar answered Nov 18 '22 03:11

mayank1513