clime
clime

Reputation: 8885

opencv: TypeError: mask is not a numerical tuple

Hey I am getting error TypeError: mask is not a numerical tuple when trying to add scalar to a matrix while using a mask. The mask variable is printed here:

(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))

And this is the error itself:

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

This is how I get the mask:

    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)

Upvotes: 2

Views: 6321

Answers (1)

John1024
John1024

Reputation: 113994

According to the opencv documentation, mask needs to be an "8-bit single channel array". Your mask is not.

Here is a small working example of using cv2.add with a mask:

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)

Using threshold to create a mask

According to the opencv documentation, the function cv2.threshold returns a tuple, the second element of which is a mask. So, use:

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

Upvotes: 3

Related Questions