opencv:TypeError:mask不是数字元组

时间:2014-02-23 20:53:36

标签: python opencv numpy

嘿我在使用掩码时尝试将标量添加到矩阵时收到错误TypeError: mask is not a numerical tuple。掩码变量在此处打印:

(15.0, array([[  0, 255,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0, 255, ...,   0,   0,   0],
       ..., 
       [255, 255, 255, ...,   0,   0,   0],
       [255, 255, 255, ...,   0,   0,   0],
       [  0, 255, 255, ...,   0,   0,   0]], dtype=uint8))

这就是错误本身:

Traceback (most recent call last):
  File "/home/clime/mrak/motionpaint/motion_painter.py", line 97, in process_frame
    self.alphas = cv2.add(self.alphas, self.alpha_increment, mask=mask)
TypeError: mask is not a numerical tuple

这是我获得面具的方式:

    diff = cv2.absdiff(self.prevFrame, smoothedFrame)

    # convert difference to grayscale.
    greyDiff = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)

    # grayscale to black and white (i.e. false and true)
    mask = cv2.threshold(greyDiff, self.threshold, 255, cv2.THRESH_BINARY)

1 个答案:

答案 0 :(得分:3)

根据opencv documentationmask需要是“8位单通道数组”。你的面具不是。

这是一个使用带掩码的cv2.add的小例子:

In [43]: import cv2, numpy
In [44]: src1 = numpy.ones((5, 5), dtype=numpy.uint8)
In [45]: src2 = numpy.ones((5, 5), dtype=numpy.uint8)
In [46]: mask = numpy.ones((5, 5), dtype=numpy.uint8)
In [47]: mask[0, 0] = 0
In [48]: cv2.add(src1, src2, None, mask )
Out[48]: 
array([[0, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2],
       [2, 2, 2, 2, 2]], dtype=uint8)

使用阈值创建掩码

根据opencv documentation,函数cv2.threshold返回一个元组,其中第二个元素是一个掩码。所以,使用:

retval, mask = cv2.threshold(src1, 5, 255, cv2.THRESH_BINARY)