function g = spatialFilter ( Type , Size , Varargin ) if nargin < 3 I I isempty ( size ) size = [3 3] ; end if nume 1 (size ) = = 1 size = [size size] ; end Type = Lower ( Type ) ; Switch Type Case ‘ median ‘ g = medfilt2 ( f , size ) ; Case { ‘ average ‘ , ‘ mean ‘ } W = fspecial ( ‘average ‘ , size ) ; G = imfilter ( f , w ) ; Case { ‘ geomtric ‘ , ‘gmean’ , ‘geomean’ , ‘ geomtricmean ‘} g = colfilt (f , size , ‘ sliding ‘ , @GeomtricMean ) ; Case { ‘ harmonic ‘ , ‘ hmean ‘ , ‘ harmean ‘ , ‘ harmonicmean ‘} g = colfilt (f , size , ‘ sliding ‘ , @ HarmonicMean) ; Case { ‘ charmonic ‘ , ‘ contraharmonic ‘} if isempty ( Varargin ) Q = 1 ; else Q = Varargin } 1 { ; end fun = @ (X) ContraHarmonicMean ( X , Q ) ; g = colfilt (f , size , ‘ sliding ‘ , fun ) ; Case ‘ max ‘ g= ordfilt2 (f , prod ( size) , true ( size ) ) ; Case ‘ min ‘ g= ordfilt2 ( f , 1 , true ( size ) ) ; Case ‘ midpoint ‘ gmin = ordfilt2 ( f , 1 , true ( size ) ) ; gmax= ordfilt2 (f , prod ( size) , true ( size ) ) ; g=( gmin + gmax ) /2 ; Case ‘ alphatrim ‘ if isempty ( Varargin ) alpha = 0 . 1 ; else alpha = Varargin } 1 { ; end fun = @ ( X ) AlphaTrimMean ( x , alpha ) ; g = colfilt ( f , size , ‘ sliding ‘ , fun ) ; Other wise Error ( ‘ Undefined filter type . ‘ ) ; end function gm= GeomtricMean (X) gm= exp (mean (Log ( X ) ) ) ; end function hm = HarmonicMean (X) hm= 1./mean (1./X) ; end function chm= ContraHarmonicMean (X) Chm= Sum ( X . ^ (Q+1) ) ./Sum( X . ^ Q ) ; end function atm = AlphaTrimMean ( X , alpha ) atm = zeros ( 1 , size ( x , 2 ) ) ; for j = 1 : nume 1 (atm ) xtj = AlphaTrim ( x ( : , j ) , alpha ) ; atm ( j ) = mean ( xtj ) ; end function xt= AlphaTrim ( X , alpha ) nx = numel ( X ) ; nt = 2*round ( alpha * nx/2 ) ; xt = sort ( X ) ; xt ( [ 1 : nt/2 end – nt/2 + 1 : end ] ) = [ ] ; end