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
Clc ; Clear ; Close all ; f = imread ( ‘Coins . png ‘ ) ; f = im2double ( f ) ; g = imnoise ( f , ‘ salt & pepper ‘ ) ; figure ; subplot (1, 2, 1 ) ; imshow ( f ) ; title ( ‘ Original Image ‘ ) ; subplot (1 , 2 ,2 ) ; imshow ( g ) ; title ( ‘ Noisy Image ‘ ) ;
Clc ; Clear ; Close all ; f = imread ( ‘Coins . png ‘ ) ; f = im2double ( f ) ; g = imnoise ( f , ‘ salt & pepper ‘ ) ; fhat1= spatialFilter ( g , ‘median‘ , [5 5] ) ; fhat2= spatialFilter ( g , ‘ average ‘ , [5 5] ) ; fhat3= spatialFilter ( g , ‘geomtric‘ , [5 5] ) ; fhat4= spatialFilter ( g , ‘harmonic‘ , [5 5] ) ; figure ; subplot (2, 3, 1 ) ; imshow ( f ) ; title ( ‘ Original Image ‘ ) ; subplot (2, 3, 2) ; imshow (g) ; title ( ‘ Noisy Image ‘ ) ; subplot (2, 3, 3) ; imshow ( fhat 1 ) ; title ( ‘ After Meadin Filter ‘ ) ; subplot (2, 3, 4) ; imshow (fhat 2 ) ; title ( ‘ After Average Filter ‘ ) ; subplot (2, 3, 5) ; imshow (fhat 3 ) ; title ( ‘ After Geometric Mean Filter ‘ ) ; subplot (2, 3, 6 ) ; imshow (fhat 4 ) ; title ( ‘After Harmonic Mean Filter ‘ ) ;
function gm= GeomtricMean (X) epsilon = 0.001 ; % x = max (x , epsilon ) ; x = ( 1- epsilon ) * x + epsilon ; gm= exp (mean (Log ( X ) ) ) ; end function hm = HarmonicMean (X) epsilon = 0.001 ; % x = max (x , epsilon ) ; x = ( 1- epsilon ) * x + epsilon ; hm= 1./mean (1./X) ; end Clc ; Clear ; Close all ; f = imread ( ‘Coins . png ‘ ) ; f = im2double ( f ) ; %g = imnoise ( f , ‘ Gaussian ‘ , 0 , 0.01 ) ; g = imnoise ( f , ‘ salt & pepper ‘ ) ; fhat1= spatialFilter ( g , ‘median‘ , [5 5] ) ; fhat2= spatialFilter ( g , ‘ average ‘ , [5 5] ) ; fhat3= spatialFilter ( g , ‘geomtric‘ , [5 5] ) ; fhat4= spatialFilter ( g , ‘harmonic‘ , [5 5] ) ; fhat5= spatialFilter ( g , ‘ charmonic ‘ , [5 5] , 2 ) ; fhat6= spatialFilter ( g , ‘max ‘ , [5 5] ) ; fhat7= spatialFilter ( g , ‘min ‘ , [5 5] ) ; fhat8= spatialFilter ( g , ‘ midpoint ‘ , [5 5] ) ; fhat9= spatialFilter ( g , ‘ alphatrim ‘ , [5 5] , 0.1 ) ; figure ; subplot (3, 4, 1 ) ; imshow ( f ) ; title ( ‘ Original Image ‘ ) ; subplot (3, 4, 2) ; imshow (g) ; title ( ‘ Noisy Image ‘ ) ; subplot (3, 4, 3) ; imshow ( fhat 1 ) ; title ( ‘Meadin Filter ‘ ) ; subplot (3, 4, 4) ; imshow (fhat 2 ) ; title ( ‘Average Filter ‘ ) ; subplot (3, 4, 5) ; imshow (fhat 3 ) ; title ( ‘Geometric Mean Filter ‘ ) ; subplot (3, 4, 6 ) ; imshow (fhat 4 ) ; title ( ‘Harmonic Mean Filter ‘ ) ; subplot (3, 4, 7 ) ; imshow (fhat 5 ) ; title ( ‘Contra - Harmonic Filter ‘ ) ; subplot (3, 4, 8 ) ; imshow (fhat 6 ) ; title ( ‘Max Filter ‘ ) ; subplot (3, 4, 9 ) ; imshow (fhat 7 ) ; title ( ‘ Min Filter ‘ ) ; subplot (3, 4, 10 ) ; imshow (fhat 8 ) ; title ( ‘ Mid – point Filter ‘ ) ; subplot (3, 4, 11 ) ; imshow (fhat 9 ) ; title ( ‘ \alpha – trim Filter ‘ ) ;
g = imnoise ( f , ‘gaussian ‘ , 0 , 0.01 ) ; %g = imnoise ( f , ‘ salt & pepper ‘ ) ;