TypeError:UMat()最多接受2个参数(给定3个)

时间:2019-04-26 10:55:33

标签: python opencv

我尝试在python中创建一个新的cv UMat。该文档说,构造函数的参数有以下几种可能:

§ UMat() [4/19]
cv::UMat::UMat  (   int     rows,
int     cols,
int     type,
const Scalar &  s,
UMatUsageFlags  usageFlags = USAGE_DEFAULT 
)       
Python:
<UMat object>   =   cv.UMat(    [, usageFlags]  )
<UMat object>   =   cv.UMat(    rows, cols, type[, usageFlags]  )
<UMat object>   =   cv.UMat(    size, type[, usageFlags]    )
<UMat object>   =   cv.UMat(    rows, cols, type, s[, usageFlags]   )
<UMat object>   =   cv.UMat(    size, type, s[, usageFlags] )
<UMat object>   =   cv.UMat(    m   )
<UMat object>   =   cv.UMat(    m, rowRange[, colRange] )
<UMat object>   =   cv.UMat(    m, roi  )
<UMat object>   =   cv.UMat(    m, ranges   )

我使用了rowscolstype作为参数。这会引发TypeError: UMat() takes at most 2 arguments (3 given)

代码如下:

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
...
array255 = cv2.UMat(height, width, cv2.CV_8UC1)

1 个答案:

答案 0 :(得分:1)

问题是VideoCapture::get返回浮点值。您将此直接传递给constructor which takes integers

不幸的是,Python包装器使用了参数的数据类型来选择构造函数的多个重载中的哪个。 (失败时的错误消息往往会令人困惑)

演示(OpenCV 4.0):

>>> cv2.UMat(10.0, 12.0,  cv2.CV_8UC1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: UMat() takes at most 2 arguments (3 given)

>>> cv2.UMat(10, 12,  cv2.CV_8UC1)
<UMat 000000000573DC50>

在OpenCV 3.4.x中,错误消息略有不同,但是结果是相同的:

>>> cv2.UMat(10.0, 12.0,  cv2.CV_8UC1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: no matching UMat constructor found/supported

>>> cv2.UMat(10, 12,  cv2.CV_8UC1)
[ INFO:0] Initialize OpenCL runtime...
<cv2.UMat object at 0x0506FA90>

修复很简单,只需将widthheight转换为整数即可。

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

array255 = cv2.UMat(int(height), int(width), cv2.CV_8UC1)