简体   繁体   English

如何正确地将cv :: Mat转换为CV_8UC1?

[英]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. 因此,我应该将16位3通道图像转换为8位1通道图像。 I use this binary image in cv::inpaint function. 我在cv::inpaint函数中使用这个二进制图像。
maskBGR , which has only two colors - black and white, is my source image. maskBGR只有两种颜色 - 黑色和白色,是我的源图像。
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: 在inpaint.cpp中,第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. convertTo()仅更改通道的类型,而不是通道的数量。

for an 8bit, 3channel it would be: 对于8位,3通道,它将是:

cvtColor(maskBGR, mask, CV_BGR2GRAY);

if your maskBGR is really 16 bits, 3 channels, you need 2 steps: 如果你的maskBGR真的是16位,3个通道,你需要2个步骤:

maskBGR.convertTo(maskBGR, CV_8U);
cvtColor(maskBGR, mask, CV_BGR2GRAY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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