Matlab,C/C++语言读取RAW图和保存RAW图

摘要

何为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图像

在这里插入图片描述
放大后可以看见
在这里插入图片描述
还能发现芯片出图后像素的排列 。

  • 9
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手写不期而遇

感谢你的打赏,也欢迎一起学习

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值