【正文】
for i=2:xlen1 for j=2:ylen1 if(dx(i,j)~=0) if(dy(i,j)/dx(i,j)=amp。amp。dy(i,j)/dx(i,j)) %0 degree grad1=dxy(i,j+1)。 grad2=dxy(i,j1)。 elseif(dy(i,j)/dx(i,j)=amp。amp。dy(i,j)/dx(i,j)) %45 degree grad1=dxy(i1,j+1)。 grad2=dxy(i+1,j1)。 elseif(dy(i,j)/dx(i,j)=||dy(i,j)/dx(i,j)) %135 degree grad1=dxy(i1,j)。 grad2=dxy(i+1,j)。 elseif(dy(i,j)/dx(i,j)=amp。amp。dy(i,j)/dx(i,j)) %90 degree grad1=dxy(i1,j1)。 grad2=dxy(i+1,j+1)。 end else grad1=dxy(i1,j1)。 grad2=dxy(i+1,j+1)。 end if((dxy(i,j)=grad1)amp。amp。(dxy(i,j)=grad2)) q(i,j)=2。 if(dxy(i,j)=hth) s(i,j)=255。 q(i,j)=255。 q=TraceEdge(j,i,lth,q,dxy)。 %尋找周圍是否有非零點(diǎn),有則為邊緣 end end endend %final processingfor i=2:xlen1 for j=2:ylen1 if(q(i,j)~=255) q(i,j)=0。 end endend edgeC=q。%filename %用于雙閾值方法刪除非顯著的邊緣 function q=TraceEdge(y,x,lth,q,dxy)。xn=[1,1,0,1,1,1,0,1]。yn=[0,1,1,1,0,1,1,1]。for k=1:8 yy=y+yn(k)。 xx=x+xn(k)。 if(q(xx,yy)==2amp。amp。dxy(xx,yy)=lth) q(xx,yy)=255。 q=TraceEdge(yy,xx,lth,q,dxy)。 endend算法性能分析為了進(jìn)行性能分析,我們采用標(biāo)準(zhǔn)LENA圖,在圖像中加入噪聲,噪聲的分布為[25,25]上的高斯分布,其效果見圖一。圖一 原圖像和噪聲圖像主程序如下:%%all the functions are called in this program%written by SuBailong 10/04/25 %read imageimage = 39。39。oimage = imread(image)。 %show the origin imagefigure(1)。 %every figure has two imagessubplot(1,2,1)。imshow(oimage)。title(39。原圖像39。)。 %add noiseoimage=double(oimage)。noise=*(rand(256,256))。oimagewn=oimage+noise。 %show the image with noisesubplot(1,2,2)。imshow(oimagewn,gray)。title(39。加噪聲后的圖像39。)。 %edge detection by Roberts operatoredgeRb=Roberts(oimage)。 %show the edgefigure(2)。subplot(1,2,1)。imshow(edgeRb)。title(39。Roberts算子檢測(cè)39。)。 %edge detection by Sobel operatoredgeS=Sobel(oimage)。 %show the edgesubplot(1,2,2)。imshow(edgeS)。title(39。Sobel算子檢測(cè)39。)。 %edge detection by Prewitt operatoredgeS=Prewitt(oimage)。 %show the edgefigure(3)。subplot(1,2,1)。imshow(edgeS)。title(39。Prewitt算子檢測(cè)39。)。 %edge detection by Kirsch operatoredgeK=Kirsch(oimage)。 %show the edgesubplot(1,2,2)。imshow(edgeK)。title(39。Kirsch算子檢測(cè)39。)。 %edge detection by Robinson operatoredgeR=Robinson(oimage)。 %show the edgefigure(4)。subplot(1,2,1)。imshow(edgeR)。title(39。Robinson算子檢測(cè)39。)。 %edge detection by secondorder operatoredgeReO=secondorder(oimage)。 %show the edgesubplot(1,2,2)。imshow(edgeReO)。title(39。二階算子檢測(cè)39。)。 %edge detection by log operatingedgeL=LOG(oimage)。 %show the edgefigure(5)。subplot(1,2,1)。imshow(edgeL)。title(39。LOG檢測(cè)39。)。 %edge detection by log operatingedgeC=Canny(oimage)。 %show the edgesubplot(1,2,2)。imshow(edgeC)。title(39。Canny檢測(cè)39。)。 %%egde detection with noise %edge detection by Roberts operatoredgeRbwn=Roberts(oimagewn)。 %show the edgefigure(6)。subplot(1,2,1)。imshow(edgeRbwn)。title(39。噪聲下Roberts算子檢測(cè)39。)。 %edge detection by Sobel operatoredgeSwn=Sobel(oimagewn)。 %show the edgesubplot(1,2,2)。imshow(edgeSwn)。title(39。噪聲下Sobel算子檢測(cè)39。)。 %edge detection by Prewitt operatoredgeSwn=Prewitt(oimagewn)。 %show the edgefigure(7)。subplot(1,2,1)。imshow(edgeSwn)。title(39。噪聲下Prewitt算子檢測(cè)39。)。 %edge detection by Kirsch operatoredgeKwn=Kirsch(oimagewn)。 %show the edgesubplot(1,2,2)。imshow(edgeKwn)。title(39。噪聲下Kirsch算子檢測(cè)39。)。 %edge detection by Robinson operatoredgeRwn=Robinson(oimagewn)。 %show the edgefigure(8)。subplot(1,2,1)。imshow(edgeRwn)。title(39。噪聲下Robinson算子檢測(cè))。 %edge detection by secondorder operatoredgeSeOwn=secondorder(oimagewn)。 %show the edgesubplot(1,2,2)。imshow(edgeSeOwn)。title(39。噪聲下二階算子檢測(cè)39。)。 %edge detection by log operatingedgeLwn=LOG(oimagewn)。 %show the edgefigure(9)。subplot(1,2,1)。imshow(edgeLwn)。title(39。噪聲下LOG檢測(cè)39。)。 %edge detection by log operatingedgeCwn=Canny(oimagewn)。 %show the edgesubplot(1,2,2)。imshow(edgeCwn)。title(39。噪聲下Canny檢測(cè)39。)。運(yùn)行程序可得如下結(jié)果:圖二 Roberts算子和Sobel算子邊緣檢測(cè)圖三 Prewitt算子和Kirsch算子邊緣檢測(cè)圖四 Robinson算子和二級(jí)算子邊緣檢測(cè)圖五 LOG算子和Canny算子邊緣檢測(cè)圖六 噪聲下Roberts算子和Sobel算子邊緣檢測(cè)圖七 噪聲下Prewitt算子和Kirsch算子邊緣檢測(cè)圖八 噪聲下Robinson算子和二級(jí)算子邊緣檢測(cè)圖九 噪聲下LOG算子和Canny算子邊緣檢測(cè)結(jié)論:(1) 一階微分算子檢測(cè)圖像較容易丟失圖像邊緣信息,從以上幾個(gè)圖中我們可以看出Roberts、Sobel、Prewitt、Krisch、Robinson算子均丟失了部分邊緣信息;(2) 一階微分算子如果閾值選擇不合理,將會(huì)對(duì)噪聲十分敏感;該結(jié)論在Roberts算子的檢測(cè)中可以看出;(3) 二階微分算子,對(duì)圖像中的階躍型邊緣點(diǎn)定位準(zhǔn)確,該算子對(duì)噪聲非常敏感,它使噪聲成分得到加強(qiáng),這兩個(gè)特性使得該算子容易丟失一部分邊緣的方向信息,造成一些不連續(xù)的檢測(cè)邊緣,同時(shí)抗噪聲能力比較差,同時(shí)較容易產(chǎn)生雙邊緣,圖四中的二階微分算子檢測(cè)可以說明這點(diǎn);(4) LOG算子克服了Laplician算子抗噪聲能力比較差的缺點(diǎn),但是其包含了二階微分算子,對(duì)噪聲也十分敏感,產(chǎn)生雙邊緣;(5) Canny算子雖然是基于最優(yōu)化思想推導(dǎo)出的邊緣檢測(cè)算子,但實(shí)際效果并不一定最優(yōu),原因在于理論和實(shí)際有許多不一致的地方(只離散了四個(gè)方向);(6) Canny該算子同樣采用高斯函數(shù)對(duì)圖像作平滑處理,因此具有較強(qiáng)的抑制噪聲能力,同樣該算子也會(huì)將一些高頻邊緣平滑掉,造成邊緣丟失。