类型错误:在cv2.rectangle函数中,该函数需要4个参数(已提供2个)。

10

我知道互联网上所有的解决方案都说要给整数坐标,但这对我没用。

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

调试输出

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

Python版本-3.7.0 OpenCv版本-4.4.0.42


3
“thickness”是你的最后一个参数,必须是“int”类型,参见rectangle文档 - HansHirse
1
你好,你得到答案了吗?我也遇到了同样的问题。 - Mayank Kumar Chaudhari
1
如果传递的坐标不是“int”类型,则会发生这种情况。 - mrtpk
在我的情况下,对于pt1和pt2(第二个和第三个输入),大的负值(-2147483646,-2147483657)导致了相同的错误信息。这个错误信息并不是很清楚... - Antti A
2个回答

12

使用整数坐标对我很有效。

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

以上代码运行良好。在我的情况下,rect包含了从0到1的值。


3

TypeError: 函数需要4个参数,但只提供了2个会在任何坐标不是int时发生。

请考虑以下代码片段:

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

上面的代码片段是有效的。但以下代码片段可能会引发TypeError错误:
_ = cv2.rectangle(arr, (-100, 20), (0, 0.0), (255, 0, 0), 2)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接