M函数dftuv,提供了距离计算及其他类似应用所需要的网格数组。
function [U, V] = dftuv(M, N)
%DFTUV Computes meshgrid frequency matrices.
% [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and
% V. U and V are useful for computing frequency-domain filter
% functions that can be used with DFTFILT. U and V are both
% M-by-N.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.3 $ $Date: 2003/04/16 22:30:34 $
% Set up range of variables.
u = 0:(M - 1);
v = 0:(N - 1);
% Compute the indices for use in meshgrid.
idx = find(u > M/2);
u(idx) = u(idx) - M;
idy = find(v > N/2);
v(idy) = v(idy) - N;
% Compute the meshgrid arrays.
[V, U] = meshgrid(v, u);
代码示例
f = imread('pattern.tif');
PQ = paddedsize(size(f));
[U,V] = dftuv(PQ(1),PQ(2));
D0 = 0.05*PQ(2);
F = fft2(f,PQ(1),PQ(2));
H = exp(-(U.^2+V.^2)/(2*(D0^2))); %低通高斯滤波器
g = dftfilt(f,H);
subplot(2,2,1);imshow(f);title('原图像');
subplot(2,2,2);imshow(fftshift(H),[ ]);title('高斯低通滤波器');
subplot(2,2,3);imshow(log(1+abs(fftshift(F))),[ ]);title('原图像的频谱');
subplot(2,2,4);imshow(g,[ ]);title('处理后图像(比原图像模糊)');
运行结果
函数lpfilter用于生成低通滤波器的传递函数:
function H = lpfilter(type, M, N, D0, n)
%LPFILTER Computes frequency domain lowpass filters.
% H = LPFILTER(TYPE, M, N, D0, n) creates the transfer function of
% a lowpass filter, H, of the specified TYPE and size (M-by-N). To
% view the filter as an image or mesh plot, it should be centered
% using H = fftshift(H).
%
% Valid values for TYPE, D0, and n are:
%
% 'ideal' Ideal lowpass filter with cutoff frequency D0. n need
% not be supplied. D0 must be positive.
%
% 'btw' Butterworth lowpass filter of order n, and cutoff
% D0. The default value for n is 1.0. D0 must be
% positive.
%
% 'gaussian' Gaussian lowpass filter with cutoff (standard
% deviation) D0. n need not be supplied. D0 must be
% positive.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.8 $ $Date: 2004/11/04 22:33:16 $
% Use function dftuv to set up the meshgrid arrays needed for
% computing the required distances.
[U, V] = dftuv(M, N);
% Compute the distances D(U, V).
D = sqrt(U.^2 + V.^2);
% Begin filter computations.
switch type
case 'ideal' %理想低通滤波器
H = double(D <= D0);
case 'btw' %巴特沃兹低通滤波器
if nargin == 4
n = 1;
end
H = 1./(1 + (D./D0).^(2*n));
case 'gaussian' %高斯低通滤波器
H = exp(-(D.^2)./(2*(D0^2)));
otherwise
error('Unknown filter type.')
end
绘制低通滤波器的线框图及表面图
% 绘制线框图
H = fftshift(lpfilter('gaussian',500,500,50)); % 高斯低通滤波器
mesh(H(1:10:500,1:10:500)); % 绘制线框图
axis([0 50 0 50 0 1]); % 坐标轴
colormap([0 0 0]); % 通过彩色绘制网线
axis on; % 打开网格,axis off关闭
grid on; % 打开坐标轴,grid off关闭
view(-25,30); % 查看点(观察者)位置
% 绘制表面图
H = fftshift(lpfilter('gaussian',500,500,50)); % 高斯低通滤波器
surf(H(1:10:500,1:10:500)); % 绘制表面图
axis([0 50 0 50 0 1]); % 坐标轴
colormap(gray); % 通过彩色绘制网线
axis pff; % 关闭网格
grid off; % 关闭坐标轴
高通滤波通过削弱傅里叶变换的低频而保持高频相对不变,会使图像变得更加清晰(锐化)。
函数hpfilter用于生成低通滤波器的传递函数:
function H = hpfilter(type, M, N, D0, n)
%HPFILTER Computes frequency domain highpass filters.
if nargin == 4
n = 1; % Default value of n.
end
% Generate highpass filter.
Hlp = lpfilter(type, M, N, D0, n);
H = 1 - Hlp;
代码示例
H = fftshift(hpfilter('ideal',500,500,50));
mesh(H(1:10:500,1:10:500));
axis([0 50 0 50 0 1]);
colormap([0 0 0]);
axis off;
grid off;
title('理想高通滤波器的透视图');
figure,imshow(H,[ ]);
title('理想高通滤波器相应图像');
运行结果
代码示例
f = imread('pattern.tif');
PQ = paddedsize(size(f));
D0 = 0.05* PQ(1);
H = hpfilter('gaussian',PQ(1),PQ(2),D0);
g = dftfilt(f,H);
subplot(1,2,1);imshow(f);title('原图像');
subplot(1,2,2);imshow(g,[ ]);title('高斯高通滤波后的结果');
运行结果
高频强调滤波
代码示例
PQ = paddedsize(size(f));
D0 = 0.05*PQ(1);
HBW = hpfilter('btw',PQ(1),PQ(2),D0,2);
H = 0.5 + 2 * HBW;
gbw = dftfilt(f,HBW);
gbw = gscale(gbw);
ghf = dftfilt(f,H);
ghf = gscale(ghf);
ghe = histeq(ghf,256);
subplot(2,2,1);imshow(f);title('原图像');
subplot(2,2,2);imshow(gbw);title('巴特沃兹高通滤波');
subplot(2,2,3);imshow(ghf);title('高频强调滤波');
subplot(2,2,4);imshow(ghe);title('高频强调滤波与直方图均衡化结合');
运行结果