霍夫变换实现直线检测
博客地址:https://blog.csdn.net/Fly_Fly_Zhang/article/details/89218641
代码实现:
clear;
close all;
img = imread('C:\Users\lenovo\Desktop\1(1).bmp');
figure(1),
subplot(1,2,1);
imshow(img);
title('原始图像');
img=rgb2gray(img); % 灰度图像
subplot(1,2,2);
imshow(img);
title('灰度图像');
thresh=[0.01,0.10]; %敏感度阈值
sigma=3;%定义高斯参数
f = edge(double(img),'canny',thresh,sigma); %边缘检测
figure(2),
imshow(f);
title('canny 边缘检测');
% 检测函数;
[H, theta, rho]= hough(f,'Theta', 20:0.1:75); %0-1
% H->累计数组 , thetaH:对应的θ,实际上 H 的大小就是 Rho×Theta
% Rho:H 对应的ρ
peak=houghpeaks(H,1); %峰值提取
hold on %保留当前的图和特定的坐标轴属性,以便后续的绘图命令添加到现有的图表。
%得到线段信息
lines=houghlines(f,theta,rho,peak);
figure(3);
imshow(f,[]);
title('霍夫变换检测结果');
hold on ;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]);
end