cleanimg=deislands2d(img,sizelim) remove isolated islands on a 2D image below speicified size limit author: Qianqian Fang (fangq <at> nmr.mgh.harvard.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)
0001 function cleanimg=deislands2d(img,sizelim) 0002 % 0003 % cleanimg=deislands2d(img,sizelim) 0004 % 0005 % remove isolated islands on a 2D image below speicified size limit 0006 % 0007 % author: Qianqian Fang (fangq <at> nmr.mgh.harvard.edu) 0008 % 0009 % input: 0010 % img: a 2D binary image 0011 % sizelim: a integer as the maximum pixel size of a isolated region 0012 % 0013 % output: 0014 % cleanimg: a binary image after removing islands below sizelim 0015 % 0016 % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) 0017 % 0018 0019 img=squeeze(img); 0020 maxisland=-1; 0021 if(nargin==2) maxisland=sizelim; end 0022 0023 islands={}; 0024 0025 cleanimg=zeros(size(img)); 0026 if(sum(img(:))) 0027 img=imclose(img, strel('disk',3)); 0028 islands=bwislands(img); 0029 end 0030 0031 if(length(islands)) 0032 % remove small islands of the foreground 0033 maxblock=-1; 0034 maxblockid=-1; 0035 if(maxisland<0) 0036 for i=1:length(islands) 0037 if(length(islands{i})>maxblock) 0038 maxblockid=i; 0039 maxblock=length(islands{i}); 0040 end 0041 end 0042 if(maxblock>0) 0043 cleanimg(islands{maxblockid})=1; 0044 end 0045 else 0046 for i=1:length(islands) 0047 if(length(islands{i})>maxisland) 0048 cleanimg(islands{i})=1; 0049 end 0050 end 0051 end 0052 0053 % remote small islands of the background 0054 0055 cleanimg=imfill(cleanimg,'holes'); 0056 end