Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly convert cv::Mat to CV_8UC1?

I googled a lot about this problem, but I can't solve it.
So, I should convert 16-bit 3-channel image to 8-bit 1-channel image. I use this binary image in cv::inpaint function.
maskBGR, which has only two colors - black and white, is my source image.
So, there is the code:

Mat mask;
maskBGR.convertTo(mask, CV_8UC1);
inpaint(image, mask, dst, 8, cv::INPAINT_TELEA);

After that my program crashed. That was wrote in command line:

OpenCV Error: Unsupported format or combination of formats (The mask must be
8-bit 1-channel image) in unknown function, file ..\..\..\src\opencv\modules\
photo\src\inpaint.cpp, line 747

In inpaint.cpp, line 747:

if( CV_MAT_TYPE(inpaint_mask->type != CV_8UC1 )
   CV_ERROR( CV_StsUnsupportedFormat, "The mask must be 8-bit 1-channel image" );

What I doing wrong?

like image 662
dmitryvodop Avatar asked Dec 11 '14 20:12

dmitryvodop


1 Answers

convertTo() only changes the type of the channels, not the number of channels.

for an 8bit, 3channel it would be:

cvtColor(maskBGR, mask, CV_BGR2GRAY);

if your maskBGR is really 16 bits, 3 channels, you need 2 steps:

maskBGR.convertTo(maskBGR, CV_8U);
cvtColor(maskBGR, mask, CV_BGR2GRAY);
like image 192
berak Avatar answered Oct 16 '22 22:10

berak