matlab已知斜率画直线,MATLAB 霍夫变换 连接斜率相同的直线
基于matlab的霍夫變換的應用,傳統的霍夫變換可以檢測圖像中的直線,但是生活中一般都會出現遮擋物,導致一條直線發生中斷,于是在傳統霍夫變換的基礎上做了修改,進而保證了檢測直線的整體性。
matlab 代碼實現如下:
% 入口圖像為 BW,出口圖像為f
%optimize from main_optimize, merely select 2 lines, one has positive
%slope,the other has negative slope
clear all,close all
BW=imread('p.jpg');
figure,imshow(BW);
BW=rgb2gray(BW);
%thresh=[0.01,0.17];
thresh=[0.01,0.10];
sigma=2;%定義高斯參數
f = edge(double(BW),'canny',thresh,sigma);
figure,subplot(121);
imshow(f,[]);
title('canny Edge Detect Result');
[H, theta, rho]= hough(f);
peaks=houghpeaks(H,10);
hold on
lines=houghlines(f,theta,rho,peaks);
subplot(122);
imshow(f,[]),title('Hough Transform Detect Result'),hold on
nlind=0;%new line index
st=1;
%%%%%%%%%求斜率%%%%%%%%%%%%
for k=1:length(lines)
%xy=[lines(k).point1;lines(k).point2];
xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001)
end
%%%%%%%%%將相同斜率的直線連起來%%%%%%%%%%%%
k=1;
while(k<=length(lines))
if(k~=length(lines))
k=k+1;
end
while(abs(xielv(k)-xielv(k-1))<0.0001)
k=k+1;
if(k>length(lines))
break;
end
end
if(abs(xielv(k-1))<0.05||abs(xielv(k-1))>=10)%eliminate horizontal and vertical lines
st=k;
if(k~=length(lines))
continue;
end
end
if(st==length(lines)&&k==st)
if(abs(xielv(k))>0.05&&abs(xielv(k))<10)
nlind=nlind+1;
newlines(nlind)=lines(st);
newlines(nlind).point2=lines(k).point2;
newxy=[newlines(nlind).point1;newlines(nlind).point2];
plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);
end
break;
end
%end=k-1,start=st;
draw line
nlind=nlind+1;
newlines(nlind)=lines(st);
newlines(nlind).point2=lines(k-1).point2;
newxy=[newlines(nlind).point1;newlines(nlind).point2];
plot(newxy(:,2),newxy(:,1),'LineWidth',4,'Color',[.6 1.0 .8]);
st=k;
end
fprintf('%d lines are detected in sum.\n',nlind);
總結
以上是生活随笔為你收集整理的matlab已知斜率画直线,MATLAB 霍夫变换 连接斜率相同的直线的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es中的一些知识点记录
- 下一篇: 某项目的双代号网络图如下所示_2019一