日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

圖像拉普拉斯金字塔融合(Laplacian Pyramid Blending)

 學(xué)海無涯GL 2012-09-11

圖像拉普拉斯金字塔融合(Laplacian Pyramid Blending)

分類: Image & Video Processing 713人閱讀 評(píng)論(12) 收藏 舉報(bào)

本文將介紹圖像金字塔以及拉普拉斯融合的相關(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.建立兩幅圖像的拉普拉斯金字塔
2.求高斯金字塔(掩模金字塔-為了拼接左右兩幅圖像)
3. 進(jìn)行拼接blendLapPyrs() ; 在每一層上將左右laplacian圖像直接拼起來得結(jié)果金字塔resultLapPyr
4.重建圖像: 從最高層結(jié)果圖
//將左右laplacian圖像拼成的resultLapPyr金字塔中每一層,從上到下插值放大并和下一層相加,即得blend圖像結(jié)果(reconstructImgFromLapPyramid)


Code:
配置環(huán)境:
VS2010+opencv 2.3.1(2.2版本以上均可)



  1. #include "opencv2/opencv.hpp"
  2. using namespace cv;
  3. /************************************************************************/
  4. /* 說明:
  5. *金字塔從下到上依次為 [0,1,...,level-1] 層
  6. *blendMask 為圖像的掩模
  7. *maskGaussianPyramid為金字塔每一層的掩模
  8. *resultLapPyr 存放每層金字塔中直接用左右兩圖Laplacian變換拼成的圖像
  9. */
  10. /************************************************************************/
  11. class LaplacianBlending {
  12. private:
  13. Mat_<Vec3f> left;
  14. Mat_<Vec3f> right;
  15. Mat_<float> blendMask;
  16. vector<Mat_<Vec3f> > leftLapPyr,rightLapPyr,resultLapPyr;//Laplacian Pyramids
  17. Mat leftHighestLevel, rightHighestLevel, resultHighestLevel;
  18. vector<Mat_<Vec3f> > maskGaussianPyramid; //masks are 3-channels for easier multiplication with RGB
  19. int levels;
  20. void buildPyramids() {
  21. buildLaplacianPyramid(left,leftLapPyr,leftHighestLevel);
  22. buildLaplacianPyramid(right,rightLapPyr,rightHighestLevel);
  23. buildGaussianPyramid();
  24. }
  25. void buildGaussianPyramid() {//金字塔內(nèi)容為每一層的掩模
  26. assert(leftLapPyr.size()>0);
  27. maskGaussianPyramid.clear();
  28. Mat currentImg;
  29. cvtColor(blendMask, currentImg, CV_GRAY2BGR);//store color img of blend mask into maskGaussianPyramid
  30. maskGaussianPyramid.push_back(currentImg); //0-level
  31. currentImg = blendMask;
  32. for (int l=1; l<levels+1; l++) {
  33. Mat _down;
  34. if (leftLapPyr.size() > l)
  35. pyrDown(currentImg, _down, leftLapPyr[l].size());
  36. else
  37. pyrDown(currentImg, _down, leftHighestLevel.size()); //lowest level
  38. Mat down;
  39. cvtColor(_down, down, CV_GRAY2BGR);
  40. maskGaussianPyramid.push_back(down);//add color blend mask into mask Pyramid
  41. currentImg = _down;
  42. }
  43. }
  44. void buildLaplacianPyramid(const Mat& img, vector<Mat_<Vec3f> >& lapPyr, Mat& HighestLevel) {
  45. lapPyr.clear();
  46. Mat currentImg = img;
  47. for (int l=0; l<levels; l++) {
  48. Mat down,up;
  49. pyrDown(currentImg, down);
  50. pyrUp(down, up,currentImg.size());
  51. Mat lap = currentImg - up;
  52. lapPyr.push_back(lap);
  53. currentImg = down;
  54. }
  55. currentImg.copyTo(HighestLevel);
  56. }
  57. Mat_<Vec3f> reconstructImgFromLapPyramid() {
  58. //將左右laplacian圖像拼成的resultLapPyr金字塔中每一層
  59. //從上到下插值放大并相加,即得blend圖像結(jié)果
  60. Mat currentImg = resultHighestLevel;
  61. for (int l=levels-1; l>=0; l--) {
  62. Mat up;
  63. pyrUp(currentImg, up, resultLapPyr[l].size());
  64. currentImg = up + resultLapPyr[l];
  65. }
  66. return currentImg;
  67. }
  68. void blendLapPyrs() {
  69. //獲得每層金字塔中直接用左右兩圖Laplacian變換拼成的圖像resultLapPyr
  70. resultHighestLevel = leftHighestLevel.mul(maskGaussianPyramid.back()) +
  71. rightHighestLevel.mul(Scalar(1.0,1.0,1.0) - maskGaussianPyramid.back());
  72. for (int l=0; l<levels; l++) {
  73. Mat A = leftLapPyr[l].mul(maskGaussianPyramid[l]);
  74. Mat antiMask = Scalar(1.0,1.0,1.0) - maskGaussianPyramid[l];
  75. Mat B = rightLapPyr[l].mul(antiMask);
  76. Mat_<Vec3f> blendedLevel = A + B;
  77. resultLapPyr.push_back(blendedLevel);
  78. }
  79. }
  80. public:
  81. 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);
  82. left(_left),right(_right),blendMask(_blendMask),levels(_levels)
  83. {
  84. assert(_left.size() == _right.size());
  85. assert(_left.size() == _blendMask.size());
  86. buildPyramids(); //construct Laplacian Pyramid and Gaussian Pyramid
  87. blendLapPyrs(); //blend left & right Pyramids into one Pyramid
  88. };
  89. Mat_<Vec3f> blend() {
  90. return reconstructImgFromLapPyramid();//reconstruct Image from Laplacian Pyramid
  91. }
  92. };
  93. Mat_<Vec3f> LaplacianBlend(const Mat_<Vec3f>& l, const Mat_<Vec3f>& r, const Mat_<float>& m) {
  94. LaplacianBlending lb(l,r,m,4);
  95. return lb.blend();
  96. }
  97. int main() {
  98. Mat l8u = imread("left.png");
  99. Mat r8u = imread("right.png");
  100. imshow("left",l8u);
  101. imshow("right",r8u);
  102. Mat_<Vec3f> l; l8u.convertTo(l,CV_32F,1.0/255.0);//Vec3f表示有三個(gè)通道,即 l[row][column][depth]
  103. Mat_<Vec3f> r; r8u.convertTo(r,CV_32F,1.0/255.0);
  104. /***************** void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;******************/
  105. /* Performs linear transformation on every source array element:
  106. dst(x,y,c) = scale*src(x,y,alpha)+beta.
  107. Arbitrary combination of input and output array depths are allowed
  108. (number of channels must be the same), thus the function can be used
  109. for type conversion */
  110. //create blend mask matrix m
  111. Mat_<float> m(l.rows,l.cols,0.0); //將m全部賦值為0
  112. m(Range::all(),Range(0,m.cols/2)) = 1.0; //取m全部行&[0,m.cols/2]列,賦值為1.0
  113. Mat_<Vec3f> blend = LaplacianBlend(l, r, m);
  114. imshow("blended",blend);
  115. waitKey(0);
  116. return 0;
  117. }



效果圖:


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多