分为高斯金子塔和拉普拉斯金字塔。

高斯金字塔是由两次下采样的图像构成。

拉普拉斯金字塔由高斯金字塔的某一层先下采样再上采样,这两次采样做差构成拉普拉斯金字塔的一层。

图3-30.png
图3-31.png

  1. #include <opencv2\opencv.hpp>
  2. #include <iostream>
  3. using namespace cv;
  4. using namespace std;
  5. int main()
  6. {
  7. Mat img = imread("lena.png");
  8. if (img.empty())
  9. {
  10. cout << "请确认图像文件名称是否正确" << endl;
  11. return -1;
  12. }
  13. vector<Mat> Gauss, Lap; //高斯金字塔和拉普拉斯金字塔
  14. int level = 3; //高斯金字塔下采样次数
  15. Gauss.push_back(img); //将原图作为高斯金字塔的第0层
  16. //构建高斯金字塔
  17. for (int i = 0; i < level; i++)
  18. {
  19. Mat gauss;
  20. pyrDown(Gauss[i], gauss); //下采样
  21. Gauss.push_back(gauss);
  22. }
  23. //构建拉普拉斯金字塔
  24. for (int i = Gauss.size() - 1; i > 0; i--)
  25. {
  26. Mat lap, upGauss;
  27. if (i == Gauss.size() - 1) //如果是高斯金字塔中的最上面一层图像
  28. {
  29. Mat down;
  30. pyrDown(Gauss[i], down); //上采样
  31. pyrUp(down, upGauss);
  32. lap = Gauss[i] - upGauss;
  33. Lap.push_back(lap);
  34. }
  35. pyrUp(Gauss[i], upGauss);
  36. lap = Gauss[i - 1] - upGauss;
  37. Lap.push_back(lap);
  38. }
  39. //查看两个金字塔中的图像
  40. for (int i = 0; i < Gauss.size(); i++)
  41. {
  42. string name = to_string(i);
  43. imshow("G" + name, Gauss[i]);
  44. imshow("L" + name, Lap[i]);
  45. }
  46. waitKey(0);
  47. return 0;
  48. }

图3-32.png

图3-33.png