1、bayer为一个16bit色深的RGGB拜耳阵列RAW数据对象
2、返回值为一个16bit色深的三通道数据对象
3、使用opencv的imshow显示图像时需要注意色深问题
cv::Mat bayer2rgb(cv::Mat bayer)
{
Mat res(bayer.rows, bayer.cols, CV_16UC3, Scalar(255,255,255));
//R(R,G,B)
for (int i = 0; i < res.rows; i=i+2)
for (int j = 0; j < res.cols; j = j + 2)
{
//R(R)
res.ptr<Vec3w>(i)[j][R] = bayer.ptr<ushort>(i)[j];//
if ((i > 0) && (j > 0))
{
//R(G)
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i - 1)[j] + bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i)[j - 1] + bayer.ptr<ushort>(i)[j + 1])/4;//
//R(B)
res.ptr<Vec3w>(i)[j][B] = (bayer.ptr<ushort>(i - 1)[j-1] + bayer.ptr<ushort>(i + 1)[j-1] + bayer.ptr<ushort>(i-1)[j + 1] + bayer.ptr<ushort>(i+1)[j + 1])/4;//
}
else if ((i == 0) && (j == 0))
{
//R(G)
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i)[j + 1])/2;//
//R(B)
res.ptr<Vec3w>(i)[j][B] = bayer.ptr<ushort>(i + 1)[j+1];//
}
else if (i == 0)
{
//R(G)
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i)[j-1] + bayer.ptr<ushort>(i)[j + 1])/3;//
//R(B)
res.ptr<Vec3w>(i)[j][B] = (bayer.ptr<ushort>(i + 1)[j-1] + bayer.ptr<ushort>(i+1)[j + 1])/2;//
}
else if ((j == 0))
{
//R(G)
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i-1)[j] + bayer.ptr<ushort>(i)[j + 1])/3;//
//R(B)
res.ptr<Vec3w>(i)[j][B] = (bayer.ptr<ushort>(i + 1)[j+1] + bayer.ptr<ushort>(i-1)[j + 1])/2;//
}
}
//G(R,G,B)
for (int i = 0; i < res.rows; i = i + 1)
for (int j = (i+1)%2; j < res.cols; j = j + 2)
{
//G(R)
if ((i < res.rows-1) && (j<res.cols-1) && ((i + 1) % 2))
{
res.ptr<Vec3w>(i)[j][R] = (bayer.ptr<ushort>(i)[j+1] + bayer.ptr<ushort>(i)[j-1])/2;//
}
else if ((i < res.rows - 1) && (j < res.cols - 1) && (!((i + 1) % 2)))
{
res.ptr<Vec3w>(i)[j][R] = (bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i-1)[j])/2;//
}
else if (i == res.rows-1)
{
res.ptr<Vec3w>(i)[j][R] = bayer.ptr<ushort>(i - 1)[j];//
}
else if ((j == res.cols-1))
{
res.ptr<Vec3w>(i)[j][R] = bayer.ptr<ushort>(i)[j-1];//
}
//G(G)
res.ptr<Vec3w>(i)[j][G] = bayer.ptr<ushort>(i)[j];//
//G(B)
if ((i < res.rows) && (j < res.cols) && (i > 0) && (j > 0) && ((i + 1) % 2))
{
res.ptr<Vec3w>(i)[j][B] = (bayer.ptr<ushort>(i - 1)[j] + bayer.ptr<ushort>(i + 1)[j])/2;//
}
else if ((i < res.rows) && (j < res.cols) && (i > 0) && (j > 0) && (!((i + 1) % 2)))
{
res.ptr<Vec3w>(i)[j][B] = (bayer.ptr<ushort>(i)[j+1] + bayer.ptr<ushort>(i)[j-1])/2;//
}
else if (i == 0)
{
res.ptr<Vec3w>(i)[j][B] = bayer.ptr<ushort>(i + 1)[j] ;//
}
else if (j == 0)
{
res.ptr<Vec3w>(i)[j][B] = bayer.ptr<ushort>(i)[j + 1];//
}
}
//B(R,G,B)
for (int i = 1; i < res.rows; i = i + 2)
for (int j = 1; j < res.cols; j = j + 2)
{
//B(R)
if ((i < res.rows - 1) && (j < res.cols - 1))
{
res.ptr<Vec3w>(i)[j][R] = (bayer.ptr<ushort>(i-1)[j -1] + bayer.ptr<ushort>(i+1)[j - 1]+ bayer.ptr<ushort>(i-1)[j + 1] + bayer.ptr<ushort>(i+1)[j + 1])/4;//
}
else if ((i == res.rows - 1) && (j == res.cols - 1))
{
res.ptr<Vec3w>(i)[j][R] = bayer.ptr<ushort>(i - 1)[j - 1];//
}
else if (i == res.rows - 1)
{
res.ptr<Vec3w>(i)[j][R] = (bayer.ptr<ushort>(i - 1)[j - 1] + bayer.ptr<ushort>(i - 1)[j + 1])/2;//
}
else if (j == res.cols - 1)
{
res.ptr<Vec3w>(i)[j][R] = (bayer.ptr<ushort>(i - 1)[j - 1] + bayer.ptr<ushort>(i + 1)[j - 1])/2;//
}
//B(G)
if ((i < res.rows - 1) && (j < res.cols - 1))
{
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i - 1)[j] + bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i)[j - 1] + bayer.ptr<ushort>(i)[j + 1])/4;//
}
else if ((i == res.rows - 1) && (j == res.cols - 1))
{
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i - 1)[j] + bayer.ptr<ushort>(i)[j - 1])/2;//
}
else if (i == res.rows - 1)
{
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i)[j - 1] + bayer.ptr<ushort>(i)[j + 1] + bayer.ptr<ushort>(i - 1)[j])/3;//
}
else if (j == res.cols - 1)
{
res.ptr<Vec3w>(i)[j][G] = (bayer.ptr<ushort>(i - 1)[j] + bayer.ptr<ushort>(i + 1)[j] + bayer.ptr<ushort>(i)[j -1])/3;//
}
//B(B)
res.ptr<Vec3w>(i)[j][B] = bayer.ptr<ushort>(i)[j];//
}
return res;
}