乐趣区

关于c++:c-通过-libraw-获取-raw-格式的-rgb-值

概述

获取 canon CR2 格局的 rgb 值,因为 opencv 的 imread 并不反对 CR2 格局的,所以先用 libraw 解决,而后转成 Mat 矩阵解决,最初再通过 opencv 获取 rgb 的值。

步骤

1. 初始化 libraw

    libraw_data_t *iprc = libraw_init(0);

2. 关上 CR2 文件

    QString imgSavePath("C://Users//Administrator//Desktop//test//IMG_0782.CR2");
    ret = libraw_open_file(iprc,imgSavePath.toStdString().data());

3. 解包 raw 的原始格局

    libraw_unpack(iprc);

4. 以 RGB 位图模式存储到缓存

   int check = libraw_dcraw_process(iprc); // 模仿 dcraw 解决
   libraw_processed_image_t* img = libraw_dcraw_make_mem_image(iprc, &check);

5. 应用 Mat 结构获取到的数据

    cv::Mat output = cv::Mat(cv::Size(img ->width, img ->height), CV_16UC3, img ->data, cv::Mat::AUTO_STEP);

6. 转成 IplImage 格局并将获取到的 rgb 值写入到 txt 中

    IplImage img = IplImage (output);
    CvScalar pixel;
    for (int i = 0; i < img.height; ++i)
    {for (int j = 0; j < img.width; ++j)
        {
            // 取得像素的 RGB 值并显示
            pixel = cvGet2D(&img, i, j);
            outfile << pixel.val[0] << "," << pixel.val[1] << "," << pixel.val[2];
            outfile << "\n";
            cvSet2D(&img, i, j, pixel);
        }
    }

残缺代码

int RawToRgb()
{
    int ret;
    QString imgSavePath("C://Users//Administrator//Desktop//test//IMG_0782.CR2");
    libraw_data_t *iprc = libraw_init(0);
    libraw_output_params_t p = iprc->params;

    ret = libraw_open_file(iprc,imgSavePath.toStdString().data());
    libraw_unpack(iprc);
    int check = libraw_dcraw_process(iprc);
    libraw_processed_image_t* img = libraw_dcraw_make_mem_image(iprc, &check);
    if(img != NULL)
    {ofstream outfile("C://Users//Administrator//Desktop//rgb.txt");
       cv::Mat output = cv::Mat(cv::Size(img->width, img->height), CV_16UC3, img->data, cv::Mat::AUTO_STEP);
       
       IplImage rawImg = IplImage (output);    // Mat 格局转为 IplImage 格局
       CvScalar pixel;

       for (int i = 0; i < rawImg.height; ++i)
       {for (int j = 0; j < rawImg.width; ++j)
            {
               // 取得像素的 RGB 值并显示,程序是 bgr
                pixel = cvGet2D(&rawImg, i, j);
                outfile << pixel.val[0] << "," << pixel.val[1] << "," << pixel.val[2];
                outfile << "\n";
               cvSet2D(&rawImg, i, j, pixel);
            }
       }
    }
    return 0;
}

参考资料

  • libraw-docs
  • rawpy-api
退出移动版