【问题标题】:OpenCV RAW to RGB conversion resulting in incorrect elemSize and depthOpenCV RAW 到 RGB 转换导致不正确的 elemSize 和深度
【发布时间】:2020-07-17 16:50:51
【问题描述】:

使用 OpenCV RAW 到 RGB 转换 (CV_BayerBG2BGR) 时,图像显示正确,但图像基本类型不正确(elemSize & depth)。

即使将转换后的 Mat 写入文件然后加载它(下面代码中的 rgb_path),加载的 rgb 图像和转换后的 rgb 图像之间存在差异,尽管两者都显示正常。

这会导致下游问题,我将 Mat 转换为 uint8_t*,因为转换后的 rgb 图像中的缓冲区大小较大。

这是转换本身的问题还是我对转换/OpenCV 基本数据类型的理解?我正在使用 OpenCV 341。

int main() {

    Mat img = imread(rgb_path);

    ifstream ifd(raw_path, ios::binary | ios::ate);
    int size = ifd.tellg();
    ifd.seekg(0, ios::beg);
    vector<char> buffer;
    buffer.resize(size);
    ifd.read(buffer.data(), size);

    Mat rgb_image;
    Mat raw_image(600, 800, CV_16UC1, buffer.data());

    cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);

    cout << "elemSize() orig: " << img.elemSize() << endl;
    cout << "elemSize() conv: " << rgb_image.elemSize() << endl;
    cout << "channels() conv: " << rgb_image.channels() << endl;
    cout << "channels() orig: " << img.channels() << endl;
    cout << "depth() conv: " << rgb_image.depth() << endl;
    cout << "depth() orig: " << img.depth() << endl;
    cout << "total() orig: " << img.total() << endl;
    cout << "total() conv: " << rgb_image.total() << endl;

    return 0;

}

Output:
elemSize() orig: 3
elemSize() conv: 6
channels() conv: 3
channels() orig: 3
depth() conv: 2
depth() orig: 0

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

    与转换图像的位数相关的问题仍然是 16 位格式,而我期望是 8 位。通过添加解决:

    Mat rgb_image8u;
    rgb_image.convertTo(rgb_image8u, CV_8UC3, 1.0/255);
    

    行后:

    cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);
    

    【讨论】:

      猜你喜欢
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多