若出现的退化仅仅是噪声,则遵循模型:g(x,y) = f(x,y) + η(x,y),此时所选的降低噪声的方法是空间滤波。
这里用到的函数为自定义的spfilt函数,适用于不考虑图像特性在不同位置之间的差异的图像。由于代码过长,具体函数贴在本文的末尾以供参考。
代码示例
% 胡椒噪声
f = imread('ckt-board-orig.tif');
[M,N] = size(f);
R = imnoise2('salt & pepper',M,N,0.1,0);
c = find(R==0);
gp = f;
gp(c) = 0;
% 盐粒噪声
R = imnoise2('salt & pepper',M,N,0,0.1);
c = find(R==1);
gs = f;
gs(c) = 255;
% 过渡胡椒噪声的较好方法是使用Q为正值的反调和滤波器
fp = spfilt(gp,'chmean',3,3,1.5);
% 过渡盐粒噪声的较好方法是使用Q为负值的反调和滤波器
fs = spfilt(gs,'chmean',3,3,-1.5);
% 使用最大和最小滤波器可以得到类似的结果
fpmax = spfilt(gp,'max',3,3);
fsmin = spfilt(gs,'min',3,3);
subplot(3,2,1);imshow(gp);title('仅被胡椒噪声污染');
subplot(3,2,2);imshow(gs);title('仅被盐粒噪声污染');
subplot(3,2,3);imshow(fp);title('Q=1.5反调和滤波器处理胡椒噪声');
subplot(3,2,4);imshow(fs);title('Q=-1.5反调和滤波器处理盐粒噪声');
subplot(3,2,5);imshow(fpmax);title('最大滤波器处理胡椒噪声');
subplot(3,2,6);imshow(fsmin);title('最小滤波器处理盐粒噪声');
运行结果
设计一个自适应中值滤波器,从而可以通过根据被滤波区域的图像特性自适应的滤波器来改进结果。详细说明的算法如下所示:
代码示例
f = imread('ckt-board-orig.tif');
g = imnoise(f,'salt & pepper',.25);
f1 = medfilt2(g,[7 7],'symmetric');
f2 = adpmedian(g,7);
subplot(1,3,1);imshow(g);title('被椒盐噪声污染的图像');
subplot(1,3,2);imshow(f1);title('使用中值滤波器');
subplot(1,3,3);imshow(f2);title('使用自适应中值滤波器');
运行结果
function f = spfilt(g, type, m, n, parameter)
%SPFILT Performs linear and nonlinear spatial filtering.
% F = SPFILT(G, TYPE, M, N, PARAMETER) performs spatial filtering
% of image G using a TYPE filter of size M-by-N. Valid calls to
% SPFILT are as follows:
%
% F = SPFILT(G, 'amean', M, N) Arithmetic mean filtering.
% F = SPFILT(G, 'gmean', M, N) Geometric mean filtering.
% F = SPFILT(G, 'hmean', M, N) Harmonic mean filtering.
% F = SPFILT(G, 'chmean', M, N, Q) Contraharmonic mean
% filtering of order Q. The
% default is Q = 1.5.
% F = SPFILT(G, 'median', M, N) Median filtering.
% F = SPFILT(G, 'max', M, N) Max filtering.
% F = SPFILT(G, 'min', M, N) Min filtering.
% F = SPFILT(G, 'midpoint', M, N) Midpoint filtering.
% F = SPFILT(G, 'atrimmed', M, N, D) Alpha-trimmed mean filtering.
% Parameter D must be a
% nonnegative even integer;
% its default value is D = 2.
%
% The default values when only G and TYPE are input are M = N = 3,
% Q = 1.5, and D = 2.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.6 $ $Date: 2003/10/27 20:07:00 $
% Process inputs.
if nargin == 2
m = 3; n = 3; Q = 1.5; d = 2;
elseif nargin == 5
Q = parameter; d = parameter;
elseif nargin == 4
Q = 1.5; d = 2;
else
error('Wrong number of inputs.');
end
% Do the filtering.
switch type
case 'amean'
w = fspecial('average', [m n]);
f = imfilter(g, w, 'replicate');
case 'gmean'
f = gmean(g, m, n);
case 'hmean'
f = harmean(g, m, n);
case 'chmean'
f = charmean(g, m, n, Q);
case 'median'
f = medfilt2(g, [m n], 'symmetric');
case 'max'
f = ordfilt2(g, m*n, ones(m, n), 'symmetric');
case 'min'
f = ordfilt2(g, 1, ones(m, n), 'symmetric');
case 'midpoint'
f1 = ordfilt2(g, 1, ones(m, n), 'symmetric');
f2 = ordfilt2(g, m*n, ones(m, n), 'symmetric');
f = imlincomb(0.5, f1, 0.5, f2);
case 'atrimmed'
if (d <= 0) | (d/2 ~= round(d/2))
error('d must be a positive, even integer.')
end
f = alphatrim(g, m, n, d);
otherwise
error('Unknown filter type.')
end
%-------------------------------------------------------------------%
function f = gmean(g, m, n)
% Implements a geometric mean filter.
inclass = class(g);
g = im2double(g);
% Disable log(0) warning.
warning off;
f = exp(imfilter(log(g), ones(m, n), 'replicate')).^(1 / m / n);
warning on;
f = changeclass(inclass, f);
%-------------------------------------------------------------------%
function f = harmean(g, m, n)
% Implements a harmonic mean filter.
inclass = class(g);
g = im2double(g);
f = m * n ./ imfilter(1./(g + eps),ones(m, n), 'replicate');
f = changeclass(inclass, f);
%-------------------------------------------------------------------%
function f = charmean(g, m, n, q)
% Implements a contraharmonic mean filter.
inclass = class(g);
g = im2double(g);
f = imfilter(g.^(q+1), ones(m, n), 'replicate');
f = f ./ (imfilter(g.^q, ones(m, n), 'replicate') + eps);
f = changeclass(inclass, f);
%-------------------------------------------------------------------%
function f = alphatrim(g, m, n, d)
% Implements an alpha-trimmed mean filter.
inclass = class(g);
g = im2double(g);
f = imfilter(g, ones(m, n), 'symmetric');
for k = 1:d/2
f = imsubtract(f, ordfilt2(g, k, ones(m, n), 'symmetric'));
end
for k = (m*n - (d/2) + 1):m*n
f = imsubtract(f, ordfilt2(g, k, ones(m, n), 'symmetric'));
end
f = f / (m*n - d);
f = changeclass(inclass, f);
%-------------------------------------------------------------------%
function image = changeclass(class, varargin)
switch class
case 'uint8'
image = im2uint8(varargin{:});
case 'uint16'
image = im2uint16(varargin{:});
case 'double'
image = im2double(varargin{:});
otherwise
error('Unsupported IPT data class.');
end