Mat拜耳数据邻域转换算法返回Mat对象,16bit三通道

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值