我使用opencv制作了一个手势鼠标控制器,但是当我运行代码时,我在cv2上遇到了这个错误。圆圈
Traceback (most recent call last):
File "C:/Users/Arne/PycharmProjects/JARVISv2/Virtual.py", line 65, in <module>
cv2.circle(img, (cx, cy), (w + h) / 4, (0, 0, 255), 2)
TypeError: integer argument expected, got float
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674)
SourceReaderCB::~SourceReaderCB terminating async callback
Process finished with exit code 1
请有人帮帮我,我离完成我的项目有点近了
提前谢谢这是我的代码--
if (len(conts) == 2):
if (pinchFlag == 1):
pinchFlag = 0
mouse.release(Button.left)
x1, y1, w1, h1 = cv2.boundingRect(conts[0])
x2, y2, w2, h2 = cv2.boundingRect(conts[1])
cv2.rectangle(img, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 2)
cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (255, 0, 0), 2)
cx1 = int(x1 + w1 // 2
)cy1=int(y1 h1//2)cx2=int(x2 w2//2)cy2=int(y2 h2//2)cx=(cx1 cx2)/2 cy=(cy1 cx2)/2 cv2。线(img,(cx1,cy1),(cx2,cy2),(255,0,0),2)cv2。圆圈(img,(cx,cy),2,(0,0255),2)鼠标C=(sx-(cx*sx/camx),cy*sy/camy)鼠标。位置=鼠标移动时鼠标移动。位置!=穆塞洛克:通过
您需要为cv2提供整数坐标。线和cv2。圆圈()。将cx和cy更改为int(cx1)和int(cy1),或者仅更改为int(cx1,cy1),类似地,更改为cx2和cy2。或者当除以2时,使用//而不是/进行除法,以创建整数值。
如果您对要求int(一个小版本(4.5.2)上的丑陋且不兼容的更改)这一非音速错误感到恼火,您可以撤消此操作:
import cv2
def monkeypatch_cv2():
v = tuple(map(int, cv2.__version__.split('.')))
if (v >= (4,5,2)) and cv2.circle.__name__ != 'monkey_circle':
old_circle = cv2.circle
old_line = cv2.line
def monkey_circle(img, center, *args, **kwargs):
old_circle(img, tuple(map(int, center)), *args, **kwargs)
def monkey_line(img, pt1, pt2, *args, **kwargs):
old_line(img, tuple(map(int, pt1)), tuple(map(int, pt2)), *args, **kwargs)
cv2.circle = monkey_circle
cv2.line = monkey_line
monkeypatch_cv2()
这样,您就不需要修复所有使用float的完美工作代码。