opencv图像融合
以下内容转自:
Opencv之图像融合_时光碎了天的博客-CSDN博客_opencv图像融合
1.介绍
主流的图像融合算法主要有以下几种:
1)直接进行图像拼接,会导致图片之间有很明显的界线
2)加权平均法,界线的两侧各取一定的比例来融合缝隙,速度快,但不自然
3)羽化算法,即使得图边缘达到朦胧的效果,效果比加权平均法好,但会导致界线处模糊
4)拉普拉斯金字塔融合,效果最好,也是本章的猪脚,主题原理可以参见:Opencv之图像金字塔:高斯金字塔和拉普拉斯金字塔
2.算法原理
(1)首先建立两幅图片的高斯金字塔,然后根据高斯金字塔建立拉普拉斯金字塔,层数越高,融合效果越好
(2)建立一个mask掩膜,表示融合的位置。比如要对图片的中间进行融合,那么其中一张图片所对应的掩膜图像的左半为1,右半为0,另外一张图片所对应的掩膜图像的左半为0,右半为1。将此mask掩膜也建立出一个高斯金字塔,用于后面的融合。
(3)根据mask掩膜将两幅图像的拉普拉斯金字塔的图像进行权值相加,其结果就生成了一个新的拉普拉斯金字塔。
(4)将两幅图像的高斯金字塔最高层(根据需求,你自己下采样最小的那个)也根据相对应的mask掩膜进行权值相加
(5)第(4)所得到的最高层融合图片与第(3)所得到新的拉普拉斯金字塔进行拉普拉斯金字塔融合算法,具体可以参见下图
opencv4x版本测试ok:
这个代码是个左右融合的效果代码:
-
#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, COLOR_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, COLOR_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("11.png");
-
Mat r8u = imread("22.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;
-
}
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/124506451
- 点赞
- 收藏
- 关注作者
评论(0)