分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx

上传人:b****7 文档编号:22328721 上传时间:2023-02-03 格式:DOCX 页数:13 大小:19.19KB
下载 相关 举报
分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx_第1页
第1页 / 共13页
分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx_第2页
第2页 / 共13页
分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx_第3页
第3页 / 共13页
分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx_第4页
第4页 / 共13页
分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx

《分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx》由会员分享,可在线阅读,更多相关《分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx(13页珍藏版)》请在冰豆网上搜索。

分水岭算法MATLABWatershedalgorithmMATLAB文档格式.docx

%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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 预防医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1