LibRaw 是一个用来处理 RAW (CRW/CR2, NEF, RAF, DNG, and others) 格式图片的 C 库,支持各种操作系统。下面演示下如何读取raw格式的照片,这里以小米11Ultra来演示。
打开小米相机,选择专业模式,选择右上角的三条杆,选择RAW,这样小米手机就能输出dng格式的RAW文件了。
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui_c.h>
#include "libraw.h"
#define OUT RawProcessor.imgdata.params
#define OUTR RawProcessor.imgdata.rawparams
#define P1 RawProcessor.imgdata.idata
#define S RawProcessor.imgdata.sizes
#define C RawProcessor.imgdata.color
int main(int argc, char* argv[])
{
printf("Processing file %s\n", argv[arg]);
ret = RawProcessor.open_file(argv[arg]);
if (ret != LIBRAW_SUCCESS)
{
fprintf(stderr, "Cannot open %s: %s\n", argv[arg],
libraw_strerror(ret));
continue; // no recycle b/c open_file will recycle itself
}
printf("相机模式:%s, 厂商:%s.\n", P1.model, P1.make);
printf("拜尔模式: ");
putchar(P1.cdesc[RawProcessor.fcol(0, 0)]);
putchar(P1.cdesc[RawProcessor.fcol(0, 1)]);
putchar(P1.cdesc[RawProcessor.fcol(1, 0)]);
putchar(P1.cdesc[RawProcessor.fcol(1, 1)]);
putchar('\n');
std::cout << "ibraw_iparams_t----->" << P1.cdesc << std::endl; //libraw_iparams_t结构体,也就是宏定义P1, 这里P1.cdesc只是告诉用户是GMCY还是RGBG
//给后面程序判断用
char cdesc[5] = {0};
cdesc[0] = P1.cdesc[RawProcessor.fcol(0, 0)];
cdesc[1] = P1.cdesc[RawProcessor.fcol(0, 1)];
cdesc[2] = P1.cdesc[RawProcessor.fcol(1, 0)];
cdesc[3] = P1.cdesc[RawProcessor.fcol(1, 1)];
RawProcessor.raw2image();
for (int i = 0; i < RawProcessor.imgdata.sizes.iwidth * RawProcessor.imgdata.sizes.iheight; i )
{
if (i == 10)
{
break;
}
printf("i=%d R=%d G=%d B=%d G=%d\n",
i,
RawProcessor.imgdata.image[i][0],
RawProcessor.imgdata.image[i][1],
RawProcessor.imgdata.image[i][2],
RawProcessor.imgdata.image[i][3]
);
}
}
再使用RawProcessor.raw2image();之后就可以读取RAW格式的RGB格式了。我本地windows上输出结果为
打印小米的拜耳模式
打印每个像素点的值
这里说说拜耳这个天才的人物