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