11function res = medianFilter(obj , se , varargin )
22% Compute median value in the neighboorhood of each pixel.
33%
4+ % RES = medianFilter(IMG2D, [M N])
5+ % RES = medianFilter(IMG3D, [M N P])
6+ % Applies median filtering to the input image, by computing the median
7+ % value in the square or cubic neighborhood of each image element.
8+ %
49% RES = medianFilter(IMG, SE)
5- % Compute the mean filter of image IMG, using structuring element SE.
6- % The goal of obj function is to provide the same interface as for
7- % other image filters (imopen, imerode ...), and to allow the use of
8- % mean filter with user-defined structuring element.
10+ % Compute the median filter of image IMG, using structuring element SE.
11+ % The goal of this function is to provide the same interface as for
12+ % other image filters (opening, erosion ...), and to allow the use of
13+ % median filter with user-defined structuring element.
914% This function can be used for directional filtering.
1015%
11- %
1216% RES = medianFilter(IMG, SE, PADOPT)
1317% also specify padding option. PADOPT can be one of:
1418% 'zeros'
1721% see ordfilt2 for details. Default is 'symmetric' (contrary to the
1822% default for ordfilt2).
1923%
24+ % Implementation notes
25+ % When neighborhood is given as a 1-by-2 or 1-by-3 array, the methods is
26+ % a wrapper for the medfilt2 or medfilt3 function. Otherwise, the
27+ % ordfilt2 function ise used.
28+ %
29+ % Example
30+ % % apply median filtering on rice image
31+ % img = Image.read('rice.png');
32+ % imgf = medianFilter(img, [3 3]);
33+ % figure; show(imgf);
34+ %
2035% See also:
2136% meanFilter, ordfilt2, median
2237%
2338
2439% ------
2540% Author: David Legland
26- % e-mail: david.legland@inra .fr
41+ % e-mail: david.legland@inrae .fr
2742% Created: 2011-08-05, using Matlab 7.9.0.529 (R2009b)
2843% Copyright 2011 INRA - Cepia Software Platform.
2944
3045
31- if obj .Dimension > 2
32- error(' Median filter implemented only for planar images' );
33- end
34-
3546% transform STREL object into single array
3647if isa(se , ' strel' )
3748 se = getnhood(se );
4354 padopt = varargin{1 };
4455end
4556
46- % rotate structuring element
47- se = permute(se , [2 1 3 : 5 ]);
48-
49- % perform filtering
50- order = ceil(sum(se(: )) / 2 );
51- data = ordfilt2(obj .Data , order , se , padopt );
57+ if isnumeric(se ) && all(size(se ) == [1 2 ]) && obj .Dimension == 2
58+ % if input corresponds to filter size, use medfilt2
59+ data = medfilt2(obj .Data , se([2 1 ]));
60+
61+ elseif isnumeric(se ) && all(size(se ) == [1 3 ]) && obj .Dimension == 3
62+ % process the 3D case, only for cubic neighborhoods
63+ data = medfilt3(obj .Data , se([2 1 3 ]));
64+
65+ else
66+ % otherwise, use the ordfilt2 function by choosing the order according
67+ % to SE size.
68+
69+ % rotate structuring element
70+ se = permute(se , [2 1 3 : 5 ]);
71+
72+ % perform filtering
73+ order = ceil(sum(se(: )) / 2 );
74+ data = ordfilt2(obj .Data , order , se , padopt );
75+ end
5276
5377% create result image
5478name = createNewName(obj , ' %s -medianFilt' );
55- res = Image(' Data' , data , ' Parent' , obj , ' Name' , name );
79+ res = Image(' Data' , data , ' Parent' , obj , ' Name' , name );
0 commit comments