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

高斯金字塔、拉普拉斯金字塔实现(C++),第1张

高斯金字塔拉普拉斯金字塔实现(C++)
#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 &pyramid_images, int level);
void Laplaian_Pyramid(vector &pyramid_images, Mat &image);
void reconstuction(int level);

int main ( int argc, char** argv )
{
    
    const char* filename = argc >=2 ? argv[1] : "图片地址";
    // Loads an image
    Mat src = imread( samples::findFile( filename ) );
    // Vector;
    vector p_images;
    const int layer=3; //金字塔层数
    // Check if image is loaded fine
    if(src.empty()){
        printf(" Error opening imagen");
        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 &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 &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);
}




欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5699426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存