OpenCV: Merge separated JPEG bayer channels -


i have camera giving 4 separated jpeg images 4 different bayer channels (b,g1,g2,r).

i want transform in colour image.

what i'm doing @ moment uncompress jpeg, restore "original" image manually , converting colour image using cvtcolor. slow. how better?

    cv::mat imgmat[4]=cv::mat::zeros(616, 808, cv_8u); //height, width     (k=0;k<4;k++) {         ........         imgmat[k] = cv::imdecode(buffer, cv_load_image_grayscale);     }     //reconstruct original image 4 channels! rggb     cv::mat reconstructed=cv::mat::zeros(1232, 1616, cv_8u);     int x,y;     for(x=0;x<1616;x++){         for(y=0;y<1232;y++){             if(y%2==0){                 if(x%2==0){                     //r                     reconstructed.at<uint8_t>(y,x)=imgmat[0].at<uint8_t>(y/2,x/2);                 }                 else{                     //g1                     reconstructed.at<uint8_t>(y,x)=imgmat[1].at<uint8_t>(y/2,floor(x/2));                 }             }             else{                 if(x%2==0){                     //g2                     reconstructed.at<uint8_t>(y,x)=imgmat[2].at<uint8_t>(floor(y/2),x/2);                 }                 else{                     //b                     reconstructed.at<uint8_t>(y,x)=imgmat[3].at<uint8_t>(floor(y/2),floor(x/2));                 }             }         }     }     //debayer     cv::mat reconstructedcolor;     cv::cvtcolor(reconstructed, reconstructedcolor, cv_bayerbg2bgr); 

it seems clear takes more time decoding jpeg images. has advice/trick use speed code?

firstly should profile see time going. maybe in imdecode(), "seems clear", might wrong.

if not, .at<>() bit slow (and calling 4 million times). can speedup more efficent scanning of image. not need floor() - avoid converting int double , again (2 million times). faster:

int x , y; for(y = 0; y < 1232; y++){     uint8_t* row = reconstructed.ptr<uint8_t>(y);     if(y % 2 == 0){         uint8_t* i0 = imgmat[0].ptr<uint8_t>(y / 2);         uint8_t* i1 = imgmat[1].ptr<uint8_t>(y / 2);          for(x = 0; x < 1616; ){             //r             row[x] = i0[x / 2];             x++;              //g1             row[x] = i1[x / 2];             x++;         }     }     else {         uint8_t* i2 = imgmat[2].ptr<uint8_t>(y / 2);         uint8_t* i3 = imgmat[3].ptr<uint8_t>(y / 2);          for(x = 0; x < 1616; ){             //g2             row[x] = i2[x / 2];             x++;              //b             row[x] = i3[x / 2];             x++;         }     } } 

Comments