Lane tracking- matlab continue

lane

 

lane track, load pic. if you have camera, 

 strP='F:\work\clemson\camera\Image28.png'
pic=imread(strP);
 
imshow(pic)
grapy_pic=rgb2gray(pic);
edge_pic=edge(grapy_pic,'canny',[0.05 0.35]);
imshow(edge_pic)

have camera,load picture from camera, then use edge.

camLane = webcam('USB_Camera')
% preview(cam)
% closePreview(cam)
i=[1 10];i=1;
while(i<10)
    i=i+1;
img = snapshot(camLane);
image(img)
imwrite(img, strcat(num2str(i),'.jpg');

end

region mask

shape=size(pic);
% row coordinate
a=[shape(2)*0 shape(2)*1 shape(2)*1 0];
%column corordinate
b=[shape(1)*0.05 shape(1)*0.05 shape(1)*0.65 shape(1)*0.65 ];
%use a,b to specify the interesting area
bw=roipoly(pic,a,b);
BW=(edge_pic(:,:,1) & bw);
imshow(BW)

if you are confused by the roipoly function see "https://blog.csdn.net/meteor_033/article/details/54633205".  in the program, a, b are used to specified the square coner's coordinates. a for widith, b for height.

(camera: 480 *640)  a=[0    640    640    0] ,b=[24    24    312    312], so

hold on
plot(a, b)
hold off

hough transform to get line
y=mx+b

[H, T, R]=hough(BW);
P=houghpeaks(H,3);
lines=houghlines(BW,T,R,P,'FillGap',4,'MinLength',5);
imagesc(pic);
hold on;
%plot(lines(5).point1(1),lines(5).point1(2),'rd')
for i=1:length(lines)
  plot([lines(i).point1(1),lines(i).point2(1)],[lines(i).point1(2),lines(i).point2(2)],'Color','blue','LineWidth',4)
end

 

merge line

anglethres=0.01; %separate left/right by orientation threshold
leftlines=[];rightlines=[]; %Two group of lines
for k = 1:length(lines)
    x1=lines(k).point1(1);y1=lines(k).point1(2);
    x2=lines(k).point2(1);y2=lines(k).point2(2);
    if (x2>=shape(2)/2) && ((y2-y1)/(x2-x1)>anglethres)
        rightlines=[rightlines;x1,y1;x2,y2];
    elseif (x2<=shape(2)/2) && ((y2-y1)/(x2-x1)<(-1*anglethres))
        leftlines=[leftlines;x1,y1;x2,y2];
    end
end
draw_y=[shape(1)*0.1,shape(1)*0.65]; %two row coordinates
PL=polyfit(leftlines(:,2),leftlines(:,1),1);
draw_lx=polyval(PL,draw_y); %two col coordinates of left line
PR=polyfit(rightlines(:,2),rightlines(:,1),1);
draw_rx=polyval(PR,draw_y); %two col coordinates of right line
imagesc(pic);
hold on;
plot(draw_lx,draw_y,'LineWidth',2,'Color','red');
hold on
plot(draw_rx,draw_y,'LineWidth',2,'Color','red');
hold on

plot((draw_lx+draw_rx)/2,draw_y,'LineWidth',2,'Color','blue');


 

The result is good. but if the lane  is tilted to right, the above program will meet error, such leftlines or righlines will be missed.

if it is tilted,

 

use the line.theta or rho to decide, just average them ,you can test which is belonging to right line ,and left lane.

anglethres=0.01; %separate left/right by orientation threshold
leftlines=[];rightlines=[]; %Two group of lines
angle=0;
for k = 1:length(lines)
  angle=angle+lines(k).theta;
end
  aveAngle=angle/length(lines);

for k = 1:length(lines)
    x1=lines(k).point1(1);y1=lines(k).point1(2);
    x2=lines(k).point2(1);y2=lines(k).point2(2);
   LineAngle=(y2-y1)/(x2-x1)
%     if (x2>=shape(2)/2) && (LineAngle>anglethres)
%         rightlines=[rightlines;x1,y1;x2,y2];
%     elseif (x2<=shape(2)/2) && (LineAngle<(-1*anglethres))
%         leftlines=[leftlines;x1,y1;x2,y2];
%     end
    if lines(k).theta< aveAngle
         rightlines=[rightlines;x1,y1;x2,y2];
    elseif lines(k).theta>= aveAngle
        leftlines=[leftlines;x1,y1;x2,y2];
    end
    
end
draw_y=[shape(1)*0.1,shape(1)*0.65]; %two row coordinates
PL=polyfit(leftlines(:,2),leftlines(:,1),1);
draw_lx=polyval(PL,draw_y); %two col coordinates of left line
PR=polyfit(rightlines(:,2),rightlines(:,1),1);
draw_rx=polyval(PR,draw_y); %two col coordinates of right line
imagesc(pic);
hold on;
plot(draw_lx,draw_y,'LineWidth',2,'Color','red');
hold on
plot(draw_rx,draw_y,'LineWidth',2,'Color','red');
hold on
plot((draw_lx+draw_rx)/2,draw_y,'LineWidth',2,'Color','blue','LineStyle','--');
hold off

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一个码农都是奢望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值