摘要
何为raw图?
对于图像传感器而言,传感器数据每个像素点是RGGB等(排列方式有很多种),此时并不能看见颜色等信息,这种原始数据的图像,便是raw图。对于ISP算法工程师而言,查看RAW图十分重要,这里提供了一些查看RAW图的源代码。
Matlab读取RAW图
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%函数---读取原始Raw图
%Author:Zhu
%time:2022.3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function rawData = readRaw(fileName,bitsNum,width,height)
fin = fopen(fileName,'r');
switch bitsNum
case 8
disp('bits: 8');
format = sprintf('uint8=>uint8');
case 10
disp('bits: 10');
format = sprintf('uint16=>uint16');
case 12
disp('bits: 10');
format = sprintf('uint16=>uint16');
case 16
disp('bits: 16');
format = sprintf('uint16=>uint16');
end
I = fread(fin,width*height,format);
z = reshape(I,width,height);
z = z';
rawData = z;
end
主程序
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%代码---读取原始Raw图
%Author:Zhu
%time:2022.3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;
clear;
close all;
filePath = 'D:\now.raw';
bayerBits = 8;
row = 4000;
col = 3000;
rawData = readRaw(filePath,bayerBits,row,col);
figure();
imshow(rawData);
title('rawImage');
C/C++语言读取Raw图
/**************************************************
Function: ReadRawFronFile()
Description: 读取Raw图文件
Input: filePath----->文件路径
Output: filesize----->图像尺寸
return: 图像地址
**************************************************/
unsigned char* ReadRawFromFile(char *filePath,long *filesize)
{
FILE *fp;
fp = fopen(filePath,"rb");
if(fp==NULL)
{
std::cout<<"打开文件失败";
return NULL;
}
fpos_t startpos,endpos;
fseek(fp,0,SEEK_END);
fgetpos(fp,&endpos);
fseek(fp,0,SEEK_SET);
fgetpos(fp,&startpos);
long filelen = (long)(endpos-startpos);
unsigned char *bTemp = NULL;
bTemp = (unsigned char*)malloc(filelen);
if(bTemp == NULL)
{
fclose(fp);
return NULL;
}
memset(bTemp,0,filelen);
fread(bTemp,filelen,1,fp);
fclose(fp);
*filesize = filelen;
return bTemp;
}
C/C++语言保存Raw图
/**************************************************
Function: SaveRawFile()
Description: 保存Raw图文件
Input: pRawBuf--->图像地址
imageWidth,imageHeight----->图像尺寸
strSavePath--->文件保存路径
return: 状态值
**************************************************/
int SaveRawFile(unsigned char *pRawBuf,int imageWidth,int imageHeight,char *strSavePath)
{
if((pRawBuf == NULL) || (strSavePath == ""))
{
return 1;
}
FILE *fpdst = fopen(strSavePath,"wb");
if(!fpdst)
{
return 1;
}
fwrite(pRawBuf,sizeof(unsigned char),imageWidth*imageHeight,fpdst);
fclose(fpdst);
return 0;
}
主程序
int main()
{
long rawDataSize = 0;
unsigned char *pInRawBuf = NULL;
//读取RAW图
pInRawBuf = ReadRawFromFile("D:\\now.raw",&rawDataSize);
if (pInRawBuf == NULL)
{
return 0;
}
//保存Raw图
int width = 4000;
int height = 3000;
char *filePath = "E://6.raw";
SaveRawFile(pInRawBuf,width,height,filePath);
return 0;
}
Matlab读取Raw图
clc;
clear;
close all;
filePath ='E:\1.raw';
bayerFormat = 'RGGB';
width = 4208;
height= 3120;
bits = 8;
%%%读取RAW
bayerData = ReadRaw(filePath, bits, width, height);
figure();
imshow(uint8(bayerData));
title('raw image');
显示的RAW图像
放大后可以看见
还能发现芯片出图后像素的排列 。