Mask Dimensions from thresholding don't match?

asked 2017-02-06 13:03:53 -0500

ginister gravatar image

updated 2017-02-06 14:47:52 -0500

Hi everyone, I have the following code producing this error:

  Mat imgMasked, mask;
  threshold(backprojOut, mask, 128, 255, THRESH_BINARY);
  image.copyTo(imgMasked, mask);
OpenCV Error: Assertion failed (mask.depth() == CV_8U && (mcn == 1 || mcn == cn)) in copyTo

Can anyone provide me with some advice as to what is causing this? I've tried: mask = Mat::zeros(image.rows, image.cols, CV_8U); but that wasn't it. Thanks.

EDIT: To clarify I'm trying to take my output from back projection on a HSV image (which is grayscale) and turn it into a binary Mat so I can apply it as a mask. Also trying the following doesn't work:

  threshold(backprojOut, mask, 128, 255, THRESH_BINARY);
  mask.convertTo(mask, CV_8U);
  image.copyTo(imgMasked, mask);

EDIT 2: Mask has 3 channels and my image has 4, hence the error. Threshold is giving me a Mat I can't use. How can I get a single channel out of threshold?

edit retag flag offensive close merge delete


may be backprojout type is CV_32F then mask type will be CV_32F

LBerger gravatar imageLBerger ( 2017-02-06 14:49:08 -0500 )edit

What is the best approach to this? Should I be extracting channels from the mask or somehow ensuring it is a single channel? I'm not sure how to specify how many channels are in a Mat. The calcBackproject() method must take a HSV and hence backprojOut will have to be HSV too as it outputs the same data type.

ginister gravatar imageginister ( 2017-02-06 14:56:37 -0500 )edit

Fair point, must have got mixed up. So back project in is the image frame, but converted to HSV, therefore single channel output with same rows and cols as image frame. Therefore because I've converted to HSV I've lost the 4th channel from the original image (presumably alpha?). So how do I get down to a single channel?

ginister gravatar imageginister ( 2017-02-06 16:24:46 -0500 )edit
LBerger gravatar imageLBerger ( 2017-02-07 01:10:14 -0500 )edit