【问题标题】:Python TypeError: integer argument expected, got floatPython TypeError:需要整数参数,得到浮点数
【发布时间】:2018-05-18 12:59:45
【问题描述】:

您好,我正在尝试运行涉及绘制框但不断返回错误的代码

TypeError: integer argument expected, got float

出现的代码中的第一个问题是

def draw_box(Image, x, y, w, h):
    cv2.line(Image, (x, y), (x + (w/5) ,y), WHITE, 2)
    cv2.line(Image, (x+((w/5)*4), y), (x+w, y), WHITE, 2)
    cv2.line(Image, (x, y), (x, y+(h/5)), WHITE, 2)
    cv2.line(Image, (x+w, y), (x+w, y+(h/5)), WHITE, 2)
    cv2.line(Image, (x, (y+(h/5*4))), (x, y+h), WHITE, 2)
    cv2.line(Image, (x, (y+h)), (x + (w/5) ,y+h), WHITE, 2)
    cv2.line(Image, (x+((w/5)*4), y+h), (x + w, y + h), WHITE, 2)
    cv2.line(Image, (x+w, (y+(h/5*4))), (x+w, y+h), WHITE, 2)

我通过用 python 地板分割替换分割符号来解决这个问题,但接下来的方框图区域返回相同的类型错误

cv2.rectangle(Image, (Name_X_pos-10, Name_y_pos-25), (Name_X_pos +10 + (len(NAME) * 7), Name_y_pos-1), (0,0,0), -2)

我试过把 cv2.rectangle(int(Image, (Name_.. 只是为了接收回来

TypeError: int() takes at most 2 arguments (5 given)

关于如何解决这个问题的任何想法?

【问题讨论】:

  • 你不想要int(Image, (Name)..) - 你想将单个数字转换为整数,例如(int(Name_X_pos+10+len(NAME)*7), ...)
  • 我这样做了,它现在返回错误“ValueError: int() base must be >= 2 and
  • 您在int() 中包含多个参数。一次只做一个数字(仔细查看我刚刚发送的代码)。
  • 例如cv2.rectangle(Image, (int(Name_X_pos-10), int(Name_y_pos-25)), (int(Name_X_pos +10 + (len(NAME) * 7)), int(Name_y_pos-1)), (0,0,0), -2)
  • 修复了它。感谢您的帮助

标签: opencv draw typeerror


【解决方案1】:

与原始错误一样,rectangle() 调用中的问题是指定像素坐标的参数不是整数。即使没有除法,也不清楚你的一些原始变量是浮点数还是乘法只是将其中一些转换为浮点数而不是整数......无论哪种方式,如果你只是将每个坐标转换为整数,你应该很好去。例如:

cv2.rectangle(Image,
              (int(Name_X_pos-10), int(Name_y_pos-25)),
              (int(Name_X_pos +10 + (len(NAME) * 7)), int(Name_y_pos-1)),
              (0,0,0), -2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多