Matlab实现Hough直线检测

基于Matlab实现图像的直线识别
代码如下:(有些参数需要自己根据图像情况调整)

clc;
clear;
close all;
%%
%图像分割
J=imread('C:\Users\pc\Desktop\屏幕截图 2023-01-25 214646.png');   %获取图像
G=rgb2gray(J);
I=G;
% I=roipoly(I);
I=im2double(I);
I=imnoise(I,'gaussian',0,0.001);                                  %添加高斯噪声
[BW,thresh]=edge(I,'log',[],6);
% BW=edge(I,'canny');                                             %获取图像边缘
%%
%Hough变换
[H,Theta,Rho]=hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89.5); %hough变换
P=houghpeaks(H,2,'threshold',ceil(0.2*max(H(:))));                %获取2个最值点
figure;
% subplot(121),
imshow(G);
% subplot(121),imshow(K);
% subplot(122),
figure;
imshow(BW);
hold on;
figure;
x=Theta(P(:,2));
y=Rho(P(:,1));
set(0,'defaultFigurePosition',[100,100,1000,500]);
set(0,'defaultFigurecolor',[1 1 1]);
% subplot(223);
imshow(imadjust(mat2gray(H)),'XData',Theta,'YData',Rho,...        %绘制Hough变换结果
    'InitialMagnification','fit');                  
axis on;                                                          %设置坐标轴
axis normal;
xlabel('\theta');
ylabel('\rho'); 
hold on;
plot(x,y,'s','color','red');
figure;
lines=houghlines(BW,Theta,Rho,P,'FillGap',100,'MinLength',5);
imshow(BW);
hold on;
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];
%     if lines(k).theta==0
    plot(xy(:,1),xy(:,2),'linewidth',1,'color','blue');
    plot(xy(1,1),xy(1,2),'linewidth',2,'color','yellow');
    plot(xy(2,1),xy(2,2),'linewidth',2,'color','red');
%     end
end 
%%
% %画整段直线
% for i=1:length(y)
%     xx=[y(i),y(i)];
%     yy=[0.5,767.5];
%     plot(xx,yy,'linewidth',2,'color','red');
% end
  • 5
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值