2013-07-11 50 views
1

我有一臺相機爲4個不同的拜耳通道(B,G1,G2,R)提供了4個獨立的JPEG圖像。OpenCV:合併分開的JPEG bayer通道

我想將其轉換爲彩色圖像。

我現在在做的是解壓縮jpeg,手動恢復「原始」圖像並使用cvtColor轉換爲彩色圖像。但是這太慢了。我怎麼能做得更好?

cv::Mat imgMat[4]=cv::Mat::zeros(616, 808, CV_8U); //height, width 
    for (k=0;k<4;k++) { 
     ........ 
     imgMat[k] = cv::imdecode(buffer, CV_LOAD_IMAGE_GRAYSCALE); 
    } 
    //Reconstruct the original image from the four 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); 

看來很清楚,花費更多時間解碼jpeg圖像。有人可以使用某些建議/技巧來加速此代碼?

+0

你確定你不能設置相機作爲輸出的原始拜耳圖像,沒有通道分離和沒有jpg壓縮?這可能是帶寬方面的問題,但肯定會加速隨後的細化...... – Antonio

+0

是的。我可以做到這一點,選擇不同的模式。但是,我得到的幀速率顯然要慢得多......我想用更高的幀速率工作。 –

+0

我會檢查時間真的花在哪裏。如果是在BGGR圖像重建步驟中,您有一些希望。另一方面,如果時間真的花在imdecode()中,我看不到可以對其進行優化的方式。如果BGGR圖像重建步驟是計算中大部分時間的部分,我的第一個猜測是您必須查看cvtColor的代碼並用不同類型的輸入重寫自己的bayer轉換。同時檢查你是不是花時間重新分配內存(所有這些零可以用setTo(0)改變。) – Antonio

回答

1

首先,你應該做一個配置文件,看看時間最主要的去向。也許這全是在imdecode()之間,因爲「看起來很清楚」,但你可能是錯的。

如果不是,.at<>()是有點慢(你稱它接近400萬次)。您可以通過更高效地掃描圖像來加快速度。此外,您不需要floor() - 這將避免將int轉換爲double並返回(200萬次)。這樣的事情會更快:

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++; 
     } 
    } 
} 
+0

謝謝!那太棒了。 更精確的時間的觀察,我發現了時間上的幀後: 時間讀取圖像:0.008328 時間重建圖像:0.041352 時間Debayering:0.004263 使用你的代碼次後分別爲: 時間讀取圖像:0.008466 時間重建圖像:0.008402 時間Debayering:0.004364 這是快4倍重建圖像,並節省了第二的百分之三! 我不認爲我現在可以比這更好! –