首页 > 代码库 > 使用liner、feather、multiband对已经拼接的数据进行融合(下)

使用liner、feather、multiband对已经拼接的数据进行融合(下)

 

理解mulitband。所谓的mulitband,其实就是一种多尺度的样条融合,其实现的主要方法就是laplace金字塔。
高斯金字塔是向下采样,而laplace金字塔式向上采样(也就是恢复),采用的都是差值的方法。如何能够在金字塔各个层次上面进行图像的融合,结果证明是相当不错的。网络上面流传的一个类解释了这个问题,并且能够拿来用:
// GOImage.cpp : 定义? DLL 的?初?始?化例y程。
//
 
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
using namespace cv;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define DllExport _declspec (dllexport)
 
/*
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]合?成,?最?后得?到?原-图?像?大小?的?融合?图?像?。
 
*/
 
class LaplacianBlending { 
private
                Mat_<Vec3f> top; 
                Mat_<Vec3f> down; 
                Mat_< float> blendMask; 
 
                vector<Mat_<Vec3f> > topLapPyr,downLapPyr,resultLapPyr; //Laplacian Pyramids  
                Mat topHighestLevel, downHighestLevel, resultHighestLevel; 
                vector<Mat_<Vec3f> > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGB  
 
                 int levels; 
 
                 //创建金e字?塔t
                 void buildPyramids() { 
                                 //参?数y的?解a释 top就是?top ,topLapPyr就是?top的?laplacian的?pyr,而?topHighestLevel保存?的?是?最?高?端?的?高?斯1金e字?塔t
                                buildLaplacianPyramid(top,topLapPyr,topHighestLevel);  
                                buildLaplacianPyramid(down,downLapPyr,downHighestLevel); 
                                buildGaussianPyramid(); 
                } 
 
                 //创建gauss金e字?塔t
                 void buildGaussianPyramid() {//金e字?塔t内容Y为a每?一?层?的?掩模  
                                assert(topLapPyr.size()>0); 
 
                                maskGaussianPyramid.clear(); 
                                Mat currentImg; 
                                 //blendMask就是?掩码?
                                cvtColor(blendMask, currentImg, CV_GRAY2BGR); //store color img of blend mask into maskGaussianPyramid  
                                maskGaussianPyramid.push_back(currentImg); //0-level  
 
                                currentImg = blendMask; 
                                 for (int l=1; l<levels+1; l++) { 
                                                Mat _down; 
                                                 if (topLapPyr.size() > l) 
                                                                pyrDown(currentImg, _down, topLapPyr[l].size()); 
                                                 else 
                                                                pyrDown(currentImg, _down, topHighestLevel.size()); //lowest level  
 
                                                Mat down; 
                                                cvtColor(_down, down, CV_GRAY2BGR); 
                                                maskGaussianPyramid.push_back(down); //add color blend mask into mask Pyramid  
                                                currentImg = _down; 
                                } 
                } 
 
                 //创建laplacian金e字?塔t
                 void buildLaplacianPyramid(const Mat& img, vector<Mat_<Vec3f> >& lapPyr, Mat& HighestLevel) { 
                                lapPyr.clear(); 
                                Mat currentImg = img; 
                                 for (int l=0; l<levels; l++) { 
                                                Mat down,up; 
                                                pyrDown(currentImg, down); 
                                                pyrUp(down, up,currentImg.size()); 
                                                Mat lap = currentImg - up;  //存?储的?就是?残D差?
                                                lapPyr.push_back(lap); 
                                                currentImg = down; 
                                } 
                                currentImg.copyTo(HighestLevel); 
                } 
 
                Mat_<Vec3f> reconstructImgFromLapPyramid() { 
                                 //将?左右laplacian图?像?拼成的?resultLapPyr金e字?塔t中D每?一?层?  
                                 //从上?到?下?插?值放?大并相加,?即得?blend图?像?结果?  
                                Mat currentImg = resultHighestLevel; 
                                 for (int l=levels-1; l>=0; l--) { 
                                                Mat up; 
                                                pyrUp(currentImg, up, resultLapPyr[l].size()); 
                                                currentImg = up + resultLapPyr[l]; 
                                } 
                                 return currentImg; 
                } 
 
                 void blendLapPyrs() { 
                                 //获?得?每?层?金e字?塔t中D直接用?左右两?图?Laplacian变?换?拼成的?图?像?resultLapPyr  
                                 //一?半?的?一?半?就是?在这a个?地?方?计?算?的?。 是?基于掩模的?方?式?进?行D的?.
                                resultHighestLevel = topHighestLevel.mul(maskGaussianPyramid.back()) + 
                                                downHighestLevel.mul(Scalar(1.0,1.0,1.0) - maskGaussianPyramid.back()); 
                                 for (int l=0; l<levels; l++) { 
                                                Mat A = topLapPyr[l].mul(maskGaussianPyramid[l]); 
                                                Mat antiMask = Scalar(1.0,1.0,1.0) - maskGaussianPyramid[l]; 
                                                Mat B = downLapPyr[l].mul(antiMask); 
                                                Mat_<Vec3f> blendedLevel = A + B; 
                                                resultLapPyr.push_back(blendedLevel); 
                                } 
                } 
 
public
                LaplacianBlending( const Mat_<Vec3f>& _top, const Mat_<Vec3f>& _down, const Mat_< float>& _blendMask, int _levels)://缺省?数y据Y,?使1用? LaplacianBlending lb(l,r,m,4);  
                  top(_top),down(_down),blendMask(_blendMask),levels(_levels) 
                  { 
                                  assert(_top.size() == _down.size()); 
                                  assert(_top.size() == _blendMask.size()); 
                                  buildPyramids();  //创建laplacian金e字?塔t和gauss金e字?塔t
                                  blendLapPyrs();   //将?左右金e字?塔t融合?成为a一?个?图?片?  
                  }; 
 
                  Mat_<Vec3f> blend() { 
                                  return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid  
                  } 
}; 
 
Mat_<Vec3f> LaplacianBlend( const Mat_<Vec3f>& t, const Mat_<Vec3f>& d, const Mat_< float>& m) { 
                LaplacianBlending lb(t,d,m,4); 
                 return lb.blend(); 
 
 
DllExport double aValue =http://www.mamicode.com/1.5;
DllExport int dlladd()
{
                 return 5;
}
DllExport int dlladd( int a,int b)
{
                 return a+b;
}
DllExport cv::Mat imagetest()
{
                cv::Mat image1= cv::imread( "C:\\apple.png",1);
                cv::Mat image2= cv::imread( "C:\\orange.png",1);
 
                Mat_<Vec3f> t; image1.convertTo(t,CV_32F,1.0/255.0); //Vec3f表示?有D三y个?通道,?即 l[row][column][depth]  
                Mat_<Vec3f> d; image2.convertTo(d,CV_32F,1.0/255.0); 
 
                Mat_< float> m(t.rows,d.cols,0.0);                 //将?m全?部?赋3值为a0  
                 //m(Range::all(),Range(0,m.cols/2)) = 1.0;    //原-来初?始?的?掩码?是?在这a里?!?!?
                m(Range(0,m.rows/2),Range::all())=1.0;
                Mat_<Vec3f> blend = LaplacianBlend(t,d, m); 
 
                imshow( "blended",blend); 
                 return blend;
}
需要注意的是, m(Range(0,m.rows/2),Range::all())=1.0表明了原始图像的掩码,这个掩码就是那个分界的地方