-
Notifications
You must be signed in to change notification settings - Fork 73
/
deislands2d.m
58 lines (52 loc) · 1.32 KB
/
deislands2d.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function cleanimg = deislands2d(img, sizelim)
%
% cleanimg=deislands2d(img,sizelim)
%
% remove isolated islands on a 2D image below speicified size limit
%
% author: Qianqian Fang (q.fang at neu.edu)
%
% input:
% img: a 2D binary image
% sizelim: a integer as the maximum pixel size of a isolated region
%
% output:
% cleanimg: a binary image after removing islands below sizelim
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%
img = squeeze(img);
maxisland = -1;
if (nargin == 2)
maxisland = sizelim;
end
islands = {};
cleanimg = zeros(size(img));
if (sum(img(:)))
img = imclose(img, strel('disk', 3));
islands = bwislands(img);
end
if (length(islands))
% remove small islands of the foreground
maxblock = -1;
maxblockid = -1;
if (maxisland < 0)
for i = 1:length(islands)
if (length(islands{i}) > maxblock)
maxblockid = i;
maxblock = length(islands{i});
end
end
if (maxblock > 0)
cleanimg(islands{maxblockid}) = 1;
end
else
for i = 1:length(islands)
if (length(islands{i}) > maxisland)
cleanimg(islands{i}) = 1;
end
end
end
% remote small islands of the background
cleanimg = imfill(cleanimg, 'holes');
end