
1 简介基于Hough霍夫曼算法实现直线检测matlab代码2 完整代码图片自行替换即可clear;clc;close all I1imread(图片\1.bmp); Irgb2gray(I1); indexfind(I50); I(index)0; BWim2bw(I); [H,T,R]hough(BW); imshow(H,[],XData,T,YData,R,InitialMagnification,fit); xlabel(\theta), ylabel(\rho); axis on axis normal hold on Phoughpeaks(H,5); xT(P(:,2)); yR(P(:,1)); plot(x,y,s,color,white); lineshoughlines(BW,T,R,P); figure imshow(I1) hold on k[]; b[]; for n1:length(lines) xy[lines(n).point1; lines(n).point2]; k(n)(xy(1,2)-xy(2,2))/(xy(1,1)-xy(2,1)1e-8); b(n)xy(1,2)-k(n)*xy(1,1); end k1[]; k2[]; b1[]; b2[]; alpharad2deg(atan(k)); distanceabs(alpha-alpha(1)); indexfind(distance120); distance(index)180-distance(index); index1find(distance45); index2find(distance45); k1k(index1); k2k(index2); b1b(index1); b2b(index2); for nindex1 xy[lines(n).point1; lines(n).point2]; plot(xy(:,1),xy(:,2),:g,LineWidth,1.5); end for nindex2 xy[lines(n).point1; lines(n).point2]; plot(xy(:,1),xy(:,2),:r,LineWidth,1.5); end x0[]; y0[]; count1; for i1:length(k1) for j1:length(k2) x0(count)(b1(i)-b2(j))/(k2(j)-k1(i)); y0(count)k1(i)*x0(count)b1(i); countcount1; end end plot(mean(x0),mean(y0),o,LineWidth,2.5)3 仿真结果4 参考文献[1]刘小豫, and 康世英. 基于Matlab的经典边缘检测算法实现及分析. 信息与电脑理论版 10(2014):2.