# Lane Detection
In this project, MATLAB is used as an Image Processing Tool to detect Lanes on the road. The following techniques are used for lane detection.
• Color Masking
• Canny Edge Detection
• Region of Interest Selection
• Hough Transform Line detection
## Pre-processing the Image
The first step is to import the video file and initialize the variables to be use din the code. Some variables are also imported from the .mat file to be used in the code.
#### Initializing the loop to take frames one by one
First the frame is read and them filtered using a Gaussian Filter.
while hasFrame(VideoFile)
%------------------Reading each frame from Video File------------------
frame = readFrame(VideoFile);
figure('Name','Original Image'), imshow(frame);
frame = imgaussfilt3(frame);
figure('Name','Filtered Image'), imshow(frame);
![third_main_01](https://user-images.githubusercontent.com/31979840/36962601-337ce20e-201e-11e8-8bb9-6658713a0eaa.png)
Fig 1: Original Image
![third_main_02](https://user-images.githubusercontent.com/31979840/36962730-b1cc677e-201e-11e8-8da7-576e5c6b7953.png)
Fig 2: Filtered Image
#### Masking the image for White and Yellow Color
The frame is masked with yellow and white color to detect the lane lines perfectly.
%--------------Define Thresholds for masking Yellow Color--------------
%----------------------Define thresholds for 'Hue'---------------------
channel1MinY = 130;
channel1MaxY = 255;
%------------------Define thresholds for 'Saturation'------------------
channel2MinY = 130;
channel2MaxY = 255;
%---------------------Define thresholds for 'Value'--------------------
channel3MinY = 0;
channel3MaxY = 130;
%-----------Create mask based on chosen histogram thresholds-----------
Yellow=((frame(:,:,1)>=channel1MinY)|(frame(:,:,1)<=channel1MaxY))& ...
(frame(:,:,2)>=channel2MinY)&(frame(:,:,2)<=channel2MaxY)&...
(frame(:,:,3)>=channel3MinY)&(frame(:,:,3)<=channel3MaxY);
figure('Name','Yellow Mask'), imshow(Yellow);
%--------------Define Thresholds for masking White Color---------------
%----------------------Define thresholds for 'Hue'---------------------
channel1MinW = 200;
channel1MaxW = 255;
%------------------Define thresholds for 'Saturation'------------------
channel2MinW = 200;
channel2MaxW = 255;
%---------------------Define thresholds for 'Value'--------------------
channel3MinW = 200;
channel3MaxW = 255;
%-----------Create mask based on chosen histogram thresholds-----------
White=((frame(:,:,1)>=channel1MinW)|(frame(:,:,1)<=channel1MaxW))&...
(frame(:,:,2)>=channel2MinW)&(frame(:,:,2)<=channel2MaxW)& ...
(frame(:,:,3)>=channel3MinW)&(frame(:,:,3)<=channel3MaxW);
figure('Name','White Mask'), imshow(White);
![third_main_03](https://user-images.githubusercontent.com/31979840/36962750-cbdb6f7a-201e-11e8-9caf-125f2f7dd5ec.png)
Fig 3: Yellow Mask
![third_main_04](https://user-images.githubusercontent.com/31979840/36962769-e773c6d8-201e-11e8-882e-ad4417614587.png)
Fig 4: White Mask
## Edge Detection
In this section, edges are obtained from the masked image and closed edges with smaller areas are neglected.
frameW = edge(White, 'canny', 0.2);
frameY = edge(Yellow, 'canny', 0.2);
#### Neglecting closed edges in smaller areas
frameY = bwareaopen(frameY,15);
frameW = bwareaopen(frameW,15);
figure('Name','Detecting Edges of Yellow mask'), imshow(frameY);
figure('Name','Detecting Edges of White mask'), imshow(frameW);
![third_main_05](https://user-images.githubusercontent.com/31979840/36962811-0308eb08-201f-11e8-8edd-6ad85c217ed9.png)
Fig 5: Detecting Edges of Yellow Mask
![third_main_06](https://user-images.githubusercontent.com/31979840/36962826-0a661a88-201f-11e8-873b-8d627e97f257.png)
Fig 6: Detecting Edges of White Mask
## Extraction of Region of Interest
As guided in the pipeline for the implementation of the project 1 the region of interest is extracted using the 'roipoly' function and selecting the points from the frame.
#### Deciding ROI Points and Extracting ROI
%--------------Deciding ROI points by plotting it on image-------------
% figure(1)
% imshow(frame);
% [r c] = ginput(10);
%---------Extracting Region of Interest from Yellow Edge Frame---------
roiY = roipoly(frameY, r, c);
[R , C] = size(roiY);
for i = 1:R
for j = 1:C
if roiY(i,j) == 1
frame_roiY(i,j) = frameY(i,j);
else
frame_roiY(i,j) = 0;
end
end
end
figure('Name','Filtering ROI from Yellow mask'), imshow(frame_roiY);
%---------Extracting Region of Interest from White Edge Frame----------
roiW = roipoly(frameW, r, c);
[R , C] = size(roiW);
for i = 1:R
for j = 1:C
if roiW(i,j) == 1
frame_roiW(i,j) = frameW(i,j);
else
frame_roiW(i,j) = 0;
end
end
end
figure('Name','Filtering ROI from White mask'), imshow(frame_roiW);
![third_main_07](https://user-images.githubusercontent.com/31979840/36963131-0ea92c56-2020-11e8-87e5-0cfe445d8932.png)
Fig 7: Filtering ROI from Yellow Mask
![third_main_08](https://user-images.githubusercontent.com/31979840/36963142-1616979e-2020-11e8-9380-66ff72860e13.png)
Fig 8: Filtering ROI from White Mask
## Hough Transform
In this section I have used the hough function to get the hough transfrom of the binary edge detected image, which gives us the hough values and then I have plotted the hough plot as shown in the figure below.
#### Applying Hough Tansform to get straight lines from Image
%----------Applying Hough Transform to White and Yellow Frames---------
[H_Y,theta_Y,rho_Y] = hough(frame_roiY);
[H_W,theta_W,rho_W] = hough(frame_roiW);
%--------Extracting Hough Peaks from Hough Transform of frames---------
P_Y = houghpeaks(H_Y,2,'threshold',2);
P_W = houghpeaks(H_W,2,'threshold',2);
%----------Plotting Hough Transform and detecting Hough Peaks----------
figure('Name','Hough Peaks for White Line')
imshow(imadjust(rescale(H_W)),[],'XData',theta_W,'YData',rho_W,'InitialMagnification','fit');
xlabel('\theta (degrees)')
ylabel('\rho')
axis on
axis normal
hold on
colormap(gca,hot)
x = theta_W(P_W(:,2));
y = rho_W(P_W(:,1));
plot(x,y,'s','color','blue');
hold off
figure('Name','Hough Peaks for Yellow Line')
imshow(imadjust(rescale(H_Y)),[],'XData',theta_Y,'YData',rho_Y,'InitialMagnification','fit');
xlabel('\theta (degrees)')
ylabel('\rho')
axis on
axis normal
hold on
colormap(gca,hot)
x = theta_W(P_Y(:,2));
y = rho_W(P_Y(:,1));
plot(x,y,'s','color','blue');
hold off
%--------------Extracting Lines from Detected Hough Peaks--------------
lines_Y = houghlines(frame_roiY,theta_Y,rho_Y,P_Y,'FillGap',3000,'MinLength',20);
figure('Name','Hough Lines found in image'), imshow(frame), hold on
max_len = 0;
for k = 1:length(lines_Y)
xy = [lines_Y(k).point1; lines_Y(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
end
lines_W = houghlines(frame_roiW,theta_W,rho_W,P_W,'FillGap',3000,'MinLength',20);
max_len = 0;
for k = 1:2
xy = [lines_W(k).point1; lines_W(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
end
hold off
![third_main_09](https://use
weixin_38602563
- 粉丝: 3
- 资源: 933
最新资源
- uniyt相关.zip
- clickhouse-jdbc-0.3.1 jdbc驱动
- 基于stm32的频率计设计
- STM32F030单片机控制蜂鸣器.zip
- STM32F030单片机控制继电器.zip
- STM32F030单片机连接BC20、BC26NBiot模块MQTT协议数据上报阿里云物联网平台.zip
- STM32F030单片机连接BC20、BC26NBiot模块MQTT协议数据上报及下发阿里云物联网平台,并从NTP获取网络时间.zip
- STM32F030单片机连接BC20、BC26NBiot模块TCP透传数据.zip
- pscad仿真 采用pscad搭建220kv三相空载输电线路,仿真合空线,切空线过电压,仿真避雷器,合闸电阻法抑制合闸过电压,仿真控制断路器三相分别在线路相电压为0,30,60,90分合闸的抑制过电压
- 全流程实操+大数据+操作
- 操作系统试题库.doc
- 学生学籍管理系统的数据库设计与数据操作.docx
- 计算机文化基础实验指导书.doc
- 小区视频监控系统设计方案.doc
- 计算机数学基础模拟试题.doc
- 实验3软件项目的编码与测试实验报告.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0