Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
653 views
in Technique[技术] by (71.8m points)

python - cv2.rectangle(frame, (startX, startY), (endX, endY), (155, 255, 0), 2) throwing error TypeError: function takes exactly 4 arguments (2 given)

I am trying to draw a rectangle on an image using following code

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

I have used the same code in past and it worked very well. I don't know why is it not working now. Can anyone please help.

question from:https://stackoverflow.com/questions/65922712/cv2-rectangleframe-startx-starty-endx-endy-155-255-0-2-throwing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I read following threads

https://github.com/opencv/opencv/issues/15465

https://github.com/opencv/opencv/issues/16432

It appears that startX, startY, endX, endY must be integers. In my case they were fractions bewteen 0 and 1. I multiplied startX, endX with image width and startY, endY with image height and converted them to int.

(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)

This solved the issue for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...