本文將介紹圖像金字塔以及拉普拉斯融合的相關(guān)知識(shí)。
圖像金字塔 ================================================ 一般的的線性變換通過將一幅圖像乘以transform函數(shù)分成不同的components。離散傅里葉變換、離散余弦變換、奇異值分解 和 小波變換 都以拉普拉斯金字塔和其他獎(jiǎng)采樣變換為簡單基礎(chǔ)。 真實(shí)數(shù)字圖像包括一系列物體和特征(不同scales、orientation和角度下的lines, shapes, patterns, edges) the simple process for a pyramid with an arbitrary number of levels: 平滑圖像->將圖像進(jìn)行下采樣(常取采樣率r=2)
而獲得,同樣的操作反復(fù)做,金字塔層數(shù)逐漸上升,空間采樣密度逐漸下降。(如下圖)這個(gè)多維表示就像一個(gè)金字塔,其中fi表示圖像,li表示低通濾波結(jié)果,hi表示高通濾波結(jié)果。li
/ hi通過將圖像與高通/低通濾波器卷積而得。 ![]()
與之相反,金字塔重建通過上采樣獲得。 以圖像金字塔為基礎(chǔ)的雙邊濾波器是一個(gè)圖像細(xì)節(jié)增強(qiáng)和操作的很好的框架。
圖像融合(Image Blending) ================================================ 原理: 1.建立兩幅圖像的拉普拉斯金字塔
#include "opencv2/opencv.hpp" using namespace cv; /************************************************************************/ /* 說明: *金字塔從下到上依次為 [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() {//金字塔內(nèi)容為每一層的掩模 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圖像結(jié)果 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("left.png"); Mat r8u = imread("right.png"); imshow("left",l8u); imshow("right",r8u); Mat_<Vec3f> l; l8u.convertTo(l,CV_32F,1.0/255.0);//Vec3f表示有三個(gè)通道,即 l[row][column][depth] Mat_<Vec3f> r; r8u.convertTo(r,CV_32F,1.0/255.0); /***************** void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;******************/ /* Performs linear transformation on every source array element: dst(x,y,c) = scale*src(x,y,alpha)+beta. Arbitrary combination of input and output array depths are allowed (number of channels must be the same), thus the function can be used for type conversion */ //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; }
|
|