function[B,L,N,A]=bwboundaries(varargin)
%BWBOUNDARIESTraceregionboundariesinabinaryimage.
%B=BWBOUNDARIES(BW)tracestheexteriorboundaryofobjects,
%descends
%intotheoutermostobjects(parents)andtracestheirchildren(objects
%completelyenclodbytheparents).BWmustbeabinaryimagewhere
%nonzeropixelsbelongtoanobjectand0-pixelsconstitutethe
%-by-1cellarray,wherePisthenumberofobjects
%llcontainsaQ-by-2matrix,whereQisthenumber
%wofthe
%Q-by-2matricescontainstherowandcolumncoordinatesofaboundary
%pixel.
%
%B=BWBOUNDARIES(BW,CONN)specifiestheconnectivitytouwhentracing
%ault
%valueforCONNis8.
%
%B=BWBOUNDARIES(BW,CONN,OPTIONS)providesanoptionalstring
%'noholes'speedsuptheoperationofthealgorithmby
%havingitarchonlyforobject(parentandchild)boundaries.
%Bydefault,orwhen'holes'stringisspecified,thealgorithmarches
%forbothobjectandholeboundaries.
%
%[B,L]=BWBOUNDARIES(...)returnsthelabelmatrix,L,asthecond
%
%two-dimensionalarrayofnonnegativeintegersthatreprent
%-thregionincludesallelementsinLthathave
%berofobjectsandholesreprentedbyLis
%equaltomax(L(:)).Thezero-valuedelementsofLmakeupthebackground.
%
%[B,L,N,A]=BWBOUNDARIES(...)returnsthenumberofobjectsfound(N)
%stNcellsinBareobjectboundaries.
%uare,spar,
%logicalmatrixwithsideoflengthmax(L(:)),whorowsandcolums
%correspondtothepositionofboundariesstoredinB.
%TheboundarienclodbyaB{m}aswellastheboundaryenclosing
%B{m}canbothbefoundusingAasfollows:
%
%enclosing_boundary=find(A(m,:));
%enclod_boundaries=find(A(:,m));
%
%ClassSupport
%-------------
%BWcanbelogicalornumericanditmustbereal,2-D,andnonspar.
%L,rlogical.
%
%Example1
%---------
%ythelabeled
%objectsusingthejetcolormap,onagraybackground,withregion
%boundariesoutlinedinwhite.
%
%I=imread('');
%BW=im2bw(I,graythresh(I));
%[B,L]=bwboundaries(BW,'noholes');
%imshow(label2rgb(L,@jet,[.5.5.5]))
%holdon
%fork=1:length(B)
%boundary=B{k};
%plot(boundary(:,2),boundary(:,1),'w','LineWidth',2)
%end
%
%Example2
%---------
%ytheregion
%ytextshowingtheregionnumber
%(badonthelabelmatrix),onally,
%displaytheadjacencymatrixusingSPY.
%
%HINT:Aftertheimageisdisplayed,uthezoomtoolinordertoread
%individuallabels.
%
%BW=imread('');
%[B,L,N,A]=bwboundaries(BW);
%imshow(BW);holdon;
%colors=['b''g''r''c''m''y'];
%fork=1:length(B),
%boundary=B{k};
%cidx=mod(k,length(colors))+1;
%plot(boundary(:,2),boundary(:,1),colors(cidx),'LineWidth',2);
%%randomizetextpositionforbettervisibility
%rndRow=ceil(length(boundary)/(mod(rand*k,7)+1));
%col=boundary(rndRow,2);row=boundary(rndRow,1);
%h=text(col+1,row-1,num2str(L(row,col)));
%t(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');
%end
%figure;spy(A);
%
%Example3
%---------
%Displayobjectboundariesinredandholeboundariesingreen.
%
%BW=imread('');
%[B,L,N]=bwboundaries(BW);
%imshow(BW);holdon;
%fork=1:length(B),
%boundary=B{k};
%if(k>N)
%plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
%el
%plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
%end
%end
%
%Example4
%---------
%Displayparentboundariesinred(anyemptyrowofadjacency
%matrixbelongstoaparent)andtheirholesingreen.
%
%BW=imread('');
%[B,L,N,A]=bwboundaries(BW);
%imshow(BW);holdon;
%fork=1:length(B),
%if(~sum(A(k,:)))
%boundary=B{k};
%plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);
%forl=find(A(:,k))'
%boundary=B{l};
%plot(boundary(:,2),boundary(:,1),'g','LineWidth',2);
%end
%end
%end
%
%SeealsoBWTRACEBOUNDARY,BWLABEL,BWLABELN.
%Copyright1993-2003TheMathWorks,Inc.
%$Revision:1.1.6.3$$Date:2003/05/0317:50:06$
[BW,conn,findholes]=parInputs(varargin{:});
[objs,L]=FindObjectBoundaries(BW,conn);
if(findholes)
[holes,LabeledHoles]=FindHoleBoundaries(BW,conn);
%Generatecombinedholes+objectslabelmatrix
L=L+(LabeledHoles~=0)*length(objs)+LabeledHoles;
el
holes={};
end
%Createtheoutputmatrix
B=[objs;holes];
%Returnnumberofobjectboundaries
N=length(objs);
if(nargout>3)
%Produceanadjacencymatrixshowingparent-hole-childrelationships
A=CreateAdjMatrix(B,N);
end
%-----------------------------------------------------------------------------
function[BW,conn,findholes]=parInputs(varargin)
checknargin(1,4,nargin,mfilename);
BW=varargin{1};
checkinput(BW,{'numeric','logical'},{'real','2d','nonspar'},...
mfilename,'BW',1);
if~islogical(BW)
BW=BW~=0;
end
firstStringToProcess=0;
ifnargin<2
conn=8;
el
ifischar(varargin{2})
firstStringToProcess=2;
conn=8;
el
ifnargin>2,
firstStringToProcess=3;
end
conn=varargin{2};
checkinput(conn,{'double'},{},mfilename,'CONN',2);
if(conn~=4&conn~=8)
eid=sprintf('Images:%s:badScalarConn',mfilename);
msg='AscalarconnectivityspecifierCONNmusteitherbe4or8';
error(eid,msg);
end
end
end
findholes=true;
iffirstStringToProcess
validStrings={'noholes','holes'};
fork=firstStringToProcess:nargin
%checkforoptions
string=checkstrs(varargin{k},validStrings,mfilename,'OPTION',k);
switchstring
ca'noholes'
findholes=fal;
ca'holes'
findholes=true;
otherwi
error('Images:bwboundaries:unexpectedError','%s',...
'Unexpectedlogicerror.')
end
end
end
%-----------------------------------------------------------------------------
function[A]=CreateAdjMatrix(B,numObjs)
A=spar(fal(length(B)));
levelCellArray=GroupBoundariesByTreeLevel(B,numObjs);
%scanthroughallthelevelpairs
fork=1:length(levelCellArray)-1,
parentsIdx=levelCellArray{k};%outsideboundaries
childrenIdx=levelCellArray{k+1};%insideboundaries
parents=B(parentsIdx);
children=B(childrenIdx);
sampChildren=GetSamplePointsFromBoundaries(children);
form=1:length(parents),
parent=parents{m};
inside=inpolygon(sampChildren(:,2),sampChildren(:,1),...
parent(:,2),parent(:,1));
%castingtologicalisnecessarybecauofthebug,eGECK#137394
inside=logical(inside);
A(childrenIdx(inside),parentsIdx(m))=true;
end
end
%-----------------------------------------------------------------------------
functionpoints=GetSamplePointsFromBoundaries(B)
points=zeros(length(B),2);
form=1:length(B),
boundary=B{m};
points(m,:)=boundary(1,:);
end
%-----------------------------------------------------------------------------
%
%firstelementoftheoutputcellarrayholdsadoublearrayofindices
%ofboundarieswhicharetheoutermost(firstlayerofanonion),the
%condholdsthecondlayer,andsoon.
functionidxGroupedByLevel=GroupBoundariesByTreeLevel(B,numObjs)
processHoles=~(length(B)==numObjs);
%partheinput
objIdx=1:numObjs;
objs=B(objIdx);
ifprocessHoles
holeIdx=numObjs+1:length(B);
holes=B(holeIdx);
el
holes={};
end
%initializeoutputandloopcontrolvariables
idxGroupedByLevel={};
done=fal;
findHole=fal;%startwithanobjectboundary
while~done
if(findHole)
I=FindOutermostBoundaries(holes);
holes=holes(~I);%removeprocesdboundaries
idxGroupedByLevel=[idxGroupedByLevel,{holeIdx(I)}];
holeIdx=holeIdx(~I);%removeindicesofprocesdboundaries
el
I=FindOutermostBoundaries(objs);
objs=objs(~I);
idxGroupedByLevel=[idxGroupedByLevel,{objIdx(I)}];
objIdx=objIdx(~I);
end
if(processHoles)
findHole=~findHole;
end
if(impty(holes)&&impty(objs))
done=true;
end
end
%-----------------------------------------------------------------------------
%Returnsalogicalvectorshowingthelocationsofoutermostboundaries
%intheinputvector(ie1fortheboundariesthatareoutermostand
%0forallotherboundaries)
functionI=FindOutermostBoundaries(B)
%Lookforparentboundaries
I=fal(1,length(B));
form=1:length(B),
boundary=B{m};
x=boundary(1,2);%grabasamplepointfortesting
y=boundary(1,1);
surrounded=fal;
forn=[1:(m-1),(m+1):length(B)],%excludeboundaryundertest
boundary=B{n};
if(inpolygon(x,y,boundary(:,2),boundary(:,1))),
surrounded=true;
break;
end
end
I(m)=~surrounded;
end
%-----------------------------------------------------------------------------
function[B,L]=FindObjectBoundaries(BW,conn)
L=bwlabel(BW,conn);
B=bwboundariesmex(L,conn);
%-----------------------------------------------------------------------------
function[B,L]=FindHoleBoundaries(BW,conn)
%ctsare8connected,thenholes
%mustbe4connectedandviceversa.
if(conn==4)
backgroundConn=8;
el
backgroundConn=4;
end
%Turnholesintoobjects
BWcomplement=imcomplement(BW);
%clearunwanted"hole"objectsfromtheborder
BWholes=imclearborder(BWcomplement,backgroundConn);
%gettheholes!
L=bwlabel(BWholes,backgroundConn);
B=bwboundariesmex(L,backgroundConn);
本文发布于:2022-12-31 16:33:19,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/90/66571.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |