Skip to content

Commit a1d8cb3

Browse files
committed
better support of areaOpening for label images
1 parent 785ad08 commit a1d8cb3

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

src/@Image/areaOpening.m

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,22 @@
3434
% Created: 2011-11-08, using Matlab 7.9.0.529 (R2009b)
3535
% Copyright 2011 INRA - Cepia Software Platform.
3636

37+
% check input arguments
38+
if nargin < 2
39+
error('Image:areaOpening', 'Need to specify the minimum number of pixels');
40+
end
41+
42+
% force image to be label
3743
if isLabelImage(obj)
3844
data = obj.Data;
45+
res = zeros(size(obj.Data), 'like', obj.Data);
3946

4047
elseif isBinaryImage(obj)
4148
% if image is binary compute labeling
4249

4350
% first determines connectivity to use
4451
conn = 4;
45-
if obj.Dimension == 3
52+
if ndims(obj) == 3
4653
conn = 6;
4754
end
4855
if ~isempty(varargin)
@@ -51,6 +58,7 @@
5158

5259
% appply labeling, get result as 2D or 3D matrix
5360
data = labelmatrix(bwconncomp(obj.Data, conn));
61+
res = false(size(obj.Data), 'like', obj.Data);
5462

5563
else
5664
error('Image:areaOpening', 'Requires binary or label image');
@@ -62,7 +70,10 @@
6270

6371
% select regions with areas greater than threshold
6472
inds = find(areas >= value);
65-
data = ismember(data, inds);
73+
74+
% keep only the pixels belonging to the selected regions
75+
mask = ismember(data, inds);
76+
res(mask) = data(mask);
6677

6778
% create new image
68-
res = Image.create('data', data, 'parent', obj, 'type', 'binary');
79+
res = Image.create('data', res, 'parent', obj);

tests/image/test_areaOpening.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function tests = test_areaOpening
2+
% Test suite for the file areaOpening.
3+
%
4+
% Test suite for the file areaOpening
5+
%
6+
% Example
7+
% test_areaOpening
8+
%
9+
% See also
10+
% areaOpening
11+
12+
% ------
13+
% Author: David Legland
14+
% e-mail: david.legland@inrae.fr
15+
% Created: 2020-11-25, using Matlab 9.8.0.1323502 (R2020a)
16+
% Copyright 2020 INRAE - BIA-BIBS.
17+
18+
tests = functiontests(localfunctions);
19+
20+
function test_Simple_binary(testCase) %#ok<*DEFNU>
21+
% Test call of function without argument.
22+
23+
% create default label image
24+
data = zeros(8, 8, 'uint8');
25+
data(2, 2) = 2;
26+
data(2, 4:7) = 4;
27+
data(4:7, 2) = 5;
28+
data(4:7, 4:7) = 8;
29+
img = Image(data>0);
30+
31+
imgOp = areaOpening(img, 3);
32+
33+
% expect to keep three connected components
34+
lbl = componentLabeling(imgOp, 4);
35+
assertEqual(testCase, max(lbl), 3);
36+
37+
38+
function test_Simple_label(testCase) %#ok<*DEFNU>
39+
% Test call of function without argument.
40+
41+
% create default label image
42+
data = zeros(8, 8, 'uint8');
43+
data(2, 2) = 2;
44+
data(2, 4:7) = 4;
45+
data(4:7, 2) = 5;
46+
data(4:7, 4:7) = 8;
47+
img = Image(data, 'Type', 'Label');
48+
49+
imgOp = areaOpening(img, 3);
50+
51+
% expect result to be a label image
52+
assertTrue(testCase, isLabelImage(imgOp));
53+
54+
% expect to keep three connected components
55+
% (recompute labels)
56+
labelList = unique(imgOp(imgOp>0));
57+
assertEqual(testCase, length(labelList), 3);
58+

0 commit comments

Comments
 (0)