1.介紹
主流的圖像融合算法主要有以下幾種:
1)直接進行圖像拼接,會導致圖片之間有很明顯的界線
2)加權平均法,界線的兩側各取一定的比例來融合縫隙,速度快,但不自然
3)羽化算法,即使得圖邊緣達到朦朧的效果,效果比加權平均法好,但會導致界線處模糊
4)拉普拉斯金字塔融合,效果最好,也是本章的豬腳,主題原理可以參見:Opencv之圖像金字塔:高斯金字塔和拉普拉斯金字塔
2.算法原理
(1)首先建立兩幅圖片的高斯金字塔,然後根據高斯金字塔建立拉普拉斯金字塔,層數越高,融合效果越好
(2)建立一個mask掩膜,表示融合的位置。比如要對圖片的中間進行融合,那麼其中一張圖片所對應的掩膜圖像的左半爲1,右半爲0,另外一張圖片所對應的掩膜圖像的左半爲0,右半爲1。將此mask掩膜也建立出一個高斯金字塔,用於後面的融合。
(3)根據mask掩膜將兩幅圖像的拉普拉斯金字塔的圖像進行權值相加,其結果就生成了一個新的拉普拉斯金字塔。
(4)將兩幅圖像的高斯金字塔最高層(根據需求,你自己下采樣最小的那個)也根據相對應的mask掩膜進行權值相加
(5)第(4)所得到的最高層融合圖片與第(3)所得到新的拉普拉斯金字塔進行拉普拉斯金字塔融合算法,具體可以參見下圖
3.代碼
#include "opencv2/opencv.hpp"
#include <vector>
using namespace cv;
using namespace std;
/************************************************************************/
/* 說明:
*金字塔從下到上依次爲 [0,1,...,level-1] 層
*blendMask 爲圖像的掩模
*maskGaussianPyramid爲金字塔每一層的掩模
*resultLapPyr 存放每層金字塔中直接用左右兩圖Laplacian變換拼成的圖像
*/
/************************************************************************/
class LaplacianBlending {
private:
Mat_<Vec3f> left;
Mat_<Vec3f> right;
Mat_<float> blendMask;
vector<Mat_<Vec3f> > leftLapPyr, rightLapPyr, resultLapPyr;//Laplacian Pyramids
Mat leftHighestLevel, rightHighestLevel, resultHighestLevel;
vector<Mat_<Vec3f> > 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<levels + 1; l++) {
Mat _down;
if (leftLapPyr.size() > 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<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;
lapPyr.push_back(lap);
currentImg = down;
}
currentImg.copyTo(HighestLevel);
}
Mat_<Vec3f> 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<levels; l++) {
Mat A = leftLapPyr[l].mul(maskGaussianPyramid[l]);
Mat antiMask = Scalar(1.0, 1.0, 1.0) - maskGaussianPyramid[l];
Mat B = rightLapPyr[l].mul(antiMask);
Mat_<Vec3f> blendedLevel = A + B;
resultLapPyr.push_back(blendedLevel);
}
}
public:
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);
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_<Vec3f> blend() {
return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid
}
};
Mat_<Vec3f> LaplacianBlend(const Mat_<Vec3f>& l, const Mat_<Vec3f>& r, const Mat_<float>& 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_<Vec3f> l; l8u.convertTo(l, CV_32F, 1.0 / 255.0);//Vec3f表示有三個通道,即 l[row][column][depth]
Mat_<Vec3f> r; r8u.convertTo(r, CV_32F, 1.0 / 255.0);
//create blend mask matrix m
Mat_<float> 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_<Vec3f> blend = LaplacianBlend(l, r, m);
imshow("blended", blend);
waitKey(0);
return 0;
}
效果: