Raw interpolation RGB 演算法代碼[原創]
轉載請註明出處!1.關於二維數組和三維數組,在C語言里數組退化為指針會存在bug,所以使用指針定址與解引用。2.採用相鄰像素的bayer channel插值演算法,最終將8bitRaw轉換為24bitRaw數據,以便被進一步處理。#define RAW_HORIZION 2592#define RAW_VERTICAL 1944//the layout can not be modified, RGB color mode#define RED 0#define GREEN 1#define BLUE 2char g_raw_array[RAW_VERTICAL][RAW_HORIZION] = {0};char g_rgb_array[RAW_VERTICAL][RAW_HORIZION][3] = {0};bool raw_interpolation(char *raw_array, char *rgb_array, unsigned int piexl_horizion, unsigned int pixel_vertical){ unsigned int x, y; //row loop for(y=0; y<pixel_vertical; y++){ //column loop for(x=0; x<piexl_horizion; x++){ //RGB channel if(y%2){//this odd row, first piexl is green if(x%2){//this piexl is red //rgb_array[y][x][RED] = raw_array[y][x]; *(rgb_array+y*piexl_horizion*3+x*3+RED) = *(raw_array+y*piexl_horizion+x); //rgb_array[y][x][GREEN] = (raw_array[y-1][x] + raw_array[y+1][x] + raw_array[y][x-1] + raw_array[y][x+1])/4; *(rgb_array+y*piexl_horizion*3+x*3+GREEN) = (*(raw_array+(y-1)*piexl_horizion+x)+*(raw_array+(y+1)*piexl_horizion+x)+*(raw_array+y*piexl_horizion+(x-1))+*(raw_array+y*piexl_horizion+(x+1)))/4; //rgb_array[y][x][BLUE] = (raw_array[y-1][x-1] + raw_array[y-1][x+1] + raw_array[y+1][x-1] + raw_array[y+1][x+1])/4; *(rgb_array+y*piexl_horizion*3+x*3+BLUE) = (*(raw_array+(y-1)*piexl_horizion+(x-1))+*(raw_array+(y-1)*piexl_horizion+(x+1))+*(raw_array+(y+1)*piexl_horizion+(x-1))+*(raw_array+(y+1)*piexl_horizion+(x+1)))/4; }else{//this piexl is green_odd //rgb_array[y][x][RED] = (raw_array[y][x-1] + raw_array[y][x+1])/2; *(rgb_array+y*piexl_horizion*3+x*3+RED) = (*(raw_array+y*piexl_horizion+(x-1))+*(raw_array+y*piexl_horizion+(x+1)))/2; //rgb_array[y][x][GREEN] = raw_array[y][x]; *(rgb_array+y*piexl_horizion*3+x*3+GREEN) = *(raw_array+y*piexl_horizion+x); //rgb_array[y][x][BLUE] = (raw_array[y-1][x] + raw_array[y+1][x])/2; *(rgb_array+y*piexl_horizion*3+x*3+BLUE) = (*(raw_array+(y-1)*piexl_horizion+x) + *(raw_array+(y+1)*piexl_horizion+x))/2; } }else{//this even row, first piexl is blue if(x%2){//this piexl is green_even //rgb_array[y][x][RED] = (raw_array[y-1][x] + raw_array[y+1][x])/2; *(rgb_array+y*piexl_horizion*3+x*3+RED) = (*(raw_array+(y-1)*piexl_horizion+x) + *(raw_array+(y+1)*piexl_horizion+x))/2; //rgb_array[y][x][GREEN] = raw_array[y][x]; *(rgb_array+y*piexl_horizion*3+x*3+GREEN) = *(raw_array+y*piexl_horizion+x); //rgb_array[y][x][BLUE] = (raw_array[y][x-1] + raw_array[y][x+1])/2; *(rgb_array+y*piexl_horizion*3+x*3+BLUE) = (*(raw_array+y*piexl_horizion+(x-1))+*(raw_array+y*piexl_horizion+(x+1)))/2; }else{//this piexl is blue //rgb_array[y][x][RED] = (raw_array[y-1][x-1] + raw_array[y-1][x+1] + raw_array[y+1][x-1] + raw_array[y+1][x+1])/4; *(rgb_array+y*piexl_horizion*3+x*3+RED) = (*(raw_array+(y-1)*piexl_horizion+(x-1))+*(raw_array+(y-1)*piexl_horizion+(x+1))+*(raw_array+(y+1)*piexl_horizion+(x-1))+*(raw_array+(y+1)*piexl_horizion+(x+1)))/4; //rgb_array[y][x][GREEN] = (raw_array[y-1][x] + raw_array[y+1][x] + raw_array[y][x-1] + raw_array[y][x+1])/4; *(rgb_array+y*piexl_horizion*3+x*3+GREEN) = (*(raw_array+(y-1)*piexl_horizion+x)+*(raw_array+(y+1)*piexl_horizion+x)+*(raw_array+y*piexl_horizion+(x-1))+*(raw_array+y*piexl_horizion+(x+1)))/4; //rgb_array[y][x][BLUE] = raw_array[y][x]; *(rgb_array+y*piexl_horizion*3+x*3+BLUE) = *(raw_array+y*piexl_horizion+x); } } } } return true;}
推薦閱讀:
※數學不好的人適合學編程
※簡化深度學習實踐流程:新鮮出爐的TensorFlow項目模板來了
※【知了堂帶你轉行python】python零基礎學習路線圖
※INCA二次開發-INCACOM