图像平均融合

在师兄以前做的基础上,试着将原来的IplImage风格的变成Mat类型的

Mat LinearBlending( Mat& img1, Mat &img2)
{
     //平均融合
    Mat result=Mat::zeros(img1.rows,img1.cols,img1.type());
    int channels = result.channels();  
    int nRows = result.rows ;  
    int nCols = result.cols*channels;  
    if (img1.isContinuous()&&img2.isContinuous())  
    {  
        nCols *= nRows;  
        nRows = 1;  
    }  
    uchar *ptr1,*ptr2,*r_ptr;
    for(int i=0;i<nRows;++i)
    {
        ptr1=img1.ptr<uchar>(i);
        ptr2=img2.ptr<uchar>(i);
        r_ptr=result.ptr<uchar>(i);
        for(int j=0;j<nCols;j+=3)
        {
            int p1 = (ptr1[j])&&(ptr1[j+1])&&(ptr1[j+2]);//p1=1,灰度!=0,有值;   p1=0,灰度=0
            int p2 = (ptr2[j])&&(ptr2[j+1])&&(ptr2[j+2]);//p2=1,灰度!=0;有值;   p2=0,灰度=0
            if(p1&&p2)
            {
                r_ptr[j] = (ptr1[j]+ptr2[j])*0.5;  //两幅图像的像素均不为零,则取平均
                r_ptr[j+1] = (ptr1[j+1]+ptr2[j+1])*0.5;
                r_ptr[j+2] = (ptr1[j+2]+ptr2[j+2])*0.5;
            }
            if((p1==1)&&(p2 ==0))
            {
                r_ptr[j] = ptr1[j]; //有一幅图像的像素为零,则取不为零的像素
                r_ptr[j+1] = ptr1[j+1];
                r_ptr[j+2] = ptr1[j+2];
            }
            if((p1==0)&&(p2==1))
            {
                r_ptr[j] = ptr2[j];
                r_ptr[j+1] = ptr2[j+1];
                r_ptr[j+2] = ptr2[j+2];
            }
        }
    }

    return result;

}

考虑到大图融合时的遍历像素的时间问题,参考
http://blog.csdn.net/xiaowei_cqu/article/details/7771760

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值