Raw图的裁剪

文章目录

  • 前言
  • 一、raw图的简介
  • 二、raw图裁剪代码
    • 1.MATLAB代码
    • 2.C代码


前言

在图像处理中会遇到对raw图裁剪的问题,以下是对raw图的简单介绍,并附上裁剪raw图的MATLAB代码和C代码。

一、raw图的简介

raw图是直接从CCD或者CMOS捕获到的光信号转化为数字信号的原始数据,未经ISP处理和压缩,raw图的存储方式详见raw图的存储方式和读取方式。Bayerpattern主要有四种,即BGGR、GBRG、RGGB和GRBG,存储方式如图1所示。raw图的裁剪要注意Bayerpattern的问题,建议行和列分别以2个像素为最小单位去裁剪,最终得到的裁剪图Bayerpattern格式不会变。以裁剪分辨率为3840X2160的raw图中间的1920X1080部分为例,MATLAB代码和C代码分别如下。
Raw图的裁剪_第1张图片
Raw图的裁剪_第2张图片
Raw图的裁剪_第3张图片
Raw图的裁剪_第4张图片
图1 raw图bayerpattern的四种格式

二、raw图裁剪代码

1.MATLAB代码

clc
clear
close all
%输入图的路径
file_path = ‘C:\Users\XXX\Desktop\1\’;
img_path_list = dir(strcat(file_path,'*.raw'));
img_num = length(img_path_list);
%原图的分辨率
width = 3840;
height = 2160;
if img_num > 0
	for i = 1:img_num
		image_name = strcat(file_path,img_path_list(i).name);
		fprintf('%d %s\n',i,image_name);
		%对名字进行分割得到输出raw图的名字,保证输出raw图和输入raw图名字一致
		A = strsplit(image_name,'.');
		A1 = A{1};
		A1_tmp=strsplit(A1,'\');
		A2 = A1_tmp{6};
		fid = fopen(imege_name,'r');
		I = fread(fid,height*width,'uint16=>double');
		fclose(fid);
		image = reshape(I,width,height);
		%裁剪图开始的横纵坐标
		xstart = width/4 + 1;
		ystart = height/4 + 1;
		%裁剪图的分辨率
		cropWidth = 1920;
		cropHeight = 1080;
		%对raw图进行裁剪
		%方式一
		image_tmp = image(xstart:xstart+cropWidth-1,ystart:ystart+cropHeight-1);
		%%方式二
		%image_tmp = imcrop(image,[ystart,xstart,cropHeight-1,cropWidth-1]);
		%输出raw图路径
		image_name2 = 'C:\Users\XXX\Desktop\1\crop\';
		image_name3 = strcat(image_name2,A2);
		fid = fopen([image_name3,'.raw'],'wb');
		fwrite(fid,image_tmp,'uint16');
		fclose(fid);
	end
end

2.C代码

#define _CRT_SECURE_NO_WARNINGS 1

#include
#include

int main()
{
	//原图分辨率
	int iWidth = 3840;
	int iHeight = 2160;
	//裁剪图开始坐标
	int ixstart = iWidth / 4;
	int iystart = iHeight / 4;
	//裁剪图分辨率
	int iCropWidth = 1920;
	int iCropHeight = 1080;
	unsigned short *pusRawData = (unsigned short *)malloc(iWidth * iHeight * sizeof(unsigned short));
	unsigned short *pusOutRawData = (unsigned short *)malloc(iCropWidth * iCropHeight * sizeof(unsigned short));
	//打开原始raw图
	FILE *pfRaw;
	char acRawName[256];
	sprintf(acRawName, "yuanshi.raw");
	pfRaw = fopen(acRawName,"r");
	if(NULL == pfRaw)
	{
		printf("open raw file error!\n");
		return -1;
	}
	fread(pusRawData, iHeight*iWidth*sizeof(unsigned short), 1, pfRaw);
	fclose(pfRaw);
	for(int i = 0; i < iCropHeight; i++)
	{
		for(int j = 0; j < iCropWidth; j++)
		{
			pusOutRawData[i*iCropWidth + j] = pusRawData[(i + iystart)*iWidth + j + ixstart];
		}
	}
	//保存裁剪的raw图
	FILE *pfOutRaw;
	char acOutRawName[256];
	sprintf(acOutRawName, "crop.raw");
	pfOutRaw = fopen(acOutRawName, "wb");
	if(NULL == pfOutRaw)
	{
		printf("open raw file error!\n");
		return -1;
	}
	fwrite(pusOutRawData, iCropHeight*iCropWidth*sizeof(unsigned short), 1, pfOutRaw);
	fclose(pfOutRaw);
	return 0;
}

你可能感兴趣的:(图像处理,matlab,图像处理,算法)