【问题标题】:TypeError: function takes exactly 4 arguments (2 given) in cv2.rectangle function [duplicate]TypeError:函数在 cv2.rectangle 函数中正好有 4 个参数(给定 2 个)[重复]
【发布时间】:2021-08-25 23:09:36
【问题描述】:

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

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

【问题讨论】:

  • thickness,你的最后一个参数,必须是int 类型,参见。 rectangle doc.
  • 嗨,你得到答案了吗?我也面临同样的问题。
  • 如果传递的坐标不是int,就会发生这种情况
  • 在我的情况下,pt1 和 pt2(第二和第三输入)的大负值(-2147483646、-2147483657)会导致相同的错误消息。不是很清楚的信息...

标签: python opencv


【解决方案1】:

使用 int 坐标对我来说效果很好。

(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 的值。

【讨论】:

    【解决方案2】:

    如果任何坐标不是int,就会发生TypeError: function takes exactly 4 arguments (2 given)

    考虑下面的sn-ps:

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

    上面的 sn-p 将起作用。但是下面的 sn-ps 会引发TypeError:

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

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2021-04-02
      • 1970-01-01
      • 2021-11-22
      • 2021-01-18
      • 2013-04-07
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多