【问题标题】:TypeError:integer argument expected int got float in opencv circleTypeError:integer argument expected int got float in opencv circle
【发布时间】:2020-05-09 04:57:24
【问题描述】:

我已经使用 opencv 制作了一个手势鼠标控制器,但是当我运行代码时
我在 cv2.circle 上遇到了这个错误...

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 + cy2) / 2 cv2.line(img, (cx1, cy1), (cx2, cy2), (255, 0, 0), 2) cv2.circle(img, (cx, cy), 2, (0, 0, 255), 2) mouseLoc = (sx - (cx * sx / camx), cy * sy / camy) mouse.position = mouseLoc 而 mouse.position != mouseLoc: 通过

【问题讨论】:

    标签: opencv opencv3.0 opencv-contour opencv-python


    【解决方案1】:

    您需要为 cv2.line 和 cv2.circle() 提供整数坐标。将 cx 和 cy 更改为 int(cx1) 和 int(cy1) 或者可能只是 int(cx1,cy1) 和 cx2 和 cy2 类似。或者在除以 2 时使用 // 而不是 / 进行除法以创建整数值。

    【讨论】:

    • 我这样做了,但仍然得到同样的错误。
    • (w + h) / 4 是浮动的
    • (w + h) // 4 与您对 cx 和 cy 所做的相同。请参阅 // 运算符的 python 文档。或 int((w+h)/4)。您不需要 int() 和 /,认为它不会受到伤害。 python-reference.readthedocs.io/en/latest/docs/operators/…
    【解决方案2】:

    如果您对需要 int 的非pythonic 错误感到恼火,这是次要版本 (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()
    

    这样,您无需修复所有使用浮点数的完美工作代码。

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多