边缘检测Matlab程序小节

几种边缘检测仿真

I=imread('F:\ZPB\lena.jpg');
a1=edge(I,'sobel');
a2=edge(I,'prewitt');
a3=edge(I,'roberts');
a4=edge(I,'log');
a5=edge(I,'canny');

subplot(2,3,1);imshow(I);title('原图像');
subplot(2,3,2);imshow(a1);title('sobel');
subplot(2,3,3);imshow(a2);title('prewitt');
subplot(2,3,4);imshow(a3);title('roberts');
subplot(2,3,5);imshow(a4);title('log');
subplot(2,3,6);imshow(a5);title('canny');


先高斯滤波再边缘检测

clear all;
close all;
clc;

img=imread('F:\ZPB\360截屏\lena.jpg');
imshow(img);
[m n]=size(img);
img=double(img);

%%canny边缘检测的前两步相对不复杂,所以我就直接调用系统函数了
%%高斯滤波
w=fspecial('gaussian',[5 5]);
img=imfilter(img,w,'replicate');
figure;
imshow(uint8(img))

%%sobel边缘检测
w=fspecial('sobel');
img_w=imfilter(img,w,'replicate'); %求横边缘
w=w';
img_h=imfilter(img,w,'replicate'); %求竖边缘
img=sqrt(img_w.^2+img_h.^2); %注意这里不是简单的求平均,而是平方和在开方。我曾经好长一段时间都搞错了
figure;
imshow(uint8(img))


用Hough变换求边缘检测

I=imread('F:\ZPB\360截屏\lena.jpg');
%旋转图像并寻找边缘
rotI=imrotate(I,33,'crop');
BW = edge(rotI,'canny');
[H,T,R]=hough(BW);
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on,axis normal,hold on;
%在Hough矩阵中寻找前5个大于Hough矩阵中最大值0.3倍的峰值
P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
x=T(P(:,2));y=R(P(:,1));
plot(x,y,'s','color','white');
%找到并绘制直线
lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
%合并距离小于5的线段,丢弃所有长度小于7的直线段
figure,imshow(rotI),hold on
max_len=0;
for k=1:length(lines)
%依次标出各条直线段
xy=[lines(k).point1;lines(k).point2;]
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%绘制线段段点
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
%确定最长的线段
len=norm(lines(k).point1-lines(k).point2);
if(len>max_len)
max_len=len;
xy_long=xy;
end
end

原文地址:https://www.cnblogs.com/qxql2016/p/3834660.html