您好,欢迎访问三七文档
Bayer转换算法[转]/*raw.h*/#ifndef__RAW_H__#define__RAW_H__#defineGP_OK1typedefenum{BAYER_TILE_RGGB=0,BAYER_TILE_GRBG=1,BAYER_TILE_BGGR=2,BAYER_TILE_GBRG=3,BAYER_TILE_RGGB_INTERLACED=4,BAYER_TILE_GRBG_INTERLACED=5,BAYER_TILE_BGGR_INTERLACED=6,BAYER_TILE_GBRG_INTERLACED=7,}BayerTile;intgp_bayer_expand(unsignedchar*input,intw,inth,unsignedchar*output,BayerTiletile);intgp_bayer_decode(unsignedchar*input,intw,inth,unsignedchar*output,BayerTiletile);intgp_bayer_interpolate(unsignedchar*image,intw,inth,BayerTiletile);#endif/*raw.c*/#includeraw.hstaticconstinttile_colours[8][4]={{0,1,1,2},{1,0,2,1},{2,1,1,0},{1,2,0,1},{0,1,1,2},{1,0,2,1},{2,1,1,0},{1,2,0,1}};#defineRED0#defineGREEN1#defineBLUE2staticintgp_bayer_accrue(unsignedchar*image,intw,inth,intx0,inty0,intx1,inty1,intx2,inty2,intx3,inty3,intcolour);intgp_bayer_expand(unsignedchar*input,intw,inth,unsignedchar*output,BayerTiletile){intx,y,i;intcolour,bayer;unsignedchar*ptr=input;switch(tile){caseBAYER_TILE_RGGB:caseBAYER_TILE_GRBG:caseBAYER_TILE_BGGR:caseBAYER_TILE_GBRG:for(y=0;yh;++y)for(x=0;xw;++x,++ptr){bayer=(x&1?0:1)+(y&1?0:2);colour=tile_colours[tile][bayer];i=(y*w+x)*3;output[i+RED]=0;output[i+GREEN]=0;output[i+BLUE]=0;output[i+colour]=*ptr;}break;caseBAYER_TILE_RGGB_INTERLACED:caseBAYER_TILE_GRBG_INTERLACED:caseBAYER_TILE_BGGR_INTERLACED:caseBAYER_TILE_GBRG_INTERLACED:for(y=0;yh;++y,ptr+=w)for(x=0;xw;++x){bayer=(x&1?0:1)+(y&1?0:2);colour=tile_colours[tile][bayer];i=(y*w+x)*3;output[i+RED]=0;output[i+GREEN]=0;output[i+BLUE]=0;output[i+colour]=(x&1)?ptr[x1]:ptr[(w1)+(x1)];}break;}return(GP_OK);}#defineAD(x,y,w)((y)*(w)*3+3*(x))intgp_bayer_interpolate(unsignedchar*image,intw,inth,BayerTiletile){intx,y,bayer;intp0,p1,p2,p3;intvalue,div;switch(tile){default:caseBAYER_TILE_RGGB:caseBAYER_TILE_RGGB_INTERLACED:p0=0;p1=1;p2=2;p3=3;break;caseBAYER_TILE_GRBG:caseBAYER_TILE_GRBG_INTERLACED:p0=1;p1=0;p2=3;p3=2;break;caseBAYER_TILE_BGGR:caseBAYER_TILE_BGGR_INTERLACED:p0=3;p1=2;p2=1;p3=0;break;caseBAYER_TILE_GBRG:caseBAYER_TILE_GBRG_INTERLACED:p0=2;p1=3;p2=0;p3=1;break;}for(y=0;yh;y++)for(x=0;xw;x++){bayer=(x&1?0:1)+(y&1?0:2);if(bayer==p0){/*red.greenlrtb,bluediagonals*/image[AD(x,y,w)+GREEN]=gp_bayer_accrue(image,w,h,x-1,y,x+1,y,x,y-1,x,y+1,GREEN);image[AD(x,y,w)+BLUE]=gp_bayer_accrue(image,w,h,x+1,y+1,x-1,y-1,x-1,y+1,x+1,y-1,BLUE);}elseif(bayer==p1){/*green.redlr,bluetb*/div=value=0;if(x(w-1)){value+=image[AD(x+1,y,w)+RED];div++;}if(x){value+=image[AD(x-1,y,w)+RED];div++;}image[AD(x,y,w)+RED]=value/div;div=value=0;if(y(h-1)){value+=image[AD(x,y+1,w)+BLUE];div++;}if(y){value+=image[AD(x,y-1,w)+BLUE];div++;}image[AD(x,y,w)+BLUE]=value/div;}elseif(bayer==p2){/*green.bluelr,redtb*/div=value=0;if(x(w-1)){value+=image[AD(x+1,y,w)+BLUE];div++;}if(x){value+=image[AD(x-1,y,w)+BLUE];div++;}image[AD(x,y,w)+BLUE]=value/div;div=value=0;if(y(h-1)){value+=image[AD(x,y+1,w)+RED];div++;}if(y){value+=image[AD(x,y-1,w)+RED];div++;}image[AD(x,y,w)+RED]=value/div;}else{/*blue.greenlrtb,reddiagonals*/image[AD(x,y,w)+GREEN]=gp_bayer_accrue(image,w,h,x-1,y,x+1,y,x,y-1,x,y+1,GREEN);image[AD(x,y,w)+RED]=gp_bayer_accrue(image,w,h,x+1,y+1,x-1,y-1,x-1,y+1,x+1,y-1,RED);}}return(GP_OK);}staticintgp_bayer_accrue(unsignedchar*image,intw,inth,intx0,inty0,intx1,inty1,intx2,inty2,intx3,inty3,intcolour){intx[4];inty[4];intvalue[4];intabove[4];intcounter;intsum_of_values;intaverage;inti;x[0]=x0;x[1]=x1;x[2]=x2;x[3]=x3;y[0]=y0;y[1]=y1;y[2]=y2;y[3]=y3;counter=sum_of_values=0;if(colour==GREEN){for(i=0;i4;i++){if((x[i]=0)&&(x[i]w)&&(y[i]=0)&&(y[i]h)){value[i]=image[AD(x[i],y[i],w)+colour];counter++;}else{value[i]=-1;}}if(counter==4){inthdiff;intvdiff;hdiff=value[1]-value[0];hdiff*=hdiff;/*Makevaluepositivebysquaring*/vdiff=value[3]-value[2];vdiff*=vdiff;/*Makevaluepositivebysquaring*/if(hdiff2*vdiff){return(value[3]+value[2])/2;}if(vdiff2*hdiff){return(value[1]+value[0])/2;}}}/*forblueandred*/counter=sum_of_values=0;for(i=0;i4;i++){if((x[i]=0)&&(x[i]w)&&(y[i]=0)&&(y[i]h)){value[i]=image[AD(x[i],y[i],w)+colour];sum_of_values+=value[i];counter++;}}average=sum_of_values/counter;if(counter4)returnaverage;/*Lessthanfoursurrounding-justtakeaverage*/counter=0;for(i=0;i4;i++){above[i]=value[i]average;if(above[i])counter++;}/*Note:counter==0indicatesallvaluesthesame*/if((counter==2)||(counter==0))returnaverage;sum_of_values=0;for(i=0;i4;i++){if((counter==3)==above[i]){sum_of_values+=value[i];}}returnsum_of_values/3;}intgp_bayer_decode(unsignedchar*input,intw,inth,unsignedchar*output,BayerTiletile){gp_bayer_expand(input,w,h,output,tile);gp_bayer_interpolate(output,w,h,tile);return(GP_OK);}Bayer图像处理Bayer是相机内部的原始图片,一般后缀名为.raw.很多软件都可以查看,比如PS.我们相机拍照下来存储在存储卡上的.jpeg或其它格式的图片,都是从.raw格式转化过来的..raw格式内部的存储方式有多种,但不管如何,都是前两行的排列不同.其格式可能如下:GRGRGRGRBGBGBGBGGRGRGRGRBGBGBGBG横为2的倍数,竖为4的倍数,它们构成了分辨率.如,上面则代表了8*4分辨率的Bayer图.我们要知道的是,G=2*R及G=2*B,即绿色值为红色值或蓝色值的两倍,因为人眼对绿色更敏感,所以绿色的分量更重.下面说一下从bayer转换成rgb图的算法,RGB图,即为三色图,一个像素点就由RGB三种颜色构成的混合色,而bayer图一个像素就只有一个颜色,或R或G或B.因为bayer一个像素点只有一种颜色,需要借助这个像素点周围的颜色对它进行插值(填充)另外的两种颜色,它本身的颜色
本文标题:Bayer转换算法
链接地址:https://www.777doc.com/doc-2900039 .html