Experiment3:EdgeDetectionⅠ.AimTheaimofthislaboratorysessionistolearntodealwithimagedatabyMatlab.Bytheendofthissession,youshouldbeabletoperformimagepreprocessingofedgedetectioninspatialdomainandfrequencydomain.Ⅱ.KnowledgerequiredintheExperimentⅰ.YouaresupposedtohavelearnedthebasicskillsofusingMatlab;ⅱ.YouneedtoreviewMatlabprogramminglanguageandM-fileformat.ⅲ.Youshouldhavestudiededgedetectionmethods.Ⅲ.ExperimentContentsDemand:Pleaseshowthefigureontheleftandlistthecodesontherightrespectivelybelloweachquestion.(请将运行结果(图片)和程序代码贴在每题下方)ⅰ.Read“car.jpg”file(todothisbyimreadfunction),convertthecolorimageintograyscaleimage,andthenperformedgedetectionusingRoterts,Prewitt,SobeloperatorseparatelyinspatialdomainanddisplaytheresultsinaMatlabwindow.程序:clear;im=imread('car.jpg');I=rgb2gray(im);subplot(3,2,1);imshow(I);title('Grayimage');[Y,X]=size(I);im_edge=zeros(Y,X);T=30;fork=2:Y-1forkk=2:X-1im_edge(k,kk)=abs(I(k+1,kk+1)-I(k,kk))+abs(I(k,kk+1)-I(k+1,kk));if(im_edge(k,kk)>T)im_edge(k,kk)=1;elseim_edge(k,kk)=0;endendendsubplot(3,2,2);imshow(im_edge,[]);%[]ÈÃÊý¾Ý×Ô¶¯Ëõ·Åµ½0~255µÄ·¶Î§ÄÚ¡£title('Robertimage');[YX]=size(I);imedge=zeros(Y,X);fork=2:Y-1forkk=2:X-1imedge(k,kk)=abs(I(k-1,kk+1)-I(k-1,kk-1))+abs(I(k,kk+1)-im(k,kk-1))+abs(I(k+1,kk+1)-I(k+1,kk-1))+...abs(I(k+1,kk-1)-I(k-1,kk-1))+abs(I(k+1,kk)-I(k-1,kk))+abs(I(k+1,kk+1)-I(k-1,kk+1));endendsubplot(3,2,3);imshow(imedge,[]);title('Prewitimage');[YX]=size(I);im_edge=zeros(Y,X);fork=2:Y-1forkk=2:X-1im_edge(k,kk)=abs(I(k-1,kk+1)-I(k-1,kk-1))+2*abs(I(k,kk+1)-I(k,kk-1))+abs(I(k+1,kk+1)-I(k+1,kk-1))+...abs(I(k+1,kk-1)-I(k-1,kk-1))+2*abs(I(k+1,kk)-I(k-1,kk))+abs(I(k+1,kk+1)-I(k-1,kk+1));endendsubplot(3,2,4);imshow(im_edge,[]);title('Sobelimage');图像如下:ⅱ.Read“car.jpg”file(todothisbyimreadfunction),convertthecolorimageintograyscaleimage,thenperformedgedetectioninfrequencydomainusingGaussianHighpassfilteranddisplaytheresultinaMatlabwindow.第二题程序:%频域边缘检测,利用Gassian高通滤波器进行滤波,进行边缘检测%频域边缘检测,利用Gassian高通滤波器进行滤波,进行边缘检测clear;im=imread('car.jpg');I=rgb2gray(im);subplot(1,2,1);imshow(I);title('grayimage');%shiftingimage(multiplytheimageby(-1)x+y)[row,col]=size(I);[Y,X]=meshgrid(1:col,1:row);II=double(I).*(-1).^(X+Y);F=fft2(II);%subplot(2,2,2);%title('Fourierspectrum');%creathighpassfilterD=zeros(row,col);u0=floor(row/2);%傅立叶变换中心v0=floor(col/2);D0=40;%截止频率n=2;fori=1:rowforj=1:cold=((i-u0)^2+(j-v0)^2)^0.5;%D(i,j)=1/(1+(D0/d)^(2*n));D(i,j)=1-exp((-d^2)/(2*(D0)^2));endend%filteringG=F.*D;%Inverttheresultandshiftingg=real(ifft2(G));im=g.*(-1).^(X+Y);im=im>40;%阈值确定edgesubplot(1,2,2);imshow(im);title('theimageafterGassianhighpassfilter');图像如下: