高斯金字塔、拉普拉斯金字塔实现(C++)

/*
 * @brief Sample code that shows how to generate gaussian pyramid and laplacian pyramid.
 */
#include "iostream"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include

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);
/**
 * @function main
 */
int main ( int argc, char** argv )
{
    
    const char* filename = argc >=2 ? argv[1] : "图片地址";
    // Loads an image
    Mat src = imread( samples::findFile( filename ) );
    // Vector;
    vector<Mat> p_images;
    const int layer=3; //金字塔层数
    // Check if image is loaded fine
    if(src.empty()){
        printf(" Error opening image\n");
        printf(" Program Arguments: [image_name -- lena.jpg.png] \n");
        return EXIT_FAILURE;
    }
    
    Gaussian_Pyramid(src, p_images, layer-1);
    Laplaian_Pyramid(p_images, src);
    reconstuction(layer-1);//从拉普拉斯金字塔恢复原图

}

void Gaussian_Pyramid(Mat &image, vector<Mat> &pyramid_images, int level) {
	Mat temp = image.clone();
	Mat dst;
	for (int i = 0; i < level; i++) {
		pyrDown(temp, dst);
		//imshow(format("pyramid_up_%d", i), 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]);
	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(buf, "./result/laplacian_%d.jpg", num-t+1);
            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(buf, "./result/laplacian_%d.jpg", num-t+1);
            imwrite(buf, dst);
		}
	}
}

void reconstuction(int level) {
    char buf[64];
    Mat dst = imread( "./result/laplacian_0.jpg" );
    for (int i = 0; i < level; i++) {
        pyrUp(dst, dst);
        sprintf(buf, "./result/laplacian_%d.jpg", i+1);
        Mat src = imread( buf );
        add(src,dst,dst);
    }
    imwrite("./result/origin.jpg", dst);
}




你可能感兴趣的