資源簡介
輸入圖片后進行灰度化、邊緣檢測、道路直線檢測
代碼片段和文件信息
%?入口圖像為?BW,出口圖像為f??
%optimize?from?main_optimize?merely?select?2?lines?one?has?positive??
%slopethe?other?has?negative?slope??
clear?allclose?all??
BW=imread(‘daolu.jpg‘);??
figureimshow(BW);??
??
BW=rgb2gray(BW);??
thresh=[0.010.17];??
thresh=[0.010.10];??
sigma=2;%定義高斯參數(shù)??
f?=?edge(double(BW)‘canny‘threshsigma);??
%f?=?edge(double(BW)‘sobel‘);??
figuresubplot(121);??
imshow(f[]);??
title(‘canny?Edge?Detect?Result‘);??
??
[H?theta?rho]=?hough(f?0.1);%cos(theta)*x+sin(theta)*y=rho??
%imshow(thetarhoH[]‘notruesize‘)axis?onaxis?normal??
%xlabel(‘\theta‘)ylabel(‘rho‘);??
??
[rc]=houghpeaks(H10);??
hold?on??
??
??
lines=houghlines(fthetarhorc);??
??
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-1start=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);??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2012-07-24?03:28??daolu\
?????文件???????56371??2012-02-25?14:27??daolu\邊緣檢測.JPG
?????文件???????87932??2012-02-25?06:49??daolu\Snap6.jpg
?????文件????????1126??2012-02-25?08:33??daolu\shibie.m
?????文件????????1032??2012-02-25?08:16??daolu\huiduhua.m
?????文件???????19358??2012-02-26?12:25??daolu\hh.JPG
?????文件????????2326??2012-02-27?12:59??daolu\daolu1.m
評論
共有 條評論