获取 canon CR2 格式的 rgb 值,因为 opencv 的 imread 并不支持 CR2 格式的,所以先用 libraw 处理,然后转成 Mat 矩阵处理,最后再通过 opencv 获取 rgb 的值。
libraw_data_t *iprc = libraw_init(0);
QString imgSavePath("C://Users//Administrator//Desktop//test//IMG_0782.CR2");
ret = libraw_open_file(iprc,imgSavePath.toStdString().data());
libraw_unpack(iprc);
int check = libraw_dcraw_process(iprc); // 模拟 dcraw 处理
libraw_processed_image_t* img = libraw_dcraw_make_mem_image(iprc, &check);
cv::Mat output = cv::Mat(cv::Size(img ->width, img ->height), CV_16UC3, img ->data, cv::Mat::AUTO_STEP);
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;
}