1

I am just learning OpenCV and, since I have some experience with Matlab's logical indexing, I was really interested to see the matrix method setTo. My initial attempt doesn't work though, and I can't work out why, so I'd be very grateful for your help!

I have a Mat containing image data, and want to set all values greater than 10 to zero. So, I did:

Mat not_relevant = abs(difference - frame2) > 10;

difference = difference.setTo(0, not_relevant);

This however gives me:

OpenCV Error: Assertion failed (mask.empty() || mask.type() == CV_8U) in
cv::Mat::setTo, file 
C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\core\src\copy.cpp, line 347

I have tried converting not_relevant, difference and frame2 before doing this using, e.g.:

frame2.convertTo(frame2, CV_8UC1);

but that did not fix the error, so I'm not sure what else I could try. Does anyone have any idea what might be wrong?

Thank you for your help!

noctilux
  • 763
  • 1
  • 5
  • 17

1 Answers1

1

I think the error is pretty clear.type of your mask image should be CV_8U.

so you need to convert not_relevent to grayscale.

Mat not_relevant = abs(difference - frame2) > 10;
cv::cvtColor(not_relevant, not_relevant, CV_BGR2GRAY);
difference = difference.setTo(0, not_relevant);

Why convertTo does not work here ?

CV_8U(or CV_8UC1) is type of image with one channel of uchar values.

convertTo can not change number of channels in image.

So converting image with more than one channel to CV_8U using convertTo does not work . check this answer for more detailed explanations.

Community
  • 1
  • 1
uchar
  • 2,552
  • 4
  • 29
  • 50
  • Thank you omid, it works! Thank you also for pointing out my error -- I had indeed not realised that convertTo did not work here. – noctilux Jan 16 '15 at 15:09