【问题标题】:How to correctly convert cv::Mat to CV_8UC1?如何正确将 cv::Mat 转换为 CV_8UC1?
【发布时间】:2015-02-10 10:49:44
【问题描述】:

我在谷歌上搜索了很多关于这个问题的信息,但我无法解决。
所以,我应该将 16 位 3 通道图像转换为 8 位 1 通道图像。我在cv::inpaint 函数中使用这个二进制图像。
maskBGR,只有两种颜色——黑色和白色,是我的源图像。
所以,有代码:

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

在那之后我的程序崩溃了。那是在命令行中写的:

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

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

我做错了什么?

【问题讨论】:

    标签: c++ image opencv image-processing colors


    【解决方案1】:

    convertTo()只改变频道类型,不改变频道数量。

    对于 8 位 3 通道,它会是:

    cvtColor(maskBGR, mask, CV_BGR2GRAY);
    

    如果你的 maskBGR 真的是 16 位,3 个通道,你需要 2 个步骤:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2018-07-17
      • 1970-01-01
      • 2016-08-16
      • 2018-04-16
      • 2011-06-28
      • 2019-10-02
      相关资源
      最近更新 更多