简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM