RAW转换成RGB
clc; clear; close all; [filename,pathname]=uigetfile({'*.*','All Files (*.*)'},'Pick a file'); file = strcat(pathname,filename); % raw图的尺寸 row = 30; col = 72; % 读图 所读raw图为8位 fid=fopen(file, 'r'); Bayer=fread(fid,[row,col],'uint8'); fclose(fid); % 转换到RGB空间 RGB_Pro=raw2rgb(Bayer,row,col); % 转换为RGB图像 % 显示 figure;imshow(RGB_Pro); YUVimg = rgb2ycbcr(RGB_Pro); %%% rgb -> yuv figure;imshow((YUVimg));
=================================================================================
function RGB_Pro=raw2rgb(Bayer,row,col) % 从bayer图像,用shift方法恢复RGB图像 RGB_Pro=zeros(col,row,'uint8'); for m=1 : 2 :row for n=1:2:col RGB_Pro(n,m,3)=Bayer(m+1,n); RGB_Pro(n,m,2)=Bayer(m,n)/2+Bayer(m+1,n+1)/2; RGB_Pro(n,m,1)=Bayer(m,n+1); end end for m=1 : 2 :row for n=2:2:col-1 RGB_Pro(n,m,3)=Bayer(m+1,n+1); RGB_Pro(n,m,2)=Bayer(m+1,n)/2+Bayer(m,n+1)/2; RGB_Pro(n,m,1)=Bayer(m,n); end end for m=2 : 2 :row-1 for n=1:2:col RGB_Pro(n,m,3)=Bayer(m,n); RGB_Pro(n,m,2)=Bayer(m+1,n)/2+Bayer(m,n+1)/2; RGB_Pro(n,m,1)=Bayer(m+1,n+1); end end for m=2 : 2 :row-1 for n=2:2:col-1 RGB_Pro(n,m,3)=Bayer(m,n+1); RGB_Pro(n,m,2)=Bayer(m,n)/2+Bayer(m+1,n+1)/2; RGB_Pro(n,m,1)=Bayer(m+1,n); end end