Skip to content

Commit 9e0bf22

Browse files
committed
update medianFilter.m
1 parent 4e18a63 commit 9e0bf22

File tree

2 files changed

+90
-17
lines changed

2 files changed

+90
-17
lines changed

src/@Image/medianFilter.m

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
function 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'
@@ -17,21 +21,28 @@
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
3647
if isa(se, 'strel')
3748
se = getnhood(se);
@@ -43,13 +54,26 @@
4354
padopt = varargin{1};
4455
end
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
5478
name = createNewName(obj, '%s-medianFilt');
55-
res = Image('Data', data, 'Parent', obj, 'Name', name);
79+
res = Image('Data', data, 'Parent', obj, 'Name', name);

tests/image/test_medianFilter.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function tests = test_medianFilter
2+
% Test suite for the file medianFilter.
3+
%
4+
% Test suite for the file medianFilter
5+
%
6+
% Example
7+
% test_medianFilter
8+
%
9+
% See also
10+
% medianFilter
11+
12+
% ------
13+
% Author: David Legland
14+
% e-mail: david.legland@inrae.fr
15+
% Created: 2021-11-19, using Matlab 9.10.0.1684407 (R2021a) Update 3
16+
% Copyright 2021 INRAE - BIA-BIBS.
17+
18+
tests = functiontests(localfunctions);
19+
20+
function test_2d_Size(testCase) %#ok<*DEFNU>
21+
% Test call of function without argument.
22+
23+
img = Image.read('rice.png');
24+
25+
res = medianFilter(img, [3 3]);
26+
27+
assertEqual(testCase, size(res), size(img));
28+
29+
30+
function test_2d_array(testCase) %#ok<*DEFNU>
31+
% Test call of function without argument.
32+
33+
img = Image.read('rice.png');
34+
35+
se = [0 1 0;1 1 1;0 1 0];
36+
res = medianFilter(img, se);
37+
38+
assertEqual(testCase, size(res), size(img));
39+
40+
41+
function test_3d_Size(testCase) %#ok<*DEFNU>
42+
% Test call of function without argument.
43+
44+
img = Image(ones([7 7 7]));
45+
46+
res = medianFilter(img, [3 3 3]);
47+
48+
assertEqual(testCase, size(res), size(img));
49+

0 commit comments

Comments
 (0)