首页 > 解决方案 > CV2.rectangle 在 Windows 上需要整数,但在 Ubuntu 上使用浮点数

问题描述

我在 ubuntu 上编写了一些代码,并试图在 Windows 上运行它。

我在 Windows 上收到此错误:TypeError: integer argument expected, got float

self.rect1 = [(self.width / 2) - 10, (self.height / 2) - 10]
self.rect2 = [(self.width / 2) + 10, (self.height / 2) + 10]
cv2.rectangle(self.image, tuple(self.rect1), tuple(self.rect2), (255,0,0), -1)

我知道我可以通过使用来解决这个问题,int()但我想像在 Ubuntu 上那样使用浮动值。

这是操作系统特定的原因吗?

更新

我相信@mesutpiskin 的回答是正确的。如果我手动将变量定义为浮点数:

self.rect1 = [200.5, 200.5]
self.rect2 = [210.5, 210.5]

我在 Ubuntu 上遇到同样的错误。我猜 Ubuntu 在除法期间返回 int 而不是 float。打印类型证实了这一点。

rect3 = [(width / 2) - 10, (height / 2) - 10]   
rect4 = [(width / 2) + 10, (height / 2) + 10]
print(type(rect3[0]))

输出:<type 'int'>

标签: pythonwindowsopencvubuntucv2

解决方案


推荐阅读