Image reconstruction using Gaussian pyramid and Laplacian pyramid (matlab code)

The main idea of ​​using Gaussian pyramid and Laplacian pyramid reconstruction is that the content of Gaussian pyramid will lose information in the process of Gaussian blurring and downsampling, and in order to reconstruct an image that is not much different from the original image, we will It is hoped that the information lost in the process of building the Gaussian pyramid can be preserved in some way. Later people thought of using the Laplacian pyramid to hold this information. Each layer in the Laplacian pyramid is a residual map of two adjacent layers in the Gaussian pyramid, that is, the information lost between the two adjacent layers.

Algorithm Brief

Take the original image as the first layer of the Gaussian pyramid, and then use Gaussian blur and downsample to obtain the second layer of the Gaussian pyramid. The third layer of the Gaussian pyramid is obtained by applying Gaussian blur and downsampling to the second layer. Others of the Gaussian pyramid Layers are also obtained through this operation. Then after getting the Gaussian pyramid, we need to find the residual between its two adjacent layers. So how to ask for it? The image of the second layer of the Gaussian gold is upsampled and Gaussian blurred to obtain an image of the same resolution as the image of the first layer of the Gaussian pyramid, and then (layer 1 - layer 2) to obtain the first layer of the Laplacian pyramid image, and then its The second layer image is obtained by (Gaussian pyramid layer 2 - Gaussian pyramid layer 3 upsampling and Gaussian blur to get the image), and so on. It should be noted that the Laplacian pyramid generally has one less layer than the Gaussian pyramid. Of course, some people put the last layer of the Gaussian pyramid into the last layer of the Laplacian pyramid, so that the two have the same number of layers, but at this time the last layer of the Laplacian pyramid is no longer a residual map , but the Gaussian blur of the original image and downsampled n times. It is fundamentally different from the other layers of Laplace's pyramid. When the Laplacian pyramid is built, all the information is preserved. Next, you can reconstruct the image, use the last layer of the Gaussian pyramid to upsample and smooth, and add the residual of the last layer of the Laplacian pyramid (note that the Laplacian pyramid mentioned here is one layer less than the Gaussian pyramid) situation), then upsample and smooth the resulting image and add the second last layer of the Laplacian pyramid, and so on and so forth, until the end of adding to the lowest layer of the Laplacian pyramid. Then the result is the reconstructed image.

clc;
clear;
close all;

img = imread('images/lena.jpg');
img = im2double(img);
figure;imshow(img);title('原始图像');

nums = 4;
Gau = cell(nums,1);
N =1;
% 原图进高斯金字塔最底层
Gau{N} = img;
% 确定高斯模糊核
a = 0.4;
h = [1/4-a/2 1/4 a 1/4 1/4-a/2];
% ------------------------生成高斯金字塔-------------------------------% 

for N = 2:nums
    temp = imfilter(Gau{N-1},h,'conv','same','replicate');
    temp = imfilter(temp,h','conv','same','replicate');
    Gau{N} = temp(1:2:end,1:2:end,:);
end
% 显示高斯金字塔 
% for N = 1:nums
%    str = sprintf('%s%d%s','高斯金字塔第 ',N,'层');
%    figure;imshow(Gau{N});title(str); 
% end

% -------------------生成拉普拉斯金字塔-----------------------------------% 
str = sprintf('%s%d','现在N = ',N);
disp(str);
Lapla = cell(nums-1,1);
% 拉普拉斯金字塔的最高层等于高斯金字塔的最高层
% 不保留这句话是因为我们可以利用高斯金字塔最高层
% Lapla{N} = Gau{N};


% 得到残差放入拉普拉斯金字塔
for index = nums-1:-1:1 
%   上采样
  temp = imresize(Gau{index+1},2,'bilinear');
%   temp = imresize(Gau{index+1},2,'nearest');
%   高斯模糊
  temp = imfilter(temp,h,'conv','same','replicate');
  temp = imfilter(temp,h','conv','same','replicate');
  Lapla{index} = Gau{index} -temp;  
end

% 显示拉普拉斯金字塔
for N = 1:nums-1
    str = sprintf('%s%d%s','拉普拉斯金字塔第 ',N,' 层');
    figure;imshow(Lapla{N});title(str);
end

% ---------------------利用拉普拉斯金字塔重构-------------------------%
str = sprintf('%s%d','重构开始,现在N = ',N);
disp(str);
% 重构的起点
img_re = Gau{N};

for index = N:-1:2
    temp = imresize(Gau{index},2,'bilinear');
%     temp = imresize(Gau{index},2,'nearest');
    temp = imfilter(temp,h,'conv','same','replicate');
    temp = imfilter(temp,h','conv','same','replicate');
    img_re = temp + Lapla{index-1}; 
end

% 计算误差
dif = sum(img_re(:)-img(:));
str =sprintf('%s%f','重构的误差为 ',dif);
disp(str);
figure;imshow(img_re);title('重构的图像');

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847478&siteId=291194637