Matlab 检测直线并求解直线方程
生活随笔
收集整理的這篇文章主要介紹了
Matlab 检测直线并求解直线方程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先對圖像進(jìn)行簡單處理,讓圖像相對平滑。然后二值化圖像,調(diào)整合適的閾值,使二值化后的圖像盡可能的干凈。然后對圖像進(jìn)行霍夫變換求解直線的起點(diǎn)與終點(diǎn)。用兩點(diǎn)式求解直線方程。大功告成!
#閾值可以改。閾值改大之后可以把斷掉的直線畫到一條上
?
?
%Matlab Code close all; clear ;clc I = imread('1.jpg'); b=medfilt2(I,[3,3]);%中值濾波 A=imbinarize(b,0.65); %二值化figure; subplot(1,3,1); imshow(I,[]); BW = edge(A,'canny');%Canny [H,T,R] = hough(BW);%計算二值圖像的標(biāo)準(zhǔn)霍夫變換,H為霍夫變換矩陣,I,R為計算霍夫變換的角度和半徑值 ,'RhoResolution',0.5,'Theta',-10:0.5:10 subplot(1,3,2); imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');%hough變換的圖像 xlabel('\theta'), ylabel('\rho'); axis on,axis square,hold on;P = houghpeaks(H,10,'threshold',0.7*max(H(:)));%提取3個極值點(diǎn) x = T(P(:,2)); y = R(P(:,1)); plot(x,y,'s','color','white');%標(biāo)出極值點(diǎn)lines=houghlines(BW,T,R,P);%提取線段 subplot(1,3,3); imshow(I), 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,1),xy(1,2),'x','lineWidth',2,'Color','yellow');%線段起點(diǎn)plot(xy(2,1),xy(2,2),'x','lineWidth',2,'Color','red'); %線段終點(diǎn)len=norm(lines(k).point1-lines(k).point2);%兩點(diǎn)式求直線:aX + bY + c= 0x1 = xy(1,1); y1 = xy(1,2);x2 = xy(2,1); y2 = xy(2,2);a = y2 - y1;b = x1 - x2;c = y1*x2 - x1*y2;Text = sprintf( '直線的方程是:%.2fX + %.2fY + %.2f= 0', a, b, c);disp (Text);if(len>max_len)max_len=len;xy_long=xy;end end plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');總結(jié)
以上是生活随笔為你收集整理的Matlab 检测直线并求解直线方程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阿里云大数据计算服务MaxCompute
- 下一篇: 苹果Iphone/Ipad--L2T虚拟