当前位置: 首页 > 工具软件 > LibRaw > 使用案例 >

使用libraw读取图像RAW数据返回opencv的Mat对象

柴兴修
2023-12-01

使用libraw读取图像RAW数据转返回opencv的Mat对象。

使用说明:file为RAW文件路径(包含文件名,注意反斜杠)

返回值:返回一个保存了RAW数据的Mat对象。支持索尼、佳能、宾得等主流相机的RAW。

备注:

1、iProcessor.imgdata.idata.cdesc保存了bayer阵列格式是RGB还是GRGBE还是GMCY还是GBTG

2、iProcessor.imgdata.rawdata.raw_image保存了读取到的RAW数据,需要调用unpack()之后才有效

cv::Mat raw2mat(char* file)
{
    // Let us create an image processor
    LibRaw iProcessor;

    // Open the file and read the metadata
    int ret=iProcessor.open_file(file);

    // The metadata are accessible through data fields of the class
    printf("Image size: %d x %d\n", iProcessor.imgdata.sizes.width, iProcessor.imgdata.sizes.height);

    // Let us unpack the image
    ret=iProcessor.unpack();
     cv::Mat rawData(iProcessor.imgdata.sizes.raw_height, iProcessor.imgdata.sizes.raw_width, CV_16UC1,Scalar(0));
    // And let us print its dump; the data are accessible through data fields of the class
    //rawdata in *iProcessor.imgdata.rawdata.raw_image
   for (int i = 0; i < iProcessor.imgdata.sizes.raw_height * iProcessor.imgdata.sizes.raw_width; i++)
       rawData.ptr<ushort>(i / (iProcessor.imgdata.sizes.raw_width) )[i % (iProcessor.imgdata.sizes.raw_width)] = iProcessor.imgdata.rawdata.raw_image[i];
    // Finally, let us free the image processor for work with the next image
    iProcessor.recycle();
    return rawData;
}

 类似资料: