主流的图像融合算法主要有以下几种:
1)直接进行图像拼接,会导致图片之间有很明显的界线
2)加权平均法,界线的两侧各取一定的比例来融合缝隙,速度快,但不自然
3)羽化算法,即使得图边缘达到朦胧的效果,效果比加权平均法好,但会导致界线处模糊
4)拉普拉斯金字塔融合,效果最好,也是本章的猪脚,主题原理可以参见:Opencv之图像金字塔:高斯金字塔和拉普拉斯金字塔
(1)首先建立两幅图片的高斯金字塔,然后根据高斯金字塔建立拉普拉斯金字塔,层数越高,融合效果越好
(2)建立一个mask掩膜,表示融合的位置。比如要对图片的中间进行融合,那么其中一张图片所对应的掩膜图像的左半为1,右半为0,另外一张图片所对应的掩膜图像的左半为0,右半为1。将此mask掩膜也建立出一个高斯金字塔,用于后面的融合。
(3)根据mask掩膜将两幅图像的拉普拉斯金字塔的图像进行权值相加,其结果就生成了一个新的拉普拉斯金字塔。
(4)将两幅图像的高斯金字塔最高层(根据需求,你自己下采样最小的那个)也根据相对应的mask掩膜进行权值相加
(5)第(4)所得到的最高层融合图片与第(3)所得到新的拉普拉斯金字塔进行拉普拉斯金字塔融合算法,具体可以参见下图
#include "opencv2/opencv.hpp"
#include
using namespace cv;
using namespace std;
/************************************************************************/
/* 说明:
*金字塔从下到上依次为 [0,1,...,level-1] 层
*blendMask 为图像的掩模
*maskGaussianPyramid为金字塔每一层的掩模
*resultLapPyr 存放每层金字塔中直接用左右两图Laplacian变换拼成的图像
*/
/************************************************************************/
class LaplacianBlending {
private:
Mat_ left;
Mat_ right;
Mat_ blendMask;
vector > leftLapPyr, rightLapPyr, resultLapPyr;//Laplacian Pyramids
Mat leftHighestLevel, rightHighestLevel, resultHighestLevel;
vector > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGB
int levels;
void buildPyramids() {
buildLaplacianPyramid(left, leftLapPyr, leftHighestLevel);
buildLaplacianPyramid(right, rightLapPyr, rightHighestLevel);
buildGaussianPyramid();
}
void buildGaussianPyramid() {//金字塔内容为每一层的掩模
assert(leftLapPyr.size()>0);
maskGaussianPyramid.clear();
Mat currentImg;
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 l)
pyrDown(currentImg, _down, leftLapPyr[l].size());
else
pyrDown(currentImg, _down, leftHighestLevel.size()); //lowest level
Mat down;
cvtColor(_down, down, CV_GRAY2BGR);
maskGaussianPyramid.push_back(down);//add color blend mask into mask Pyramid
currentImg = _down;
}
}
void buildLaplacianPyramid(const Mat& img, vector >& lapPyr, Mat& HighestLevel) {
lapPyr.clear();
Mat currentImg = img;
for (int l = 0; l reconstructImgFromLapPyramid() {
//将左右laplacian图像拼成的resultLapPyr金字塔中每一层
//从上到下插值放大并相加,即得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() {
//获得每层金字塔中直接用左右两图Laplacian变换拼成的图像resultLapPyr
resultHighestLevel = leftHighestLevel.mul(maskGaussianPyramid.back()) +
rightHighestLevel.mul(Scalar(1.0, 1.0, 1.0) - maskGaussianPyramid.back());
for (int l = 0; l blendedLevel = A + B;
resultLapPyr.push_back(blendedLevel);
}
}
public:
LaplacianBlending(const Mat_& _left, const Mat_& _right, const Mat_& _blendMask, int _levels) ://construct function, used in LaplacianBlending lb(l,r,m,4);
left(_left), right(_right), blendMask(_blendMask), levels(_levels)
{
assert(_left.size() == _right.size());
assert(_left.size() == _blendMask.size());
buildPyramids(); //construct Laplacian Pyramid and Gaussian Pyramid
blendLapPyrs(); //blend left & right Pyramids into one Pyramid
};
Mat_ blend() {
return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid
}
};
Mat_ LaplacianBlend(const Mat_& l, const Mat_& r, const Mat_& m) {
LaplacianBlending lb(l, r, m, 4);
return lb.blend();
}
int main() {
Mat l8u = imread("apple.png");
Mat r8u = imread("orange.png");
imshow("left", l8u);
imshow("right", r8u);
Mat_ l; l8u.convertTo(l, CV_32F, 1.0 / 255.0);//Vec3f表示有三个通道,即 l[row][column][depth]
Mat_ r; r8u.convertTo(r, CV_32F, 1.0 / 255.0);
//create blend mask matrix m
Mat_ m(l.rows, l.cols, 0.0); //将m全部赋值为0
m(Range::all(), Range(0, m.cols / 2)) = 1.0; //取m全部行&[0,m.cols/2]列,赋值为1.0
Mat_ blend = LaplacianBlend(l, r, m);
imshow("blended", blend);
waitKey(0);
return 0;
}
效果:
https://blog.csdn.net/abcjennifer/article/details/7628655