当前位置:文档之家› 计算机视觉实验报告Experiment3

计算机视觉实验报告Experiment3

Experiment 3:Edge Detection
Class: 电子1203班Student ID: 1210910322 Name: 王影
Ⅰ. Aim
The aim of this laboratory session is to learn to deal with image data by Matlab. By the end of this session, you should be able to perform image preprocessing of edge detection in spatial domain and frequency domain.
Ⅱ. Knowledge required in the Experiment
ⅰ.You are supposed to have learned the basic skills of using Matlab;
ⅱ.You need to review Matlab programming language and M-file format.
ⅲ. You should have studied edge detection methods.
Ⅲ.Experiment Contents
Demand: Please show the figure on the left and list the codes on the right respectively bellow each question.(请将运行结果(图片)和程序代码贴在每题下方)
ⅰ.Read “car.jpg” file (to do this by imread function), convert the color image into grayscale image, and then perform edge detection using Roterts, Prewitt, Sobel operator separately in spatial domain and display the results in a Matlab window.
程序:
clear;
im=imread('car.jpg');
I=rgb2gray(im);
subplot(3,2,1);imshow(I);
title('Gray image');
[Y,X]=size(I);
im_edge=zeros(Y,X);
T=30;
for k=2:Y-1
for kk=2:X-1
im_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;
else
im_edge(k,kk)=0;
end
end
end
subplot(3,2,2);
imshow(im_edge,[]);% []ÈÃÊý¾Ý×Ô¶¯Ëõ·Åµ½0~255µÄ·¶Î§ÄÚ¡£
title('Robert image');
[Y X]=size(I);
imedge=zeros(Y,X);
for k=2:Y-1
for kk=2:X-1
imedge(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)) ;
end
end
subplot(3,2,3);
imshow(imedge,[]);
title('Prewit image');
[Y X]=size(I);
im_edge=zeros(Y,X);
for k=2:Y-1
for kk=2:X-1
im_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)) ;
end
end
subplot(3,2,4);
imshow(im_edge,[]);
title('Sobel image');
图像如下:
ⅱ.Read “car.jpg” file (to do this by imread function), convert the color image into grayscale image, then perform edge detection in frequency domain using Gaussian Highpass filter and display the result in a Matlab window.
第二题程序:
%频域边缘检测,利用Gassian高通滤波器进行滤波,进行边缘检测
%频域边缘检测,利用Gassian高通滤波器进行滤波,进行边缘检测
clear;
im=imread('car.jpg');
I=rgb2gray(im);
subplot(1,2,1);
imshow(I);
title('gray image');
%shifting image (multiply the image by (-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('Fourier spectrum');
%creat highpass filter
D=zeros(row,col);
u0=floor(row/2); %傅立叶变换中心v0=floor(col/2);
D0=40; %截止频率
n=2;
for i=1:row
for j=1:col
d=((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));
end
end
%filtering
G=F.*D;
%Invert the result and shifting
g=real(ifft2(G));
im=g.*(-1).^(X+Y);
im=im>40;%阈值确定edge
subplot(1,2,2);
imshow(im);
title('the image after Gassian highpass filter'); 图像如下:。

相关主题