分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx
《分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx》由会员分享,可在线阅读,更多相关《分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
%imcloseandimreconstruct,imcomplement,imregionalmax,bwareaopen,graythresh,imimposeminfunctionandsoon.
ReadintheColorImageandConvertittoGrayscale
%thefirststep:
readthecolorimageandconvertittograyscaleimage
CLC.Clearall;
Closeall;
RGB=imread('
pears.PNG'
);
Ifndims(RGB)==3
I=rgb2gray(RGB);
Theelse
I=RGB.
Theend
Figure('
units'
'
position'
[0,0,1]);
Subplot(1,2,1);
Imshow(RGB);
Thetitle('
artwork'
Subplot(1,2,2);
Imshow(I);
Title('
grayscale'
Step2:
%UsetheGradientMagnitudeastheSegmentationFunction
%2:
thegradientisusedasapartitionfunction
%UsetheSobeledge,imfilter,andsomesimpleproductstocomputethegradient.
ThegradientishighatThebordersofTheobjectsandlow(mostly)insideTheobjects.
%usetheSobeledgeoperatorforthehorizontalandverticaldirectionfilter,andthencalculatethemodulusvalue,Sobeloperatortofiltertheimageintheborderwillbedisplayedafterthelargervalue,intheabsenceofbordervaluewillbesmall.
Hy=fspecial('
sobel'
Hx=hy'
;
Iy=imfilter(I),hy,'
get'
Ix=imfilter(I),hx,'
ream'
Gradmag=SQRT(Ix.^2+Iy.
^2);
Imshow(I,[]),title('
grayscaleimage'
)
"
Imshow"
(gradmag,[]),title('
gradientimage'
Canyousegmenttheimagebyusingthewatershedtransformdirectlyonthegradient?
CanIusewatershedalgorithmdirectlyforgradientimageimage?
L=watershed(gradmag);
Lrgb=label2rgb(L);
Imshow(gradmag,[]),title('
Imshow(Lrgb);
gradientamplitudeforwatershedtransformation'
%No.Withoutadditionalpreprocessingtotheasthemarkercomputationsbelow,usingthewatershedtransformdirectly,theresultsin"
oversegmentation."
Theresultsofthewatershedalgorithmcanbeover-segmentedbyusingtheimageofgradientmodulus.Therefore,itisusuallynecessarytomarktheforegroundobjectsandbackgroundobjectsseparatelytogetabettersegmentationeffect.
Step3:
%MarktheForegroundObjects
marktheforegroundobject
%Avarietyofprocedurescouldbeappliedheretofindtheforegroundmarkers,
%whichmustbeconnectedblobsofpixelsinsideeachoftheforegroundobjects.
Thisexampleyou'
llusemorphologicaltechniquescalled"
opening-byreconstruction"
and"
closing-by-
reconstruction"
to"
clean"
uptheimage.
Theseoperationswillcreateflatmaximainsideeachobjectthatcanbelocatedusingimregionalmax.
Therearemultiplewaystoapplyheretoobtaintheforegroundmarkup,whichmustbetheconnectiondotpixelswithintheforegroundobject.Inthisexample,wewillusemorphologicaltechniques"
basedonopenreconstruction"
closedreconstruction"
tocleanupimages.
Theseoperationswillcreateunitsofmaximumvaluewithineachobject,enablingtheuseofimregionalmaxtolocate.
%openoperationandclosedoperation:
aftercorrosion,theexpansioniscalledopen;
Afterexpansion,corrosioniscalledclosure.Openandclosedoperationscanremovespecificimagedetailsthataresmallerthanstructuralelements,whileensuringthatthereisnoglobalgeometricdistortion.
The%openoperationcanfilteroutthelittlespikesthataresmallerthanthestructuralelements,cuttingoffthelong,thinonesandseparatingthem.Theclosedoperationcanconnectthesmallgaporholefillofthestructureelement,andconnecttheshortintervals.
Itisanerosionfollowedbyamorphologicalreconstruction.
Let'
scomparethetwo.
First,computetheopeningusingimopen.
%openoperationisanexpansionaftercorrosion,basedonopenreconstruction(basedontheoperationofreconstruction),aftercorrosion,morphologicalreconstructioniscarriedout.Let'
scomparethesetwoways.First,openanoperationwithimopen.
Se=strel(disk,20).
Io=imopen(I,se);
Imshow(I,[]);
Imshow(Io),title('
imageoperation'
%Nextcomputetheopening-by-reconstructionusingimerodeandimreconstruct.
%next,buildontheopenreconstructioncalculationbycorrosion.
Ie=imerode(I,se);
Iobr=imreconstruct(Ie,I);
Imshow(Iobr,[]),title('
basedonopenreconstructedimage'
Followingtheopeningwithaclosingcanremovethedarkspotsandstemmarks.Comparearegularmorphologicalclosingwithaclosing-by-remission.
Afterthe%openoperation,theclosedoperationcanberemovedtoremovethedarkerspotsandboughs.Contrastthenormalmorphologyclosureandtheclosed-basedreconstructionoperation.First,useimclose:
TheIoc=imclose(Io,se);
Ic=imclose(I,se);
Subplot(2,2,1);
Subplot(2,2,2);
Imshow(Io,[]);
openoperationimage'
Subplot(2,2,3);
Imshow(Ic,[]);
closedoperationimage'
Subplot(2,2,4);
Imshow(Ioc,[]),title('
openclosedoperation'
%Nowuseimdilatefollowedbyimreconstruct.Noticeyoumustcomplementtheimageinputsandtheoutputofimreconstruct.
ThecomplementoftheimageIMcanbeabinary,intensity,orRGBimage.im2hasthesameclassandsizeasIM.
%nowuseimdilate,thenusetheimreconstruct.Attentionmustbecomplementarytotheinputimages,forcomplementaryimreconstructoutputimages.
%IM2=imcomplement(IM)tocomputethecomplementofimageIM.IMcanbebinaryimage,orRGBimage.IM2andIMhavethesamedatatypeandsize.
Iobrd=imdilate(Iobr,se);
Iobrcbr=imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr=imcomplement(Iobrcbr);
normalized'
theposition'
[0011));
Imshow(Ioc,[]);
openandclosedoperation'
Imshow(Iobr,[]);
Imshow(Iobrcbr,[]),title('
basedonclosedreconstructionimages'
YoucanseebycomparingIobrcbrwithIoc,reconstructionandclosingaremoreeffectivethan
Attheendoftheday,thewholeoftheobjectswasmadeupofthewholeoftheobjects.
%CalculatetheregionalmaximaofIobrcbrtoobtaingoodforegroundmarkers.
BycomparingIobrcbrandloc,itcanbeseenthatunderthe
applicationofremovingthesmudgewithoutaffectingtheglobalshapeoftheobject,theopenandclosedoperationbasedonreconstructionismoreeffectivethanthestandardopenandclosedreconstruction.CalculatethelocalmaximsofIobrcbrtogetabetteroutlook.
FGM=imregionalmax(Iobrcbr);
Subplot(1,3,1);
Subplot(1,3,2);
Imshow(Iobrcbr,[]);
openclosedoperationbasedonreconstruction'
Subplot(1,3,3);
Imshow(FGM,[]);
localmaximumimage'
%Tohelpinterprettheresult,superimpose(stack)theforegroundmarkerimageonthetheoriginalimage.
%tohelpunderstandthisresult,thestackforegroundismarkedtotheoriginal.
It1=RGB(:
:
1);
It2=RGB(:
2);
It3=RGB(:
3);
It1(FGM)=255;
It2(FGM)=0;
It3(FGM)=0;
I2=cat(3,It1,It2,It3);
Imshow(RGB,[]);
originalimage'
Imshow(I2);
localmaximumsuperimposedtotheoriginalimage'
%Noticethatsomeofthemostly-occludedandShadowedobjectsarenotmarked,whichmeansthattheseobjectswillnotbesegmentedproperlyintheendresult.
%oftheforegroundmarkersinsomeobjectsgorightuptotheobjects'
edge.Thatmeansyoushouldcleantheedgesofthemarkerblobsandthenshrinkthemabit.
Youcandothisbyaclosingfollowedbyanerosion.
%notethatmostclos