MATLAB车道线识别系统
一、核心代码
function imgout = lanemarker(img)
global top bot
persistent tform
if isempty(tform)
movingPoints = [203 720; 585 460; 695 460 ; 1127 720];
fixedPoints = [320 720; 320 0; 960 0; 960 720];
tform = fitgeotrans(movingPoints,fixedPoints,'projective');
top = 220;
bot = 12;
end
topview = view2top(img,tform);
[flanel,flaner] = lanefinder(topview);
hpic = size(topview,1);
y = 1:hpic;
y = y';
xl = polyval(flanel,y);
xr = polyval(flaner,y);
dim = size(topview);
dim = [dim,3];
imref = imref2d(size(topview));
lanel = reshape([xl';y'],1,[]);
laner = [xr';y'];
laner = fliplr(laner);
laner = reshape(laner,1,[]);
xpic = zeros(dim,'uint8');
xpic = insertShape(xpic,'FilledPolygon',[lanel,laner],'Color',[0,255,0],'Opacity',0.3);
xpic = imwarp(xpic,tform.invert,'OutputView',imref);
img = imlincomb(1,img,1,xpic);
xpic = zeros(dim,'uint8');
xpic = insertShape(xpic,'Line',lanel,'LineWidth',20,'Color',[255,0,0],'Opacity',1);
xpic = insertShape(xpic,'Line',laner,'LineWidth',20,'Color',[255,0,0],'Opacity',1);
xpic = imwarp(xpic,tform.invert,'OutputView',imref);
imgout = imlincomb(1,img,1,xpic);
end
上面是主程序view2top函数是将图转换为俯视图的函数,就是由左图变成右边的图。view2top函数由很多函数来完成工作,下面做一些必要说明。
function topview = view2top(img,tform)
global top bot
th_sobelx = [35, 100];
th_sobely= [30, 255];
th_mag = [30, 255];
th_dir = [0.7, 1.3];
th_h = [10, 100];
th_s = [85, 255];
th_l = [0, 60];
gradient_comb = gradient_combine(img, th_sobelx, th_sobely, th_mag, th_dir);
hsl_comb = hsl_combine(img, th_h, th_s, th_l);
dim = size(img);
combined_result = zeros(dim(1:2));
combined_result = uint8(combined_result);