图像平均融合

来源:互联网 发布:js apply原理 编辑:程序博客网 时间:2024/05/01 13:10

在师兄以前做的基础上,试着将原来的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
原创粉丝点击