%% 初始化
% 使用这些来初始化下部分代码的一些变量和系统对象
DrawPoly = 1; % 设置为0以绘制直线
NumRows = 120; % 要处理的视频区的行数(视图下部)
MaxLaneNum = 20; % 跟踪库中保存的最大车道数目
ExpLaneNum = 2; % 当前视频帧中需要检测的最大车道数目
Rep_ref = zeros(2, MaxLaneNum); % 存储的车道线
Count_ref = zeros(1, MaxLaneNum); % 车道线计数
TrackThreshold = 75; % 在2帧视频之间车道区域允许的最大变动距离
LaneColors = single([0 0 0;1 1 0; 1 1 0; 1 1 1;1 1 1]);
% 可以判定当前车道所需的最少帧数,即:5帧视频内未检测到偏离当前车道,则显示车道提示文本
frameFound = 5;
% 车道标记有效的最多帧数,即:车道提示文本最多保持20个帧的时长
frameLost = 20;
% 选择与原点距离为:35:45 (从1开始编号: 415:424)之间的直线
startIdxRho_R = 415;
NumRhos_R = 11;
% 选择斜率为:-90:-70deg (从1开始编号:: 1:21)之间的直线
startIdxTheta_R = 1;
NumThetas_R = 21;
% 选择与原点距离为:379:415 (从1开始编号:: 1:36)之间的直线
startIdxRho_L = 380;
NumRhos_L = 36;
% 对于斜率为:55:85deg (从1开始编号:: 146:176)的直线
startIdxTheta_L = 146;
NumThetas_L = 21;
% 显示车道线的偏移值
offset = int32([0, NumRows, 0, NumRows]);
%%
% 创建一个 |VideoFileReader| 系统对象以从文件读取视频
hVideoSrc = vision.VideoFileReader('viplanedeparture.avi');
%%
% 创建一个 |HoughLines| 系统对象以检测黄白线的笛卡尔坐标
hHoughLines1 = vision.HoughLines('SineComputation', 'Trigonometric function'); % Hough变化介绍:https://www.cnblogs.com/lancer2015/p/6852488.html
hHoughLines3 = vision.HoughLines('SineComputation', 'Trigonometric function');
%%
% 定义插入的车道偏离时的警告文本参数。
warnText = {sprintf('Right\n Departure'), '', sprintf(' Left\n Departure')};
warnTextLoc = [120 170;-1 -1; 2 170];
%%
% 定义插入的指定车道标记颜色/类型的文本参数
lineText = {'', ...
sprintf('Yellow\nBroken'), sprintf('Yellow\nSolid'), ...
sprintf('White\nBroken'), sprintf('White\nSolid')};
%%
%创建一个 |VideoPlayer| 系统对象以输出显示视频
myVideoOut = vision.VideoPlayer;
%%
% 初始化在视频流循环处理中使用到的变量
Frame = 0;
NumNormalDriving = 0;
OutMsg = int8(-1);
OutMsgPre = OutMsg;
Broken = false;
%% 视频流循环处理
% 创建一个循环过程来对给定视频进行车道线检测
% 该循环使用之前初始化的系统对象
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
%% 释放资源
release(hVideoSrc);
%% 总结
% 在样例视频中,你可以看到【车道】、【车道线】以及【离开当前车道的方式】被检测到的过程
%% 附录
% 在这个例子中使用到的函数:
% * <matlab:edit('videolanematching.m') videolanematching.m>
% * <matlab:edit('videodeparturewarning.m') videodeparturewarning.m>
% * <matlab:edit('videodetectcolorandtype.m') videodetectcolorandtype.m>
% * <matlab:edit('videoexclude3rdlane.m