实验四
实验题目
Adaptive Filter andPyramid
- Below is a small region of an image consisting of a noisy step edge.
(The original image had value=30 on the left of the step, and
value=10 to the right of the step.) It was corrupted by additive
Gaussian noise with zero mean and variance = 6.5.
32 25 29 30 27 13 14 16 12 7
28 31 32 31 28 11 7 12 6 11
32 28 30 33 31 10 11 8 14 12
27 28 33 32 27 11 12 12 6 12
32 30 29 29 31 13 9 12 13 10
At the three points indicated by the boxes, compute the value of the adaptive filter
shown in Equation 5.3-12 in the textbook. (The size of the filter is 5x5.)You should
give the results of local mean, local var and filter value on these three points.
(Hint: The equation for the adaptive filter is in Equation 5.3-12 in the textbook. We
compute the local mean and variance in a 5x5 window surrounding each of the three
points of interest. You can do this by writing a small piece of Matlab code.) - Use Matlab’s random number generator to generate a 4x4 image
of numbers ranging from 1…16. Use the Matlab commands below to
generate the matrix. Construct an approximation pyramid for this
image, using 2x2 block neighborhood averaging (don’t round off the
results to integer). Then find the corresponding residual pyramid,
assuming that the interpolation filter implements pixel replication.
rng(‘default’); % reset random number generator
I0 = randi(16,4); % create 4x4 matrix, 1…16
(Hint: imfilter and imresize)
You should show:
(Hint: imfilter and imresize)
You should show:
- the original “image” (4x4),
- the complete pyramid,
- the predication residual at level 0,
- the predication residual at level 1,
- the complete prediction residual pyramid
实验目的
本实验旨在探索自适应滤波器和金字塔的应用。
实验分析
在第一部分中,我们将计算一个小图像区域上的自适应滤波器的值。该图像区域是一个包含噪声阶跃边缘的图像。我们将使用一个5x5的滤波器,并计算三个感兴趣点的局部均值、局部方差和滤波器值。
在第二部分中,我们将使用Matlab的随机数生成器生成一个4x4的图像,该图像的数值范围为1到16。我们将使用2x2块邻域平均的方法构建一个近似金字塔,并根据像素复制的插值滤波器计算相应的残差金字塔。
实验过程
在第一部分中,我们可以通过编写一个小段的Matlab代码来计算三个感兴趣点的局部均值、局部方差和滤波器值。首先,我们需要定义一个5x5的滤波器,然后在每个感兴趣点的周围5x5的窗口中计算局部均值和局部方差。最后,根据Equation 5.3-12中的自适应滤波器公式,计算滤波器值。
% Define the input image
I = [32 25 29 30 27 13 14 16 12 7;
28 31 32 31 28 11 7 12 6 11;
32 28 30 33 31 10 11 8 14 12;
27 28 33 32 27 11 12 12 6 12;
32 30 29 29 31 13 9 12 13 10];
% Define the filter size
M = 5;
N = 5;
% Define the noise variance
noise = 6.5;
% Define the points of interest
P = [2 2; 3 3; 4 4];
% Initialize the output variables
local_mean = zeros(size(P,1),1);
local_var = zeros(size(P,1),1);
filter_value = zeros(size(P,1),1);
% Loop over the points of interest
for i = 1:size(P,1)
% Get the row and column indices of the point
r = P(i,1);
c = P(i,2);
% Get the local neighborhood of the point
W = I(r-M/2:r+M/2, c-N/2:c+N/2);
% Compute the local mean and variance
local_mean(i) = mean(W(:));
local_var(i) = var(W(:));
% Compute the filter value using Equation 5.3-12
filter_value(i) = local_mean(i) + (local_var(i) - noise) / local_var(i) * (I(r,c) - local_mean(i));
end
% Display the results
disp('Local mean:')
disp(local_mean)
disp('Local variance:')
disp(local_var)
disp('Filter value:')
disp(filter_value)
% Input data
g = [
32 25 29 30 27 13 14 16 12 7;
28 31 32 31 28 11 7 12 6 11;
32 28 30 33 31 10 11 8 14 12;
27 28 33 32 27 11 12 12 6 12;
32 30 29 29 31 13 9 12 13 10 ];
n = 2; % size of filter is n-2:n+2
r = 3; c = 3; % Point about which to compute
gsub = g(r-n:r+n,c-n:c+n);
mL = mean2(gsub); % Local mean
vN=6.5;
vL = var(gsub(:)); % Local variance
f = g(r,c) - (vN/vL)*(g(r,c) - mL);
fprintf('At point (r,c)=(%d,%d), local mean = %f, local var = %f, filter value = %f\n', ...
r,c, mL, vL, f);
r = 3; c = 8; % Point about which to compute
gsub = g(r-n:r+n,c-n:c+n);
mL = mean2(gsub); % Local mean
vL = var(gsub(:)); % Local variance
f = g(r,c) - (vN/vL)*(g(r,c) - mL);
fprintf('At point (r,c)=(%d,%d), local mean = %f, local var = %f, filter value = %f\n', ...
r,c, mL, vL, f);
r = 3; c = 6; % Point about which to compute
gsub = g(r-n:r+n,c-n:c+n);
mL = mean2(gsub); % Local mean
vL = var(gsub(:)); % Local variance
f = g(r,c) - (vN/vL)*(g(r,c) - mL);
fprintf('At point (r,c)=(%d,%d), local mean = %f, local var = %f, filter value = %f\n', ...
r,c, mL, vL, f);
- 输出结果
在第二部分中,我们可以使用Matlab的随机数生成器来生成一个4x4的图像。然后,使用imfilter和imresize函数来构建一个近似金字塔和相应的残差金字塔。最后,我们可以使用imshow函数来展示原始图像、完整的金字塔、预测残差和完整的预测残差金字塔。
clear all
close all
rng('default'); % reset random number generator
I0 = randi(16,4); % create 4x4 matrix, 1..16
% Create level 1: average 2x2 blocks of level 0 and subsample
I1 = imfilter(I0,ones(2,2)/4);
I1 = I1(1:2:end, 1:2:end)
% Create level 2: average 2x2 blocks of level 1 and subsample
I2 = imfilter(I1,ones(2,2)/4);
I2 = I2(1:2:end, 1:2:end)
% Create prediction at level 1 by replicating 2x2 blocks of level 2
I1p = imresize(I2,2,'nearest')
% Residual at level 1
R1 = I1 - I1p
% Create prediction at level 0 by replicating 2x2 blocks of level 1
I0p = imresize(I1,2,'nearest')
% Residual at level 0
R0 = I0 - I0p
实验心得
本实验通过自适应滤波器和金字塔的应用,让我们更深入地了解了图像处理中的一些基本概念和技术。通过编写Matlab代码,我们可以实现自适应滤波器和金字塔的计算和构建,从而对图像进行降噪和分析。这些技术在图像处理领域具有广泛的应用,对于改善图像质量和提取图像特征非常有帮助。