資源簡介
基于HOUGH變換的車道線檢測和識別的程序,可以使用
代碼片段和文件信息
%****************************************************************************
%功能說明:該段程序實現了對采集圖片的裁剪,可以提高計算速度;并可以檢測圖片中的直線,并將檢測到的直線畫于圖片中
%需改進部分:實現車道線的跟蹤,還有對彎曲車道線的擬合??對于道路上有障礙物、陰影時檢測效果不好??還需改進
%欲達到在前一次檢測出直線后,后續的直線檢測都在該直線附近很小的范圍內檢索,以提高實時性
clc;
clear?all;
close?all;
for?nn=1:2
aaa=[strcat(‘E:\DCIM\shipinchuli\shipinchuli1\‘)int2str(nn)‘.jpg‘];
%I=imread(strcat(‘E:\DCIM\shipinchuli\shipinchuli1\‘)int2str(i)‘?.jpg‘);
I=imread(aaa);
rs=size(I1);%行數
cs=size(I2);%列數
%ch=40;%由于固定值不使用,預改為圖像的1/5處
%cw=40;
ch=0.2*rs;%取圖像的下面4/5部分作為處理對象,因為上面部分對車道檢測沒用,故去掉,提高計算速度
cw=0.2*rs;
numr=round(rs/ch);
numc=round(cs/cw);
t1=(0:numr-1)*ch+1;
t2=(1:numr)*ch;
t3=(0:numc-1)*cw+1;
t4=(1:numc)*cw;
figure(1);
imshow(I);hold?on;
for?i=1:numr%畫出網格線
????for?j=1:numc
????????x=t1(i):t2(i);
????????y=t3(j):t4(j);
????????rectangle(‘Position‘[t3(j)?t1(i)?length(x)?length(y)]...
????????????‘EdgeColor‘‘r‘‘LineWidth‘2);
????end
end
I2=imcrop(I[0?ch?ch*numc?cw*numr]);%對圖像分塊后,將對研究不重要的塊除掉,保留感興趣區域,進行后續操作
if?nn==2
????I2=imcrop(I[lines(Index1).point1(1)-round((1/10)*cs)??lines(Index1).point2(2)?abs(lines(Index1).point2(1)-lines(Index1).point1(1))+2*round((1/10)*cs)?abs(lines(Index1).point1(2)-lines(Index1).point2(2))]);
end
figure(2)imshow(I2)
%I2=rgb2gray(imread(‘E:\DCIM\shipinchuli\shipinchuli1\5.jpg‘));%讀取視頻中某一幀圖像
I2=rgb2gray(I2);%對上面經過感興趣區域提取后的圖像進行直線檢測
%********大津法計算閾值****************
%tt=graythresh(I2);%大津法計算閾值??效果不是很好?可以沒有這兩句?有待改進?
%I2=im2bw(I2tt);
%***************************************************************
%I2=histeq(I2);%直方圖均衡化?這里不需要
figure(3)
imhist(I2)%顯示該幅圖像的直方圖
%I=rgb2gray(imread(‘E:\DCIM\100DICAM\DSCI0018.JPG‘));
I2=medfilt2(I2);%加入中值濾波
%I=filter2(fspecial(‘average‘3)I1)/255;%均值濾波
%I=wiener2(I2[5?5]);%自適應維納濾波
rotI=imrotate(I20‘crop‘);%將圖像旋轉25度?旋轉角度不同?檢測效果不同
figure(4)
subplot(221)fig1=imshow(rotI);
BW=edge(rotI‘canny‘);%使用canny邊緣檢測
%****************迭代法計算閾值(先放這,加不加效果一樣)**********************
zmax=max(max(BW));%求最大灰度值
zmin=min(min(BW));%求最小灰度值
TK=(zmax+zmin)/2;
bcal=1;
isize=size(BW);
while(bcal)
iforeground=0;
ibackground=0;
foregroundsum=0;
backgroundsum=0;
for?i=1:isize(1)
????for?j=1:isize(2)
????????tmp=I2(ij);
????????if(tmp>=TK)
????????????iforeground=iforeground+1;
????????????foregroundsum=foregroundsum+double(tmp);
????????else
????????????ibackground=ibackground+1;
????????????backgroundsum=backgroundsum+double(tmp);
????????end
????end
end
zo=foregroundsum/iforeground;
zb=backgroundsum/ibackground;
TKtmp=uint8((zo+zb)/2);
if(TKtmp==TK)
????bcal=0;
else
????TK=TKtmp;
end
end
BW=im2bw(BWdouble(TK)/255);
%BW=bwmorph(BW‘skel‘Inf);%骨架提取函數
subplot(222)imshow(BW);
[Hthetarho]=hough(BW);%hough變換
subplot(223);
imshow(imadjust(mat2gray(H))[]‘XData‘theta‘YData‘rho...
????‘InitialMagnification‘‘fit‘);
xlabel(‘\theta?(degrees)‘)ylabel(‘\rho‘);
axis?onaxis?normalhold?on;
colormap(hot)
P=houghpeaks(H5‘threshold‘ceil(0.3*max(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6492??2012-05-30?22:38??chedaoxianjianceok.m
-----------?---------??----------?-----??----
?????????????????6492????????????????????1
評論
共有 條評論