图像金字塔

2017-07-20  本文已影响649人  学而时习之_不亦说乎

图像金字塔是图像处理和计算机视觉中常用到的概念,常常用于多尺度处理领域(multiscale processing),尤其早年的图像匹配、识别等算法中都用到了图像金字塔。

高斯金字塔(Gaussian pyramid)

下图为高斯金字塔的示意图,金字塔的底层为原始图像,每向上一层则是通过高斯滤波和1/2采样得到(去掉偶数行和列)。


高斯金字塔

我们可以使用如下Matlab代码来进行得到高斯金字塔:

function [ pyr ] = gaussian_pyramid( I,nlev )
%GAUSSIAN_PYRAMID Summary of this function goes here
%   Detailed explanation goes here
pyr = cell(nlev,1);
pyr{1} = I;

filter = fspecial('gaussian');

for i=2:nlev
    % gaussian filter
    I = imfilter(I,filter,'symmetric');
    % downsample
    I = I(1:2:end,1:2:end);
    pyr{i} = I;
end
end

下图就是生成的金字塔图像


金字塔图像

高斯滤波器可以看做一个低通滤波器,那么每经过一次的高斯滤波,图像中仅能够保留某个频率值以下的频率部分,所以高斯金字塔也可以看做一个低通金字塔(每一级只保留某个频率以下的成分)。

拉普拉斯金字塔(Laplacian pyramid)

在进行高斯金字塔运算时,由于不断的进行高斯滤波和下采样,我们丢失了很多高频信号,而拉普拉斯金字塔的目的就是保存这些高频信号,保存这些高频信号所采用的方式就是保存差分图像。比如,拉普拉斯金字塔的第0层,就是原始图像和原始图像下采样(Reduce)后再次上采样(Expand)的图像的差值。

function pyr = laplacian_pyramid(I,nlev)
r = size(I,1);
c = size(I,2);
if ~exist('nlev')
    % compute the highest possible pyramid    
    nlev = floor(log(min(r,c)) / log(2));
end
% recursively build pyramid
pyr = cell(nlev,1);
filter = pyramid_filter;
J = I;
for l = 1:nlev - 1
    % apply low pass filter, and downsample
    I = downsample(J,filter);
    odd = 2*size(I) - size(J);  % for each dimension, check if the upsampled version has to be odd
    % in each level, store difference between image and upsampled low pass version
    pyr{l} = J - upsample(I,odd,filter);
    J = I; % continue with low pass image
end
pyr{nlev} = J; % the coarest level contains the residual low pass image
%下采样函数
function R = downsample(I, filter)
border_mode = 'symmetric';
% low pass, convolve with separable filter
R = imfilter(I,filter,border_mode);     %horizontal
R = imfilter(R,filter',border_mode);    %vertical
% decimate
r = size(I,1);
c = size(I,2);
R = R(1:2:r, 1:2:c, :);  
%上采样函数
function R = upsample(I,odd,filter)

% increase resolution
I = padarray(I,[1 1 0],'replicate'); % pad the image with a 1-pixel border
r = 2*size(I,1);
c = 2*size(I,2);
k = size(I,3);
R = zeros(r,c,k);
R(1:2:r, 1:2:c, :) = 4*double(I); % increase size 2 times; the padding is now 2 pixels wide,注意这里要乘以4!

% interpolate, convolve with separable filter
R = imfilter(R,filter);     %horizontal
R = imfilter(R,filter');    %vertical

% remove the border
R = R(3:r - 2 - odd(1), 3:c - 2 - odd(2), :);
%产生拉普拉斯滤波器
function f = pyramid_filter()
f = [.05, .25, .4, .25, .05];  % original [Burt and Adelson, 1983]
%f = [.0625, .25, .375, .25, .0625];  % binom-5
f = f'*f;
end

通过上面的代码,我们可以得到拉普拉斯金字塔如下所示。

拉普拉斯金字塔

由于拉普拉斯金字塔保留了高频信号,那么我们可以用它来重建原始图像。

function R = reconstruct_laplacian_pyramid(pyr)
r = size(pyr{1},1);
c = size(pyr{1},2);
nlev = length(pyr);
% start with low pass residual
R = pyr{nlev};
filter = pyramid_filter;
for l = nlev - 1 : -1 : 1
    % upsample, and add to current level
    odd = 2*size(R) - size(pyr{l});
    R = pyr{l} + upsample(R,odd,filter);
    %figure
    %imshow(R,[]);
    %imwrite(mat2gray(R),[num2str(l),'.jpg']);
end
重建的图像

需要注意的地方

上一篇下一篇

猜你喜欢

热点阅读