运行环境:Matlab2014b
部分代码:
%% 视频流循环处理
% 创建一个循环过程来对给定视频进行车道线检测
% 该循环使用之前初始化的系统对象
warningTextColors = {[1 0 0], [1 0 0], [0 0 0], [0 0 0]};
while ~isDone(hVideoSrc)
RGB = step(hVideoSrc);
% 选择输入视频的下部(限制视野)
Imlow = RGB(NumRows+1:end, :, :);
% 边缘检测和Hough变换
Imlow = rgb2gray(Imlow); % Convert RGB to intensity
I = imfilter(Imlow, [-1 0 1], 'replicate','corr'); %imfilter函数:对任意类型数组或多维图像进行滤波
% 设置饱和值为0到1之间
I(I < 0) = 0;
I(I > 1) = 1;
th = multithresh(I); % 计算门槛
[H, Theta, Rho] = hough(I > th);
% 将角度制变量Theta转换成弧度制
Theta = Theta * pi / 180;
% 峰值检测
H1 = H;
% 删除H中满足下列条件的矩阵: theta < -78 deg and theta >= 78 deg
H1(:, 1:12) = 0;
H1(:, end-12:end) = 0;
Idx1 = houghpeaks(H1, ExpLaneNum, 'NHoodSize', [301 81], 'Threshold', 1);
Count1 = size(Idx1,1);
% 根据峰值来选择Rhos和Thetas
Line = [Rho(Idx1(:, 1)); Theta(Idx1(:, 2))];
Enable = [ones(1,Count1) zeros(1, ExpLaneNum-Count1)];
% 跟踪一组标记了的车道线
[Rep_ref, Count_ref] = videolanematching(Rep_ref, Count_ref, ...
MaxLaneNum, ExpLaneNum, Enable, Line, ...
TrackThreshold, frameFound+frameLost);
% 将极值点转换到笛卡尔坐标系
Pts = step(hHoughLines1, Rep_ref(2,:), Rep_ref(1,:), Imlow);
% 检测是否有向左或向右的车道偏离
[TwoValidLanes, NumNormalDriving, TwoLanes, OutMsg] = ...
videodeparturewarning(Pts, Imlow, MaxLaneNum, Count_ref, ...
NumNormalDriving, OutMsg);
% 输出信息的含义:
% 0 = 向右离开当前车道
% 1 = 正常驾驶
% 2 = 向左离开当前车道
% 检测车道线的颜色和类别
YCbCr = rgb2ycbcr(double(RGB(NumRows+1:240, :, :)));
ColorAndTypeIdx = videodetectcolorandtype(TwoLanes, YCbCr);
% 变量ColorAndTypeIdx的含义:
% 无效的颜色或类别 = int8(0);
% 黄虚线 = int8(1);
% 黄实线 = int8(2);
% 白虚线 = int8(3);
% 白实线 = int8(4).
% 输出
Frame = Frame + 1;
if Frame >= 5
TwoLanes1 = TwoLanes + [offset; offset]';
if DrawPoly && TwoValidLanes
if TwoLanes(4,1) >= 239
Templ = TwoLanes1(3:4, 1);
else
Templ = [0 239]';
end
if TwoLanes(4,2) >= 239
Tempr = TwoLanes1(3:4, 2);
else
Tempr = [359 239]';
end
Pts_poly = [TwoLanes1(:,1); Templ; Tempr; ...
TwoLanes1(3:4,2); TwoLanes1(1:2,2)];
% 在车道区域绘制多边形
RGB = insertShape(RGB,'FilledPolygon',Pts_poly.',...
'Color',[0 1 1],'Opacity',0.2);
end
% 绘制车道线
RGB = insertShape(RGB,'Line',TwoLanes1',...
'Color',{'yellow','magenta'});
% 插入车道变更警告文本 (空文本不会被绘制)
txt = warnText{OutMsg+1};
txtLoc = warnTextLoc(OutMsg+1, :);
txtColor = single(warningTextColors{mod(Frame-1,4)+1});
RGB = insertText(RGB,txtLoc,txt,'TextColor', txtColor, ...
'FontSize',20, 'BoxOpacity', 0);
% 插入描述车道线的颜色和种类信息的文本
for ii=1:2
% 空文本不会被绘制
txtLoc = TwoLanes1([1 2], ii)' + int32([0 -35]);
lineTxt = lineText{ColorAndTypeIdx(ii)};
txtColor = LaneColors(ColorAndTypeIdx(ii), :);
RGB = insertText(RGB,txtLoc,lineTxt,'TextColor',txtColor, ...
'FontSize',14, 'BoxOpacity', 0);
end
% 如果有必要,绘制第三条车道线
if OutMsgPre ~= OutMsg
ColorType = ColorAndTypeIdx(2-(OutMsg == 2));
Broken = ColorType == 2 || ColorType == 4;
end
ShowThirdLane = Broken && (OutMsg~=1);
if ShowThirdLane
if OutMsg == 0
% 寻找位于右边的第三条车道线
Idx2 = houghpeaks(H(startIdxRho_R:startIdxRho_R+NumRhos_R-1, ...
startIdxTheta_R:startIdxTheta_R+NumThetas_R-1), ...
'NHoodSize', [7 7], 'Threshold', 1);
Rhor = Rho(Idx2(:,1) + startIdxRho_R);
Thetar = Theta(Idx2(:,2) + startIdxTheta_R);
ThirdLane = step(hHoughLines3, Thetar, Rhor, Imlow);
else
% 寻找位于左边的第三条车道线
Idx3 = houghpeaks(H(startIdxRho_L:startIdxRho_L+NumRhos_L-1 , ...
startIdxTheta_L:startIdxTheta_L+NumThetas_L-1),...
'NHoodSize', [7 7], 'Threshold', 1);
Rhol = Rho(Idx3(:,1) + startIdxRho_L);
Thetal = Theta(Idx3(:,2) + startIdxTheta_L);
ThirdLane = step(hHoughLines3, Thetal, Rhol, Imlow);
end
OutThirdLane = videoexclude3rdlane(ThirdLane, ShowThirdLane,...
TwoLanes, TwoValidLanes, YCbCr);
OutThirdLane = OutThirdLane(:) + offset(:);
RGB = insertShape(RGB,'Line',OutThirdLane.','Color','green');
end
end
OutMsgPre = OutMsg;
step(myVideoOut, RGB); % 显示视频
end
运行截图:
代码获取链接:基于Matlab的车道线检测系统