谷速软件编程matlab源码二进制地图高强度区域自动分割Word文档格式.docx
《谷速软件编程matlab源码二进制地图高强度区域自动分割Word文档格式.docx》由会员分享,可在线阅读,更多相关《谷速软件编程matlab源码二进制地图高强度区域自动分割Word文档格式.docx(5页珍藏版)》请在冰豆网上搜索。
Everythinglessthanorequaltothisthresholdis
background(false)andeverythingmoreisforeground(true).
Thisvaluedefaultsto1500.
Thisvaluewasdeterminedforuint8
imagesconvertedtodoubleprecision.
HowtoselectanappropriateObjThresh?
>
>
imtool(conv2(double(I),ones(5),'
same'
));
Usethe"
adjustcontrast"
toolandmovethelowerboundtothe
desirablethreshold.
ThelowerboundshouldbetheObjThresh.
MinSize'
=Minimumsizeofanobjecttokeepafterprocessing.
The
defaultisto500voxelswithDimView=3.
See'
DimView'
forafurtherexplanation.
NOTE:
IfMinSize=1;
andBiggestisnotcalled;
noconnected
componentsanalysiswillbeperformedandthiswillbemuch
faster.
Biggest'
=ThisPropertyNamedoesnotrequireaPropertValue.
Ifis
calledonlythebiggestobjectwillbesaved.
Thedefault
forshrinkWrapis3soitwillpreservethebiggest
objectacrossallslices.
Ifthisiscalledandthebiggestobjectissmallerthan
MinSize,thenallobjectsaredeleted.
=Thispropertyisonlyvalidif'
or'
hasbeencalled
DimViewexpectsitsPropertyValuetobeeither2or3.
Thismeans
thatobjectswillbesizedbyviewingtheminthisnumberof
dimensions.
Verbose'
=ThisPropertyNamedoesnotrequireaPropertyValue.
If
itiscalledthecurrentsliceandstepisdisplayedinthecommand
windowwhileitisworking.
Ifitisnotcalledtheyarenotdisplayed.
Parallel'
If
itiscalledtheengineisprocessedinparallel.
Ifitisnot
calledtheengineisprocessedinserial.
ThematlabpoolworkersareexpectedtobeopenbeforeshrinkWrap()
iscalled.
Iftheyarenot,itwillberuninparallelwith
onlyoneworkerwhichisslower.
(PleaseSee:
helpmatlabpool)
%OutputArguments
-Mstore=Binarymapofretainedobjects.
-Medge=EdgeImageofBinaryMap.(Optional,notcalculatedifnot
requested)
%Volumeindoubleprecision&
ParseInputs
invol=double(varargin{1});
[ObjThresh,MinSize,Biggest,DimView,Verbose,isparallel]=parseInputs(varargin(2:
end));
%Getdimensionalsizessotheydon'
tneedtoberecalculatedandso
%involisslicedinparforloop
nr=size(invol,1);
nc=size(invol,2);
np=size(invol,3);
%Preallocatethemap
Mstore=false(nr,nc,np);
ifnargout==2;
Medge=false(nr,nc,np);
end
ifVerbose;
disp('
InputsParsed,VolumesPreallocated;
EngineRunning...'
);
%RuntheEngine:
ifisparallel
parforii=1:
np
disp(['
ProcessingSlice:
num2str(ii)'
...'
]);
[Mstore(:
:
ii),myed]=Engine(invol(:
ii),nr,nc,ObjThresh);
Medge(:
ii)=myed;
end
Slice'
Complete!
'
else
forii=1:
EngineComplete;
PostProcessing...'
%Getridofunwantedobjects:
ifMinSize==1&
&
~Biggest
ShrinkwrapComplete!
return
RemovingSmallObjects'
ifDimView==2&
ii)]=bwareaopen(Mstore(:
ii),MinSize);
Mstore(:
ii)=bwareaopen(Mstore(:
elseifDimView==2%Biggest
np
Mslice=Mstore(:
ii);
CC=bwconncomp(Mslice);
sizes=cellfun(@numel,CC.PixelIdxList);
Mslice(cell2mat(CC.PixelIdxList(sizes~=max(sizes)).'
))=false;
ii)=Mslice;
ifmax(sizes)>
=MinSize
elseif~Biggest%Dimview==3
Mstore=bwareaopen(Mstore,MinSize);
else%DimView=3&
Biggest
CC=bwconncomp(Mstore);
Mstore(cell2mat(CC.PixelIdxList(sizes~=max(sizes)).'
ifnargout==2
Medge=Medge&
Mstore;
%Getridofedgesthatwereremoved
end
function[Mmyed]=Engine(I,nr,nc,ObjThresh)
%TheEngine
%Filtered2-dconvolution&
Truepointsinit
Icon=conv2(double(I),ones(5),'
)>
ObjThresh;
[rc]=find(Icon);
iflength(r)>
2&
~all(r==r
(1))&
~all(c==c
(1))%Ensurethataconvexhullcanbecalculated
%Computetheconvexhullandcreateamask(map)fromit
k=convhulln([r,c]);
M=double(poly2mask(c(k(:
1)),r(k(:
1)),nr,nc));
%Usea2-dimensionalconvolutionandthresholdof8tofindtheedge
myed=(conv2(M,ones(3),'
)<
8)&
M;
Icon=~Icon;
%Whiletheedgeischangingoneveryiterationcontinueerodingthemapwithit
while1
M(Icon&
myed)=0;
myed2=myed;
ifisequal(myed2,myed)
M=logical(M);
break;
else
M=false(nr,nc);
myed=false(nr,nc);
function[ObjThresh,MinSize,Biggest,DimView,Verbose,isparallel]=parseInputs(in)
%Parsetheinputs
%Possibleproperties
props={'
objthresh'
'
minsize'
biggest'
dimview'
verbose'
parallel'
};
in=cellfun(@lower,in,'
uni'
false);
%Defaults
isparallel=false;
%isparallelsince'
isstockfunction
Verbose=false;
DimView=3;
Biggest=false;
MinSize=500;
ObjThresh=1500;
%AssignChosenProperties
[~,idx,idv]=intersect(props,cellfun(@char,in,'
false));
ifany(idx==1);
ObjThresh=in{idv(idx==1)+1};
ifany(idx==2);
MinSize=in{idv(idx==2)+1};
ifany(idx==3);
Biggest=true;
ifany(idx==4);
DimView=in{idv(idx==4)+1};
ifany(idx==5);
Verbose=true;
ifany(idx==6);
isparallel=true;
%ErrorChecking:
assert(isnumeric(ObjThresh),'
ObjThreshisexpectedtobenumeric'
assert(isnumeric(MinSize),'
MinSizeisexpectedtobenumeric'
assert(DimView==2||DimView==3,'
DimViewisexpectedtohaveavalueof2or3'