【C++】高斯金字塔和拉普拉斯金字塔原理和实现

1. 图像金字塔

``````void resize(src,dst,size,int interpolation)
//src:源图像；dst：目标图像，
//size目标图像大小，可以是指定的尺寸或者放大缩小的比例
//指定插值方式，一般有四种插值方式可供选择，默认为线性插值法
``````

2. 高斯金字塔

``````void pyrUp(InputArray src, OutputArray dst, const Size& dstsize=Size());
``````

``````void pyrDown(InputArray src, OutputArray dst, const Size& dstsize=Size());
``````

3. 拉普拉斯金字塔

C++和opencv代码实现：

``````#include "iostream"
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

void Gaussian_Pyramid(Mat &image, vector<Mat> &pyramid_images, int level);
void Laplaian_Pyramid(vector<Mat> &pyramid_images, Mat &image);
void reconstuction(int level);

int main(int argc, char** argv)
{
/*copyMakeBorder(src, src, 0, 3, 0, 3, BORDER_REPLICATE);*/
imshow("01", src);
/*resize(src, src, Size(src.cols *4, src.rows *4));*/
/*cvtColor(src, src, COLOR_BGR2GRAY);*/
vector<Mat> p_images;
const int layer = 3; //金字塔层数
Gaussian_Pyramid(src, p_images, layer - 1);
Laplaian_Pyramid(p_images, src);
reconstuction(layer - 1);//从拉普拉斯金字塔恢复原图
waitKey();
return 0;

}

void Gaussian_Pyramid(Mat &image, vector<Mat> &pyramid_images, int level) {
Mat temp = image.clone();
Mat dst;
char buf[64];
for (int i = 0; i < level; i++) {
pyrDown(temp, dst);
imshow(format("pyramid_up_%d", i), dst);
sprintf_s(buf, "./result/gaussian_%d.jpg", i);
imwrite(buf, dst);
temp = dst.clone();
pyramid_images.push_back(temp);
}
}

void Laplaian_Pyramid(vector<Mat> &pyramid_images, Mat &image) {
int num = pyramid_images.size() - 1;
imwrite("./result/laplacian_0.jpg", pyramid_images[num]);
imshow("laplacian_0.jpg", pyramid_images[num]);
for (int t = num; t > -1; t--) {
Mat dst;
char buf[64];
if (t - 1 < 0) {
pyrUp(pyramid_images[t], dst, image.size());
subtract(image, dst, dst);
// dst = dst + Scalar(127, 127, 127);
sprintf_s(buf, "./result/laplacian_%d.jpg", num - t + 1);
imshow(buf, dst);
imwrite(buf, dst);
}
else {
pyrUp(pyramid_images[t], dst, pyramid_images[t - 1].size());
subtract(pyramid_images[t - 1], dst, dst);
// dst = dst + Scalar(127, 127, 127);
sprintf_s(buf, "./result/laplacian_%d.jpg", num - t + 1);
imshow(buf, dst);
imwrite(buf, dst);
}
}
}

void reconstuction(int level) {
char buf[64];