我有一台相机,它为4个不同的拜耳通道(B、G1、G2、R)提供4个分离的JPEG图像。
我想把它转换成彩色图像。
我现在正在做的是解压缩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图像需要更多的时间。有人给我一些建议/技巧可以用来加速这个代码吗?
首先,您应该做一个概要文件,看看时间主要花在哪里。也许这一切都在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++;
}
}
}