基于Matlab实现图像的直线识别
代码如下:(有些参数需要自己根据图像情况调整)文章来源地址https://www.toymoban.com/news/detail-761394.html
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
文章来源:https://www.toymoban.com/news/detail-761394.html
到了这里,关于Matlab实现Hough直线检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!