学习OpenCV——Laplacian图像融合

网上看到一个很有意思的利用opencv实现图像融合的方法:

1.设计一个mask(一半全1,一半全0),并计算level层的gaussion_mask[i];

2.计算两幅图像每一层的Laplacian[i],并与gaussion_mask[i]相乘,合成一幅result_lapacian[i];

3.对两幅图像不断求prydown,并把最高层保存在gaussion[i],与gaussion_mask[i]相乘,合成一幅result_gaussion;

4,对result_gaussion不断求pryup,每一层都与result_lapacian[i]合成,最后得到原图像大小的融合图像。

  1. #include "opencv2/opencv.hpp"  
  2. using namespace cv;  
  3.   
  4. /************************************************************************/  
  5. /* 说明: 
  6. *金字塔从下到上依次为 [0,1,...,level-1] 层 
  7. *blendMask 为图像的掩模 
  8. *maskGaussianPyramid为金字塔每一层的掩模 
  9. *resultLapPyr 存放每层金字塔中直接用左右两图Laplacian变换拼成的图像 
  10. */  
  11. /************************************************************************/  
  12.   
  13.   
  14. class LaplacianBlending {  
  15. private:  
  16.     Mat_<Vec3f> left;  
  17.     Mat_<Vec3f> right;  
  18.     Mat_<float> blendMask;  
  19.   
  20.     vector<Mat_<Vec3f> > leftLapPyr,rightLapPyr,resultLapPyr;//Laplacian Pyramids  
  21.     Mat leftHighestLevel, rightHighestLevel, resultHighestLevel;  
  22.     vector<Mat_<Vec3f> > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGB  
  23.   
  24.     int levels;  
  25.   
  26.     void buildPyramids() {  
  27.         buildLaplacianPyramid(left,leftLapPyr,leftHighestLevel);  
  28.         buildLaplacianPyramid(right,rightLapPyr,rightHighestLevel);  
  29.         buildGaussianPyramid();  
  30.     }  
  31.   
  32.     void buildGaussianPyramid() {//金字塔内容为每一层的掩模  
  33.         assert(leftLapPyr.size()>0);  
  34.   
  35.         maskGaussianPyramid.clear();  
  36.         Mat currentImg;  
  37.         cvtColor(blendMask, currentImg, CV_GRAY2BGR);//store color img of blend mask into maskGaussianPyramid  
  38.         maskGaussianPyramid.push_back(currentImg); //0-level  
  39.   
  40.         currentImg = blendMask;  
  41.         for (int l=1; l<levels+1; l++) {  
  42.             Mat _down;  
  43.             if (leftLapPyr.size() > l)  
  44.                 pyrDown(currentImg, _down, leftLapPyr[l].size());  
  45.             else  
  46.                 pyrDown(currentImg, _down, leftHighestLevel.size()); //lowest level  
  47.   
  48.             Mat down;  
  49.             cvtColor(_down, down, CV_GRAY2BGR);  
  50.             maskGaussianPyramid.push_back(down);//add color blend mask into mask Pyramid  
  51.             currentImg = _down;  
  52.         }  
  53.     }  
  54.   
  55.     void buildLaplacianPyramid(const Mat& img, vector<Mat_<Vec3f> >& lapPyr, Mat& HighestLevel) {  
  56.         lapPyr.clear();  
  57.         Mat currentImg = img;  
  58.         for (int l=0; l<levels; l++) {  
  59.             Mat down,up;  
  60.             pyrDown(currentImg, down);  
  61.             pyrUp(down, up,currentImg.size());  
  62.             Mat lap = currentImg - up;  
  63.             lapPyr.push_back(lap);  
  64.             currentImg = down;  
  65.         }  
  66.         currentImg.copyTo(HighestLevel);  
  67.     }  
  68.   
  69.     Mat_<Vec3f> reconstructImgFromLapPyramid() {  
  70.         //将左右laplacian图像拼成的resultLapPyr金字塔中每一层  
  71.         //从上到下插值放大并相加,即得blend图像结果  
  72.         Mat currentImg = resultHighestLevel;  
  73.         for (int l=levels-1; l>=0; l--) {  
  74.             Mat up;  
  75.   
  76.             pyrUp(currentImg, up, resultLapPyr[l].size());  
  77.             currentImg = up + resultLapPyr[l];  
  78.         }  
  79.         return currentImg;  
  80.     }  
  81.   
  82.     void blendLapPyrs() {  
  83.         //获得每层金字塔中直接用左右两图Laplacian变换拼成的图像resultLapPyr  
  84.         resultHighestLevel = leftHighestLevel.mul(maskGaussianPyramid.back()) +  
  85.             rightHighestLevel.mul(Scalar(1.0,1.0,1.0) - maskGaussianPyramid.back());  
  86.         for (int l=0; l<levels; l++) {  
  87.             Mat A = leftLapPyr[l].mul(maskGaussianPyramid[l]);  
  88.             Mat antiMask = Scalar(1.0,1.0,1.0) - maskGaussianPyramid[l];  
  89.             Mat B = rightLapPyr[l].mul(antiMask);  
  90.             Mat_<Vec3f> blendedLevel = A + B;  
  91.   
  92.             resultLapPyr.push_back(blendedLevel);  
  93.         }  
  94.     }  
  95.   
  96. public:  
  97.     LaplacianBlending(const Mat_<Vec3f>& _left, const Mat_<Vec3f>& _right, const Mat_<float>& _blendMask, int _levels)://construct function, used in LaplacianBlending lb(l,r,m,4);  
  98.       left(_left),right(_right),blendMask(_blendMask),levels(_levels)  
  99.       {  
  100.           assert(_left.size() == _right.size());  
  101.           assert(_left.size() == _blendMask.size());  
  102.           buildPyramids();  //construct Laplacian Pyramid and Gaussian Pyramid  
  103.           blendLapPyrs();   //blend left & right Pyramids into one Pyramid  
  104.       };  
  105.   
  106.       Mat_<Vec3f> blend() {  
  107.           return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid  
  108.       }  
  109. };  
  110.   
  111. Mat_<Vec3f> LaplacianBlend(const Mat_<Vec3f>& l, const Mat_<Vec3f>& r, const Mat_<float>& m) {  
  112.     LaplacianBlending lb(l,r,m,4);  
  113.     return lb.blend();  
  114. }  
  115.   
  116. int main() {  
  117.     Mat l8u = imread("left.png");  
  118.     Mat r8u = imread("right.png");  
  119.   
  120.     imshow("left",l8u);   
  121.     imshow("right",r8u);  
  122.   
  123.     Mat_<Vec3f> l; l8u.convertTo(l,CV_32F,1.0/255.0);//Vec3f表示有三个通道,即 l[row][column][depth]  
  124.     Mat_<Vec3f> r; r8u.convertTo(r,CV_32F,1.0/255.0);  
  125.     /*****************    void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;******************/  
  126.     /* Performs linear transformation on every source array element: 
  127.     dst(x,y,c) = scale*src(x,y,alpha)+beta. 
  128.     Arbitrary combination of input and output array depths are allowed 
  129.     (number of channels must be the same), thus the function can be used 
  130.     for type conversion */  
  131.   
  132.     //create blend mask matrix m  
  133.     Mat_<float> m(l.rows,l.cols,0.0);                 //将m全部赋值为0  
  134.     m(Range::all(),Range(0,m.cols/2)) = 1.0;    //取m全部行&[0,m.cols/2]列,赋值为1.0  
  135.   
  136.     Mat_<Vec3f> blend = LaplacianBlend(l, r, m);  
  137.     imshow("blended",blend);  
  138.   
  139.     waitKey(0);  
  140.     return 0;  
  141. }  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值