Lane Detection System

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 67

Lane Detection System

Submitted in partial fulfillment of the requirements for the degree of

Bachelor of Technology

in

Computer Science and Engineering

by

NISHETA GUPTA, 19BCE2233

ESHANA MOHAN, 19BCE2216

Under the guidance of


Dr. Rajkumar R

School of Computer Science and Engineering

VIT, Vellore.

May, 2023
DECLARATION

I hereby declare that the thesis entitled “Lane Detection System”


submitted by me, for the award of the degree of Bachelor of Technology in
Programme to VIT is a record of bonafide work carried out by me under the
supervision of Dr. Rajkumar R.
I further declare that the work reported in this thesis has not been submitted
and will not be submitted, either in part or in full, for the award of any other degree or
diploma in this institute or any other institute or university.

Place: Vellore
Date : 12.05.23
Eshana Mohan

2
CERTIFICATE

This is to certify that the thesis entitled “Lane Detection System” submitted by Eshana
Mohan 19BCE2216, School of Computer Science and Engineering, VIT, for the award of
the degree of Bachelor of Technology in Computer Science and Engineering, is a record of
bonafide work carried out by him / her under my supervision during the period, 01. 07. 2022
to 30.04.2023, as per the VIT code of academic and research ethics.

The contents of this report have not been submitted and will not be submitted either in part or
in full, for the award of any other degree or diploma in this institute or any other institute or
university. The thesis fulfills the requirements and regulations of the University and in my
opinion meets the necessary standards for submission.

Place : Vellore
Date : 12th May 2023 Signature of the Guide

Internal Examiner External Examiner

Head of the Department

Programme

3
ACKNOWLEDGEMENTS

I, Eshana Mohan, would like to express my sincere gratitude to my Internal Project guide,
Dr Rajkumar R, who guided me at every stage of the project, from its conception to its
completion. His support and mentorship were invaluable to this project and his
determination to help me gain maximum learning is commendable.

I would also like to thank Vellore Institute of Technology, for providing me to utilise this
great opportunity to apply the knowledge I gained from my studies over the past four years.

Eshana Mohan

4
Executive Summary

Although autonomous vehicles have been fantasized about and even attempted almost since
the advent of the automobile, they have really begun to pick up steam in the past couple of
years. There are many important benefits, including potential easing of traffic congestion
and potential to lower pollution if concepts like ride- sharing continue to increase in use.
Self-driving cars use a blend of machine learning, computer vision and various other areas
in order to take over the actions of a human driver, providing for safer and more reliable
travel.

We used deep learning to implement this project because a deep learning model may be
more effective at determining the important features in a given image than a human being
using manual gradient and color thresholds in typical computer vision techniques, as well as
other manual programming needed within that process. Specifically, we plan to use a
convolutional neural network (CNN), which is very effective at finding patterns within
images by using filters to find specific pixel groupings that are important. The aim is for the
end result to be both more effective at detecting the lines as well as faster at doing so than
common computer vision techniques.

5
Contents
ACKNOWLEDGEMENTS 4
Executive Summary 5
CONTENTS Page 8
List of Figures 10
List of Tables 11
List of Abbreviations 12
Symbols and Notations 13
1. Introduction 14
1.1. Theoretical Background 14
1.2. Motivation 15
1.3. Aim of the Proposed Work 15
1.4. Objective(s) of the Proposed Work 16
2. Literature Survey 17
2.1. Survey of the Existing Models/ Work 17
2.2. Gaps identified in the Survey 20
3. Overview of Proposed System 23
3.1. Introduction and Related Concepts 23
3.2. Framework for the Proposed System 25
4. Proposed System Analysis and Design 26
4.1. Introduction 26
4.2. Requirement Analysis 27
4.2.0. Functional Requirements 27
4.2.0.0. Product Perspective 27
4.2.0.1. Product Features 28
4.2.0.2. User Characteristics 29
4.2.0.3. Assumption & Dependencies 30
4.2.0.4. Domain Requirements 31
4.2.0.5. User Requirements 32
4.2.1. Non Functional Requirements 33
4.2.1.0. Reliability 33
4.2.1.1. Portability 34
4.2.1.2. Usability 35
4.2.2. Organizational Requirements 36
4.2.2.0. Implementation Requirements (in terms of deployment) 36
4.2.2.1. Engineering Standard Requirements 37
4.2.3. Operational Requirements 38
4.2.4. System Requirements 39

6
4.2.4.0. Hardware Requirements 39
4.2.4.1. Software Requirements 39
5. Project Demonstration 40
6. Results Discussion 65
7. References 67

7
List of Figures

Figure Index Page No.


No.
2.1 Framework of Proposed System 24
4.1 First block of code 41

4.2 Second block of code 44

4.3 Third block of code 46

4.4 Fourth block of code 48

4.5 Fifth block of code 49

4.6 Sixth block of code 51

4.7 Seventh block of code 53

4.8 Eighth block of code 54

4.9 Ninth block of code 55

4.10 Tenth block of code 57

5.1 Original Image 59


5.2 Image after Histogram Equalisation 59

5.3 Original Image 61


5.4 Image after Adaptive Histogram 61
Equalisation

5.5 Original Image


5.6 Image after CLAHE 63

5.7 Screenshot of Final Video Output 65

8
List of Tables

Table No. Title Page No.


1.0 Merits and demerits of papers reviewed 17

9
1. INTRODUCTION

1.1. Theoretical Background

Under normal driving circumstances, humans are fairly adept at recognizing lane line
markers when they are completely focused. One of the most common tasks while driving,
although likely overlooked due to its constant use when a human drives a car, is keeping the
car in its lane. As long as a person is not distracted, inebriated, or otherwise incapacitated,
most people can do this after basic training. However, what comes very simply to a person –
keeping the car between its lane’s lines – is a much harder problem for a computer to solve.

Why is this complicated for a computer? To begin with, a computer does not inherently
understand what the yellow and white streaks on a road are, the shifts in pixel values
between those and the pixels representing the road in a video feed. One way to help a
computer learn to at least detect these lines or the lanes itself is through various computer
vision techniques, including camera calibration (removing the distortion inherent to the
camera used), color and gradient thresholds (areas of the image where certain colors or
changes in color are concentrated), perspective transformation (similar to obtaining a
bird’s-eye view of the road), and more.

10
1.2. Motivation
Detecting lanes by only using the lane marking is a huge challenge presently faced by
independent driving vehicles. In recent times, lane discovery systems have aroused great
interest from both academia and assiduity where a machine vision- grounded system is used
for lane discovery. Primarily due to its safety aspect where it can help motorists in the
perception of any dangerous situations before, to avoid accidents. Traffic accidents
frequently occur due to negligence of the motorist, similar accidents can be avoided if
detected beforehand and advised to other motorists. A lane discovery system is a machine
literacy system that plays an important part in landing the surroundings, including the road
face and object. Motorists, as well as cyclers, can admit support and specific information
from lane discovery systems. By giving some kind of pointers, similar systems can enable
motorists to travel securely and help them with staying in their specific lane. During scripts
which involve unforeseen change in the structure of the markings on the road similar to
deformation or change in illumination, the cameras and detectors used will still be suitable
to identify the designated lane for the vehicle. Lane discovery efficiently helps in reducing
the business traffic as the system fully works according to the road business sense.
According to a study published by WHO,1.35 million mortal deaths were brought on by
injuries sustained in motor vehicle accidents. This number could be kindly reduced by
independent vehicles.

11
1.3. Aim of the Proposed Work
The aim of the proposed work is to develop a robust and accurate lane detection system. The
system will utilize computer vision techniques and algorithms to analyze input from a video
or image stream and accurately detect and track lane boundaries on the road.

The main objectives of the proposed work include:

1. Lane Detection: The system should be able to identify and extract lane markings from the
input video or image stream. This involves preprocessing the input data, applying
appropriate filters, and employing edge detection algorithms to detect lane boundaries.

2. Lane Tracking: Once the lane boundaries are detected, the system should track their
positions over time. This involves analyzing the temporal coherence of lane markings to
accurately track the lanes as the vehicle moves.

3. Lane Classification: In addition to detecting and tracking the lanes, the system should also
classify different types of lane markings such as solid lines, dashed lines, double lines, and
road edge markings. This information can be used for further analysis and decision-making
in autonomous vehicles or driver-assistance systems.

4. Robustness and Adaptability: The proposed system should be robust to various


environmental conditions such as different lighting conditions, weather conditions, and road
surface variations. It should also be adaptable to different road types, including highways,
urban roads, and curved roads.

5. Real-time Performance: The system should be designed to operate in real-time, providing


lane detection and tracking results at a sufficiently high frame rate to be used in real-world
applications. Efficient algorithms and optimization techniques should be employed to ensure
real-time performance on resource-constrained platforms.

12
By achieving these objectives, the proposed work aims to develop an effective and reliable
lane detection system that can contribute to various applications, including autonomous
driving, advanced driver-assistance systems, and road safety enhancement.

1.4. Objective(s) of the Proposed Work

In this project, we will train a deep learning model to be faster and more reliable than the
initial computer vision-based model. The "convolutional" neural network, or "CNN,"
architecture will serve as the model's foundation because it is known to perform well on
picture data. This is a fantastic architecture contender because we will train the model using
driving videos as input. CNN's are effective in finding patterns in images because they start
by searching for small-scale patterns at the pixel level (groups of pixels close together)
before moving on to larger patterns in extended regions of the image. By the end of this
project we aim to create a model that self detects lanes for driving automobiles.

1. Literature Survey

To understand how we can solve the problem better, existing literature on the topic and
similar topics were reviewed. This helped in identifying the advancements and research
being done in the automotive industry for lane detection systems using deep learning.The
following are summarisations of the insights received from these papers.

1.1. Survey of the Existing Models/ Work

Garnett, Noa, et al. [1] proposed a network that directly predicts the 3D layout of lanes in a
road scene from a single image. The network architecture, 3D-LaneNet, applies two new
concepts: intra-network inverse-perspective mapping (IPM) and anchor-based lane
representation. The intra-network IPM projection facilitates a dual-representation
information flow in both regular image-view and top-view. An anchor-per-column output
representation enables our end-to-end approach which replaces common heuristics such as

13
clustering and outlier rejection, casting lane estimation as an object detection problem. They
apply two new concepts of infra network inverse mapping and anchor based lane
representation, however, since the 3D images are taken once and then worked upon, a lot of
manual effort would be put in every time an existing road is altered or a new road is built.

Tabelini, Lucas, et al. [2] propose an in-depth approach using a lesser used algorithm that
has an anchor-based deep lane detection model called LaneATT, which uses the anchors for
the feature pooling step. They hypothesize that in some cases global information may be
crucial to infer their positions, especially in conditions such as occlusion, missing lane
markers, and others. Thus, this work proposes a novel anchor-based attention mechanism
that aggregates global information. The results showed both higher efficacy and efficiency.

De Brabandere, Bert, et al. [3] proposed a method to train a lane detector in an end-to-end
manner, directly regressing the lane parameters. Their method relies on the observation that
it is possible to back propagate through a least-squares fitting procedure. Additionally, the
system is not just a black box but offers a degree of interpretability because the
intermediately generated segmentation-like weight maps can be inspected and visualized.
The proposed methodology can backpropagate to discover any issues with the algorithm and
autocorrect it automatically, however, it will still be difficult to identify issues that occur in
lesser developed cities where edge cases such as two lane traffic also needs to be considered
while designing such models.

Hou, Yuenan, et al. [4] focused on the prospects of a novel knowledge distillation approach,
i.e., Self Attention Distillation (SAD), which allows a model to learn from itself and gains
substantial improvement without any additional supervision or labels. The valuable
contextual information can be used as a form of 'free' supervision for further representation
learning through performing top- down and layer-wise attention distillation within the net-
work itself. By implementing this SAD can be included in any feed- forward convolutional
neural networks (CNN) and does not increase the inference time. This methodology,
however, does not account to help with the identification of lanes in the scenario of severe
occlusion, ambiguous lanes, and poor lighting conditions.

Mamidala, Rama Sai, et al. [5] proposed a convolutional neural network (CNN) model
based on SegNet encoder-decoder architecture. The encoder block renders low-resolution
feature maps of the input and the decoder block provides pixel-wise classification from the
feature maps. The novelty of this approach lies in the integration of existing segnet

14
architecture with google APIs. The observed results show that the proposed method is
robust under challenging occlusion conditions due to pre-processing involved. However,
Google maps is quite inaccurate in countries like Africa and India, which would make the
model average at best if incorporated with its API.

Liu, Bin, Hongzhe Liu, and Jiazheng Yuan. [6] proposed to combine the Mask-RCNN
network to instantiate the lane line. There are three main advantages: Mask-RCNN network
can solve lane line detection in complex situations in road scenes. The authors apply the
multi-task learning Mask-RCNN network to single-task lane detection to solve the problem
of poor adaptability of traditional lane detection methods for complex road conditions. The
experimental results show that deep learning can achieve good results in the case of lane
detection in complex road conditions, and the detection can achieve 97.9% accuracy.

The proposed work by Jinn-Liang Liu. et al [7] compares the performances of CNN and
DSUNet integrated with a PP algorithm to form simulation models of DSUNet-PP and
CNN-PP, that can be used to assess CNN’s performance qualitatively, quantitatively, and
dynamically in a host agent car driving along with other agents all in a real-time
autonomous manner. The results show that DSUNet is efficient and effective for lane
detection and path prediction in autonomous driving. The model is able to identify between
real and fake objects as well as vehicles. While both the simulation models worked very
well for lane centering maneuver, it requires further integration with control algorithms for
multi-tasking.

Rafiee, F. et al [8] systematically reviewed PET / CT scores. They used a number of


websites to receive the resources required. The criterion of inclusion in the study were
patients who have undergone at least a single one of these imaging tests. It was found that
three quarters of the test takers were suffering from cancer, which included 18 different
types of the fatal disease. A further 11.5% seemed to be suffering from metastatic
disease.The leading findings in lungs were ground glass opacities, followed by
consolidation and stiffness. Early detections of of acute respiratory syndrome Sars-Cov-2
pneumonia is therefore not only important for the purposes of management of the people
suffering with them but also to make sure proper safety measures after exposure to the etire
department are undertaken.

The paper by Shuai, D. et al. [9] discusses a method for improving the accuracy of lane
recognition in images and also to increase the lane detection algorithm's accuracy and

15
robustness in challenging situations with shifting shadows and lighting. First, haar-like
features are extracted from the lane image and combined with environmental information.
This improves the recognition accuracy. Then, an improved boosting algorithm is used to
avoid overfitting and improve the algorithm's anti-noise performance.The results of
numerous tests confirm that our system can accurately identify straight or curved lanes in
various challenging conditions.

Li, Junfeng, et al. [10] present a deep neural network-based multi-task learning approach to
lane detection in order to improve the application level of driverless technology, improve
assisted driving technology and reduce traffic accidents. The researchers used a lane line
database to preprocess images, and used ZFNet to replace basic networks in VPGNet to
improve detection efficiency. The multi-task learning network (ZF-VPGNet) used
multi-label classification, grid box regression and object mask as task modules. They also
compressed the network (CZF-VPGNet) for use in embedded systems without greatly
affecting accuracy. The algorithm is able to handle fuzzy lane lines and missing lane line
marks, achieving high accuracy and robustness.

Joy, Salna, et al. [11] describes a method for lane detection using multiple frames from a
continuous driving scenario. The method uses a hybrid architecture that combines a
convolutional neural network (CNN) and a recurrent neural network (RNN). The authors
plan to conduct extensive experiments on two large datasets to train the model for optimal
robustness. Through rigorous experimentation and testing, our model has achieved a
significant accuracy of 96% and a precision of 0.964.

Wang, Wei, Hui Lin, and Junshu Wang. [12] describes a method for improving lane
detection in cars with autonomous driving functions using cloud computing and edge
computing. The traditional lane detection method is improved by using a dual model based
on instance segmentation and a popular convolutional neural network (CNN). Edge-cloud
computing is used to improve data processing efficiency, and a variable matrix is generated
in the lane fitting process to achieve effective detection in the scenario of slope change,
which improves the real-time performance of lane detection.

Jiao Ya-Jie. et al. [13] proposes an algorithm that is a deep convolutional neural network
(DCNN) based method for multi-lane detection. This method addresses the problem of poor
robustness in traditional lane detection techniques. The algorithm uses a fully convolutional
network (FCN) to extract lane boundary features from images. The lane marking equation is

16
established by combining a linear model with a curved model, the Hough transform is used
to determine the fit interval, and the least square method is used to fit lane marking.

Zou, Qin, et al. [14] proposed a method for lane detection that uses multiple frames from a
continuous driving scenario and combines a convolutional neural network (CNN) and a
recurrent neural network (RNN) for feature extraction and prediction. This approach aims to
improve the robustness of lane detection in handling difficult situations such as heavy
shadow, severe mark degradation, and serious vehicle occlusion. The method is evaluated
on two large-scale datasets and shown to outperform the competing methods in lane
detection.

Narayan, Aparajit, et al. [15] present a method for autonomous driving on ill-defined roads
using convolutional neural networks (CNNs) to predict the position and width of roads from
camera input images. The networks are trained with a dataset of annotated road images
using supervised learning. Two different network architectures are tested using image
sequences not used in training, and are compared to a different image processing method
that relies on differences in color distribution.

1.2. Gaps identified in the Survey

By reviewing the papers above, we can see that deep learning is a good approach for
performing detection of lanes in real life scenarios. It provides very high values of accuracy
and recall, which are two very important metrics in this classification problem. This
literature review has helped us set a benchmark for the project and can be used to compare
results in the future.

Paper no. Merits Demerits

[1] High efficacy and efficiency rate Only works for straight lanes

[2] New concepts of infra network inverse Lack of big enough dataset can lead to
mapping and anchor based lane generalisation error
representation

17
[3] Backpropogate to autocorrect algorithm Accuracy rates were not as high as
expected

[4] Supervised Machine Learning Doesn't identify lanes with severe


Algorithm occlusion, ambiguous lanes, and poor
lighting conditions

[5] Enhanced using blurry images to Google maps is quite inaccurate in


identify unexplored edge cases countries like Africa and India

[6] Mask-RCNN's regional suggestion Usage of conventional image


network can be used to roughly locate processing methods
the lane line

[7] The modelis able to identify between While both the simulation models
real and fake objects as well as vehicles. worked very well for lane centering
maneuver, it requires further
integration with control algorithms for
multi-tasking

[8] Experimented on two public datasets to The paper would not account for
verify results roads intersection and 2 way traffic

[9] The devised algorithm is fast and can There are many cases of false
work well in real time. positives while using haar cascade
classifier.

[10] The algorithm is able to handle fuzzy Requires large datasets and is hard to
lane lines and missing lane line marks, train to identify different types of
achieving high accuracy and robustness. lanes

[11] Extensive comparison has been Requires a lot of computational power


conducted between various models and extensive datasets.

[12] Better lane recognition efficiency than May result in latency and can be an
other models. issue in real-time applications, such as
autonomous vehicles.

18
[13] The FCN network can directly classify The algorithm may not perform well
lane images and achieve end-to-end under challenging scenarios such as
detection of multi-lane images. poor lighting, bad weather, or highly
curved roads.

[14] The proposed method has been shown The use of multiple frames and a deep
to outperform other lane detection neural network architecture may
methods on large-scale datasets. require more computational resources
and may not be suitable for real-time
applications.

[15] The use of convolutional neural The method relies on the availability
networks (CNNs) allows for the of a dataset of annotated road images,
prediction of the position and width of which may not always be easily
roads from camera input images, obtainable.
making it a useful tool for autonomous
driving.

Table 1: Merits and demerits of papers reviewed

19
2. Overview of Proposed System

2.1. Introduction and Related Concepts


Lane detection is a critical task in autonomous driving and advanced driver assistance
systems (ADAS) to ensure safe and reliable navigation. The provided code implements a
lane detection algorithm using computer vision techniques. This algorithm aims to identify
and track lane boundaries from a video stream or set of images.

Lane detection involves several related concepts and steps to achieve accurate results. Here
is an overview of the key concepts and steps involved in the lane detection algorithm
implemented in the code:

1. Camera Calibration:
- Camera calibration is performed to correct distortions caused by the camera lens.
- In the code, checkerboard images are used for camera calibration.
- The OpenCV library is utilized to find camera matrix and distortion coefficients.

2. Perspective Transformation:
- Perspective transformation is applied to obtain a bird's-eye view of the road.
- It transforms the image from the camera's perspective to a top-down view.
- The code defines source and destination points for the transformation.

3. Edge Detection:
- Edge detection is performed to identify potential lane markings.
- The code uses the Canny edge detection algorithm from the OpenCV library.

4. Region of Interest (ROI) Selection:


- A specific region of interest is defined in the image to focus on the road area.
- The code crops the image to the desired ROI, excluding unnecessary areas.

5. Lane Marking Segmentation:


- Lane markings are segmented from the image to isolate the lanes.
- The code applies color selection techniques, such as thresholding and clustering, to
extract lane pixels.
20
6. Sliding Window Technique:
- The sliding window technique is employed to localize lane pixels.
- It divides the image into vertical windows and identifies lane pixels within each window.
- The code implements the sliding window technique to detect and track lane pixels.

7. Polynomial Fitting:
- Polynomial fitting is used to estimate the lane curves.
- The code fits a second-degree polynomial to the detected lane pixels.

8. Lane Projection:
- The detected lane boundaries are projected back onto the original image.
- The code applies inverse perspective transformation to overlay the lane boundaries on
the original image.

9. Visualization:
- The code provides visual outputs, including intermediate results and final lane detection.
- It uses the OpenCV library to display images, draw lines, and add text annotations.

10. Video Processing:


- The lane detection algorithm is applied to a video stream.
- The code uses the OpenCV library to read video frames, process them, and write the
output video.

The combined code leverages various modules and libraries, such as NumPy, OpenCV, glob,
Matplotlib, scikit-image, and SciPy, to implement these steps and achieve accurate lane
detection.

By combining these techniques, the implemented lane detection algorithm can effectively
detect and track lane boundaries, providing essential information for autonomous vehicles or
driver assistance systems to maintain their position on the road and navigate safely.

21
2.2. Framework for the Proposed System
​The proposed system for lane detection is implemented using the following framework:

1. Input Acquisition:
- The system acquires input from a video stream or a set of images.
- It utilizes a camera or reads pre-recorded video files.

2. Camera Calibration:
- The system performs camera calibration to correct distortions caused by the camera lens.
- Calibration images are used to calculate camera matrix and distortion coefficients.
- The OpenCV library is used for camera calibration.

3. Perspective Transformation:
- The system applies perspective transformation to obtain a bird's-eye view of the road.
- Source and destination points are defined to perform the transformation.
- The OpenCV library is used to warp the image based on the perspective transformation
matrix.

4. Preprocessing:
- The system applies preprocessing techniques to enhance lane detection.
- Preprocessing may include resizing, color space conversion, and noise reduction.

5. Edge Detection:
- The system performs edge detection to identify potential lane markings.
- The Canny edge detection algorithm from the OpenCV library is utilized.
- The resulting edges provide initial information about lane boundaries.

6. Region of Interest (ROI) Selection:


- The system selects a specific region of interest in the image to focus on the road area.
- The ROI excludes unnecessary areas, such as the sky or car hood.
- The ROI is defined based on the perspective-transformed image.

7. Lane Marking Segmentation:


- The system applies segmentation techniques to isolate lane markings from the image.

22
- Color selection methods, such as thresholding or clustering, are used to extract lane
pixels.
- Segmentation helps separate the lane markings from the background.

8. Lane Detection:
- The system utilizes lane detection algorithms to identify and track lane boundaries.
- Techniques such as sliding window search, polynomial fitting, or deep learning-based
methods can be employed.
- The lane detection algorithm analyzes the segmented lane pixels to estimate lane curves.

9. Lane Projection:
- The system projects the detected lane boundaries back onto the original image.
- Inverse perspective transformation is applied to overlay the lane boundaries on the
original image.
- The OpenCV library is used to perform the inverse perspective transformation.

10. Visualization and Output:


- The system provides visual outputs to demonstrate the lane detection results.
- Intermediate images, such as the segmented lane markings or polynomial fit, may be
displayed.
- The final output includes the original image with the detected lane boundaries
overlayed.
- The OpenCV library is utilized for image visualization and annotation.

11. Video Processing:


- The system extends the lane detection algorithm to process video streams.
- It reads video frames, applies lane detection techniques to each frame, and produces a
processed video.
- The OpenCV library is used to read video frames, process them, and write the output
video.

The proposed system is implemented using various modules and libraries, such as NumPy,
OpenCV, and other image processing and computer vision libraries. The framework
provides a structured approach to lane detection, starting from input acquisition and camera

23
calibration, followed by perspective transformation, preprocessing, edge detection, lane
detection, and visualization. It allows for accurate lane detection and tracking in real-time
video streams or images, contributing to safer and more efficient autonomous driving
systems.

Img 2.1. Framework of Proposed System

24
3. Proposed System Analysis and Design

3.1. Introduction

● Firstly, we will extract the image frames from videos obtained and process the images
for use in making labels.
● Next, we will calibrate for the camera's distortion by taking pictures of chessboard
images with the same camera the video was obtained with, and un-distort the image
● With the image now undistorted, we will find good source points and determine good
destination points to perspective transform the image
● When we have these points, we can get a transformation matrix and warp the image to
a bird's eye-like view of the road.
● In order to enhance the model's robustness, we will draw red lines over the lane lines
and use a binary thresholding on areas of the image with high red values.
● After this, histograms will be computed using where the highest amount of pixels fall
vertically that split out from the middle of the image.
● Sliding windows that search for more binary activation going up the image will then be
used to attempt to follow the line.
● Based off the detected pixels from the sliding windows, polynomial functions will
return the three coefficients each for 2 lanes, and we will append the total of six
coefficients to a list to use as labels for training
● Using the labels obtained from the above steps, we will feed an image through the
original undistortion and perspective transformation, create an image "blank" with
"numpy.zeros_like", make lane points from the polynomial coefficients by calculating
the full polynomial fit equation from above for each line, and then use OpenCV Library
module with those to create a lane drawing.
● We will use the warped perspective with the inverse of my perspective transformation
matrix calculated before to revert this lane drawing back to the space of the original
image.
● Post this, we will merge the lane drawing with the original image. This way, we will
make sure that our labels being fed to the model are undergoing a robust check.
● Lastly, our project will use Keras with TensorFlow backend in order to create a CNN.
We will first try a model architecture similar to the one below

3.2. Requirement Analysis


25
3.2.0. Functional Requirements
3.2.0.0. Product Perspective
From a product perspective, lane detection using OpenCV has numerous applications in the
automotive industry. Autonomous vehicles are becoming more prevalent, and accurate and
reliable lane detection systems are critical for their safe and effective operation. Lane
detection using OpenCV can be used in the following products:
1. Autonomous vehicles - Lane detection using OpenCV can be integrated into
autonomous vehicles as a critical component of the navigation system. The lane
detection system provides the vehicle with information on the direction and curvature of
the road, allowing the vehicle to stay within its lane and avoid collisions.

2. Advanced Driver Assistance Systems (ADAS) - Lane detection using OpenCV can be
used in ADAS products to provide drivers with real-time information on the location of
their vehicle within the lane. This information can be used to alert drivers when they are
drifting out of their lane and to provide steering assistance to keep the vehicle within the
lane.

3. Fleet management systems - Lane detection using OpenCV can be used in fleet
management systems to monitor the behavior of drivers and provide feedback on their
driving performance. The system can detect when a driver is drifting out of their lane,
and provide feedback to the driver and their employer on their driving behavior.

4. Traffic monitoring systems - Lane detection using OpenCV can be used in traffic
monitoring systems to collect data on the traffic patterns and flow of vehicles on the
road. This information can be used to optimize traffic flow, reduce congestion, and
improve road safety.

26
3.2.0.1. Product Features
The following are some of the key features of lane detection using OpenCV:
1. Real-time processing: Lane detection using OpenCV can be performed in real-time,
allowing for accurate and reliable detection of lane markings as the vehicle is moving.
2. Adaptive lane detection: OpenCV allows for adaptive lane detection, which means the
system can adjust to different road and lighting conditions in real-time. This helps to
ensure accurate and reliable lane detection even in challenging conditions.
3. Perspective transform warping: OpenCV provides tools for performing perspective
transform warping, which allows for a bird's eye view of the road to be obtained. This
helps to reduce the effect of perspective distortion and improve the accuracy of lane
detection.
4. Sliding window technique: OpenCV provides tools for performing the sliding window
technique, which allows for accurate and reliable identification of the lane markings
within the transformed image.
5. Polynomial fitting: OpenCV provides tools for fitting a 2nd degree polynomial to the
lane markings, which allows for accurate prediction of the direction and curvature of the
road ahead.
6. Radius of curvature estimation: OpenCV provides tools for estimating the radius of
curvature of the road based on the coefficients of the fitted polynomial curve. This
information can be used to predict the steering direction of the vehicle and improve the
accuracy of lane keeping.
7. User-friendly interface: OpenCV provides a user-friendly interface for developing and
testing lane detection algorithms, making it easy for developers to integrate lane
detection into their products.

27
3.2.0.2. User Characteristics

The user characteristics of lane detection using OpenCV can vary depending on the
specific product or application. However, in general, the following are some of the
typical user characteristics for lane detection using OpenCV:
1. Automotive engineers : Lane detection using OpenCV is typically developed and
implemented by automotive engineers with expertise in image processing and computer
vision. These engineers are responsible for designing, developing, and testing the lane
detection system to ensure it meets the required performance specifications.
2. Autonomous vehicle manufacturers : Lane detection using OpenCV is a critical
component of autonomous vehicle systems, and is typically used by autonomous vehicle
manufacturers. These manufacturers are responsible for integrating the lane detection
system into the vehicle's overall navigation system, and for ensuring the system is
reliable and accurate in a variety of driving conditions.
3. ADAS manufacturers : Lane detection using OpenCV is also used in advanced driver
assistance systems (ADAS), which are typically manufactured by automotive electronics
companies. These companies are responsible for integrating the lane detection system
into the ADAS product and ensuring it meets the required safety and performance
specifications.
4. Fleet management companies : Fleet management companies may use lane detection
using OpenCV to monitor the driving behavior of their drivers and to provide feedback
on their performance. These companies may have a user interface that allows them to
monitor their drivers' behavior and make recommendations for improvements.
5. Traffic management agencies : Traffic management agencies may use lane detection
using OpenCV to monitor traffic patterns and flow on the road. These agencies may
have a user interface that allows them to visualize the data collected by the lane
detection system and make decisions to optimize traffic flow.

28
3.2.0.3. Assumption & Dependencies
The following are some of the typical assumptions and dependencies for lane detection
using OpenCV:

Assumptions:
1. The lane markings are visible and well-defined in the camera images, with sufficient
contrast and brightness to be detected by the image processing algorithms.
2. The camera is properly calibrated and aligned with the vehicle's position and orientation,
to ensure accurate detection and tracking of the lane markings.
3. The road surface is smooth and free of major obstacles or deformations that could
interfere with the detection and tracking of the lane markings.
4. The vehicle is moving within the speed range that the lane detection algorithm is
designed to operate in, to ensure that the detection and tracking of the lane markings are
accurate and timely.
5. The lighting conditions are within the range that the lane detection algorithm is designed
to operate in, to ensure that the lane markings are accurately detected and tracked under
different lighting conditions.

Dependencies:
1. OpenCV libraries: Lane detection using OpenCV depends on the availability and
proper functioning of the OpenCV libraries, which provide the image processing and
computer vision functions required for lane detection.
2. Camera hardware: Lane detection using OpenCV depends on the availability and
proper functioning of the camera hardware, which provides the images that are
processed by the lane detection algorithm.
3. Processor hardware: Lane detection using OpenCV depends on the availability and
proper functioning of the processor hardware, which executes the image processing and
computer vision algorithms required for lane detection.
4. Operating system: Lane detection using OpenCV depends on the availability and
proper functioning of the operating system that supports the OpenCV libraries and the
application code that implements the lane detection algorithm.
5. Integration with other systems: Lane detection using OpenCV may depend on the
integration with other systems in the vehicle, such as the steering and braking systems,

29
to enable automated driving. The proper functioning of these systems is critical to the
safety and reliability of the lane detection system.

3.2.0.4. Domain Requirements


The domain requirements of lane detection using OpenCV are as follows:
1. Computer Vision: Lane detection using OpenCV requires expertise in computer vision,
including knowledge of image processing techniques, feature extraction, pattern
recognition, and machine learning algorithms.
2. Signal Processing: Lane detection using OpenCV requires expertise in signal
processing, including knowledge of filtering techniques, signal analysis, and frequency
domain analysis.
3. Mathematics: Lane detection using OpenCV requires expertise in mathematics,
including knowledge of linear algebra, calculus, and probability theory.
4. Automotive Engineering: Lane detection using OpenCV requires expertise in
automotive engineering, including knowledge of vehicle dynamics, control systems, and
safety regulations.
5. Embedded Systems: Lane detection using OpenCV requires expertise in embedded
systems, including knowledge of microcontroller programming, hardware design, and
real-time operating systems.
6. Software Development: Lane detection using OpenCV requires expertise in software
development, including knowledge of programming languages, software design patterns,
and software testing.
7. User Experience: Lane detection using OpenCV requires expertise in user experience
design, including knowledge of human-computer interaction, usability testing, and user
interface design.

30
3.2.0.5. User Requirements

In general, the following are some of the typical user requirements for lane detection using
OpenCV:
1. Accuracy: Lane detection using OpenCV must be accurate in detecting and tracking the
lane markings, even in challenging conditions such as poor lighting or bad weather.
2. Real-time processing: Lane detection using OpenCV must be able to process images in
real-time to ensure that the lane markings are accurately detected and tracked as the
vehicle is moving.
3. Adaptability: Lane detection using OpenCV must be able to adapt to different road and
lighting conditions in real-time, to ensure accurate and reliable detection of the lane
markings.
4. Robustness: Lane detection using OpenCV must be robust, with the ability to
accurately detect the lane markings even in situations where there is partial occlusion,
shadows or other obstructions.
5. Integration with other systems: Lane detection using OpenCV must be able to
integrate with other systems in the vehicle, such as the steering and braking systems, to
enable automated driving.
6. User interface: Lane detection using OpenCV may require a user interface for
calibration, monitoring, and visualizing the system's performance.
7. Reliability and safety: Lane detection using OpenCV must be reliable and safe, with
fail-safe mechanisms in place to ensure that the vehicle can safely and smoothly
transition from automated driving to manual driving, if necessary.
8. Low computational requirements: Lane detection using OpenCV must have low
computational requirements, to ensure that it can be run on embedded systems with
limited processing power.

31
3.2.1. Non Functional Requirements

3.2.1.0. Reliability
Reliability is a critical aspect of a lane detection system as it directly impacts the system's
usability and safety. Here are some considerations for ensuring the reliability of the
proposed project:

1. Robustness to Variations: The system should be designed to handle variations in


real-world conditions such as lighting conditions, weather conditions, and road surface
variations. It should be able to provide accurate lane detection and tracking even in
challenging scenarios, such as low light, rain, or poor road markings.

2. Robust Algorithm Design: The algorithms employed in the system should be robust
and resistant to noise, outliers, and unexpected situations. They should be able to handle
different types of lane markings, lane configurations, and traffic scenarios.

3. Error Handling and Recovery: The system should be equipped with error handling
mechanisms to handle failures or unexpected situations. For example, if the lane
detection algorithm fails to detect lanes in a frame, the system should have fallback
mechanisms or alternative strategies to ensure continuous operation. It should also be
capable of recovering from temporary disruptions, such as occlusions or sudden changes
in lighting conditions.

4. Continuous Monitoring and Maintenance: Regular monitoring and maintenance of


the system are crucial to ensure its ongoing reliability. This includes monitoring system
performance, checking for sensor calibration issues, and conducting periodic software
updates to address any potential vulnerabilities or bugs.

By considering these aspects and implementing appropriate measures, the proposed lane
detection system can achieve a high level of reliability, ensuring accurate and consistent
performance in various real-world scenarios.

32
3.2.1.1. Portability
Portability is an important consideration for a lane detection system, especially when it is
intended to be used in different platforms or integrated into various applications. Here are
some factors to consider for ensuring portability in the proposed project:

1. Platform Independence: The system should be designed to be platform-independent,


allowing it to run on different hardware architectures and operating systems. It should be
developed using programming languages and frameworks that are widely supported and
compatible with multiple platforms.

2. Hardware Compatibility: Consider the hardware requirements and ensure that the
system can be easily deployed on a range of hardware configurations.

3. Software Modularity: The system should be structured in a modular manner, with


well-defined interfaces and components. This allows for easy integration and porting of
individual modules into different software environments or frameworks.

By considering these factors and incorporating portability as a design principle, the


proposed lane detection system can be made more adaptable and portable, enabling its
integration into various platforms and applications with ease.

33
3.2.1.2. Usability
Usability is a crucial aspect of a lane detection system as it determines how easily and
effectively users can interact with and utilize the system.

1. Simplified Setup and Configuration: By making the setup and configuration process
as simple as possible. Provide clear instructions and guidance to users, ensuring that
they can easily install and configure the system without technical expertise.

2. Real-Time Feedback and Visualization: Provide real-time feedback to users,


displaying the detected lane boundaries or relevant information in a visually intuitive
manner. This allows users to have a clear understanding of the system's performance and
aids in decision-making. Use appropriate visualization techniques, such as overlaying
lane markings on the video stream or providing graphical representations of detected
lanes.

3. Documentation and Tutorials: Create comprehensive documentation and tutorials that


explain the system's functionality, usage, and troubleshooting steps. Include step-by-step
instructions, examples, and frequently asked questions to assist users in understanding
and effectively utilizing the system.

4. Training and Support: Offer training resources and support channels to assist users in
mastering the system. This can include video tutorials, online forums, user guides, and
responsive customer support. Encourage user feedback and incorporate user suggestions
to continuously improve the system's usability.

By prioritizing usability considerations and incorporating user feedback throughout the


development process, the proposed lane detection system can be designed to be
user-friendly, intuitive, and accessible to a wide range of users.

34
3.2.2. Organizational Requirements

3.2.2.0. Implementation Requirements (in terms of deployment)


When deploying a lane detection system, there are several implementation requirements to
consider.
1. Software Frameworks and Libraries: Select the appropriate software frameworks and
libraries for implementing the lane detection system. This may include computer vision
libraries (e.g., OpenCV), deep learning frameworks (e.g., TensorFlow or PyTorch), and
other relevant tools and libraries.
2. Integration with Existing Systems: Consider how the lane detection system will
integrate with other components or systems. For example, if the system is part of an
autonomous driving platform, it needs to communicate and interact with other modules
such as perception, planning, and control. Define the necessary interfaces and protocols
for seamless integration and interoperability.
3. Real-Time Performance: Assess the real-time performance requirements of the system.
Lane detection often needs to operate in real-time, especially in applications like
autonomous driving or driver assistance systems. Optimize the algorithms, software
implementation, and hardware platform to meet the desired frame rate and latency
requirements.
4. Deployment Scalability: Evaluate the deployment scalability needs of the lane
detection system. Determine if the system needs to be deployed on a single device or
scaled across multiple devices in a distributed manner. Plan for the necessary
infrastructure, network configurations, and communication protocols to support the
desired scalability.
5. Testing and Validation: Conduct thorough testing and validation of the lane detection
system before deployment. This includes testing the system in various real-world
scenarios and validating its performance against ground truth data. Perform systematic
testing to ensure the system's accuracy, robustness, and reliability in different conditions.

By addressing these implementation requirements, we can ensure a successful deployment


of the lane detection system, taking into account the specific needs of the deployment
environment, platform, and integration requirements.

35
3.2.2.1. Engineering Standard Requirements
When developing a lane detection system, it's important to adhere to engineering standards
to ensure safety, quality, and interoperability. While specific standards may vary depending
on the industry and application, here are some general engineering standard requirements to
consider:

1. ISO 26262 (Automotive): Since the lane detection system is intended for use in
automotive applications, compliance with ISO 26262, the international standard for
functional safety in the automotive industry, is crucial. This standard outlines the
processes, methodologies, and requirements for ensuring the safety of electronic
systems, including lane detection systems, in vehicles.

2. Software Development Standards: We did adhere to established software development


standards such as the ISO/IEC 12207 standard for software life cycle processes or the
ISO/IEC 25010 standard for software quality characteristics and evaluation. These
standards provide guidance on software development processes, documentation, testing,
and quality assurance.

3. System Safety Standards: We considered system safety standards such as IEC 61508
(for functional safety of electrical, electronic, and programmable electronic
safety-related systems) or ISO 13849 (for safety-related parts of control systems). These
standards provide guidelines for ensuring the safety and reliability of systems, including
the implementation of safety measures and risk assessment methodologies.

36
3.2.3. Operational Requirements
Operational requirements play a significant role in shaping the implementation and impact
of a lane detection system. Here's an explanation of how these requirements are applicable
to the project in relation to the following aspects:

● Economic: The economic aspect considers the financial feasibility and


cost-effectiveness of the lane detection system. This includes factors such as the cost of
development, deployment, and maintenance of the system. The project should aim to
develop a solution that provides value for the intended users and stakeholders while
considering the economic constraints and ensuring a reasonable return on investment.

● Environmental: The environmental aspect pertains to the system's impact on the


environment. In the case of a lane detection system, it is important to minimize any
negative environmental effects resulting from the system's operation. This can be
achieved by optimizing the power consumption of hardware components, using
energy-efficient algorithms, and considering eco-friendly materials in the system's
design and manufacturing processes.

● Social: The social aspect considers the broader societal implications of the lane
detection system. It is important to develop a system that enhances road safety, reduces
accidents, and improves overall transportation efficiency. Consideration should be given
to the impact on different user groups, such as pedestrians, cyclists, and drivers, to
ensure equitable benefits and improved mobility for all.

● Ethical: The ethical aspect involves considering the moral implications and responsible
use of the lane detection system. This includes ensuring privacy protection, avoiding
biased or discriminatory outcomes, and addressing potential ethical dilemmas such as
the balance between driver autonomy and system intervention. Ethical considerations
should guide the design and implementation of the system to ensure fairness,
transparency, and responsible decision-making.
● Health and Safety: The health and safety aspect focuses on mitigating risks and
ensuring the well-being of individuals interacting with the lane detection system. This
includes ensuring that the system does not introduce hazards or cause harm to users or
bystanders.

37
● Sustainability: The sustainability aspect involves considering the long-term
environmental, social, and economic impact of the lane detection system. The project
should aim to develop a system that is sustainable in terms of resource usage, energy
efficiency, and waste reduction. This can be achieved through optimization techniques,
use of recyclable materials, and consideration of the system's life cycle impacts.

By considering and addressing these operational requirements, the lane detection system can
be developed and deployed in a manner that aligns with economic considerations,
minimizes environmental impact, benefits society, complies with regulations, upholds
ethical standards, ensures health and safety, promotes sustainability, maintains legality, and
allows for inspectability to ensure system performance and compliance.

38
3.2.4. System Requirements
3.2.4.0. Hardware Requirements (details about Application Specific Hardware)
● Macbook air running Mac OS ventura 13.0
● Apple M1 processor

3.2.4.1. Software Requirements(details about Application Specific Software)


Software Requirements:
The proposed system for lane detection has the following software requirements:

1. Python:
- The system is implemented using the Python programming language.
- Python provides a rich set of libraries for image processing and computer vision tasks.
- The system is compatible with Python 3.x versions.
2. OpenCV:
- OpenCV (Open Source Computer Vision Library) is a popular open-source library for
computer vision tasks.
- It provides a wide range of functions and algorithms for image processing, feature
detection, and machine learning.
- The system utilizes OpenCV for camera calibration, perspective transformation, edge
detection, image warping, and visualization.
3. NumPy:
- NumPy is a fundamental library for numerical computations in Python.
- The system utilizes NumPy for array manipulation, mathematical calculations, and data
storage.
4. Matplotlib:
- Matplotlib is a plotting library for Python.
- The system utilizes Matplotlib for displaying and annotating images with detected lane
boundaries.
5. SciPy:
- SciPy is a library for scientific and technical computing in Python.
- The system may use SciPy for additional image processing tasks, such as curve fitting or
optimization.

39
Modules Involved:

● numpy (`import numpy as np`): Used for mathematical operations and array
manipulation.

● cv2 (OpenCV library): Used for various computer vision tasks, such as image
processing, drawing, perspective transformation, video capture, and video writing.

○ cv2.imread(): Used to read images from files.

○ cv2.copyMakeBorder(): Used to add borders to images.

○ cv2.putText(): Used to draw text on images.

○ cv2.imshow(): Used to display images in windows.

○ cv2.waitKey(): Used for keyboard event handling.

○ cv2.VideoWriter(): Used to write video frames to a video file.

○ cv2.VideoWriter_fourcc(): Used to specify the video codec.

○ cv2.VideoCapture(): Used to capture frames from a video file.

● glob (`import glob`): Used to retrieve file paths that match a specified pattern. In
this code, it is used to retrieve calibration image paths.

● matplotlib (`import matplotlib.pyplot as plt`): Used for data visualization. In


this code, it is used to plot the histogram of pixel intensities.

● sklearn (`from sklearn.cluster import KMeans`): Used for clustering


algorithms. In this code, it is used to perform K-means clustering for color
selection.

● skimage (`from skimage.filters import threshold_otsu`): Used for image


processing tasks. In this code, it is used to apply Otsu's thresholding to segment
lane lines.

● scipy (`from scipy.signal import find_peaks`): Used for scientific computing and
signal processing. In this code, it is used to find peaks in the histogram of pixel
intensities.
40
4. Project Demonstration

41
Fig 4.1 First Block of Code

The provided code performs histogram equalization and adaptive histogram equalization on
a sequence of images. It also applies Contrast Limited Adaptive Histogram Equalization
(CLAHE) using OpenCV's built-in function. The code includes several functions and a class
for demonstrating the results.

1. The code imports necessary libraries like numpy, cv2, matplotlib.pyplot.


2. The ‘cumulative_image_hist’ function computes the cumulative histogram of an input
image.
3. The ‘show_histogram’ function displays the input image and its histogram using
‘matplotlib.
4. The ‘show_equalised_histogram’ function displays the equalized image and its
histogram using ‘matplotlib.
5. The ‘show_equalised_image_with_histogram’ function displays the equalized image
and its histogram using ‘matplotlib.
6. The ‘create_equalised_image’ function performs histogram equalization on the input

42
image by converting it into HSV color space, equalizing the value channel, and then
converting it back to BGR color space.

7. The ‘show_equalised_image’ function displays the equalized image using ‘matplotlib.


8. The ‘histogram_equalization’ function performs histogram equalization on the input
image by computing the cumulative distribution function of pixel intensities and
applying it to images.
9. The ‘split_hsv_merge_adaptive’ function splits the input image into HSV channels,
applies adaptive histogram equalization to the value channel and then merges the
channels back to obtain the equalized image.
10. The ‘adaptive_histogram_equalization’ divides image into blocks and applies
histogram equalization to each block individually.
11. The ‘CLAHE’ function applies Contrast Limited Adaptive Histogram Equalization to
the input image using OpenCVs ‘createCLAHE’ function.
12. The histogram_equalization_demo class encapsulates the demonstration of various
histogram equalization techniques.
13. In the main part of the code, a video file is created to save the output frames. A loop
iterates over a sequence of input images, and for each image, the
histogram_equalization_demo class is instantiated to demonstrate the equalization
techniques. The output images are saved and displayed.
14. At the end, the created video file is released, and a message is printed to indicate the
location of the saved video.

43
Fig 4.2 Second Block of Code

The provided code defines a function `get_undistorted_image_params` that calculates the


distortion coefficients and the K matrix for undistorting images using camera calibration.
Here's a breakdown of the code:

1. The code imports necessary libraries, including `numpy`, `cv2` (OpenCV),


`matplotlib.pyplot`, and `glob`.

2. The function `get_undistorted_image_params` takes two parameters: `filename` and


`image`. `filename` represents the pattern of file names for the calibration images
(chessboard images), and `image` is the image to be processed.

3. The function first creates an array `obj_coords` to hold the object points (3D coordinates
of the chessboard corners). These coordinates are set to the x-y coordinates of the corners on
a grid with size 9x6.

4. It initializes empty lists `obj_pts` and `img_pts` to store the object points and the image
points, respectively.
44
5. The function uses `glob.glob` to obtain a list of image files that match the provided
`filename` pattern.

6. For each image file, the following steps are performed:


- Read the image using `cv2.imread`.
- Convert the image to grayscale using `cv2.cvtColor`.
- Use `cv2.findChessboardCorners` to detect the corners of the chessboard pattern in the
grayscale image.
- If the corners are found (`ret=True`), append the object points (`obj_coords`) and image
points (`corners`) to `obj_pts` and `img_pts`, respectively.

7. The size of the input image (`img_res`) is obtained.

8. The function calls `cv2.calibrateCamera` with the object points (`obj_pts`), image points
(`img_pts`), image resolution (`img_res`), and other parameters to calculate the camera
calibration parameters: rotation matrix (`r`), K matrix (`mtx`), distortion coefficients (`dist`),
and other optional outputs.

9. The function returns the distortion coefficients (`dist`) and the K matrix (`mtx`).

The code assumes that there are calibration images (chessboard images) available in the
specified `filename` pattern and uses them to calibrate the camera. The resulting distortion
coefficients and K matrix can then be used for undistorting images taken with the same
camera. Make sure to provide the correct `filename` pattern and pass the image to be
processed as an argument to the function.

45
4.3 Third Block of Code

The provided code includes two functions: `undistort` and `process_image`. Here's an
explanation of each function:

1. `undistort(img, K_matrix, dist)`: This function takes an image (`img`), the camera matrix
(`K_matrix`), and distortion coefficients (`dist`) as inputs. It uses the `cv2.undistort`
function to undistort the image based on the provided camera calibration parameters. The
undistorted image is then returned.
46
2. `process_image(image)`: This function processes an input image and returns the detected
edges using the Canny edge detection algorithm. Here are the steps performed in this
function:

a. The input image is cropped using slicing (`image_cropped`) to focus on the region
of interest (ROI).
b. The cropped image is undistorted using the `undistort` function, with the camera
matrix (`K_matrix`) and distortion coefficients (`dist`) as parameters. The
undistorted image is stored in `image_undist`.
c. The undistorted image is converted from BGR color space to HLS color space
(`image_hls`).
d. A yellow color mask is defined using lower and upper thresholds
(`yellow_mask_lower` and `yellow_mask_upper`) in the HLS color space. The
`cv2.inRange` function is then used to create a binary mask (`masked_yellow`) that
isolates the yellow color in the image. The yellow color is extracted by applying the
mask to the HLS image using `cv2.bitwise_and` and stored in `yellow_image`.
e. Similarly, a white color mask is defined using lower and upper thresholds
(`white_mask_lower` and `white_mask_upper`) in the HLS color space. The
`cv2.inRange` function is used to create a binary mask (`masked_white`) that
isolates the white color in the image. The white color is extracted by applying the
mask to the HLS image using `cv2.bitwise_and` and stored in `white_image`.
f. The yellow and white color images are combined using `cv2.bitwise_or` to obtain
an image (`detected_lanes`) with both yellow and white lines.
g. The `detected_lanes` image is converted from HLS to BGR color space
(`detected_lanes_bgr`).
h. The `detected_lanes_bgr` image is converted to grayscale (`detected_lanes_gray`).
i. A bilateral filter is applied to `detected_lanes_gray` to smooth out the image
(`blur`).
j. Canny edge detection is performed on the smoothed image (`blur`) using the
`cv2.Canny` function, and the resulting edges are stored in the `edges` variable.
k. Finally, the `edges` image is returned.
The code assumes that the provided `dist` and `K_matrix` are correct and match the camera
calibration parameters.

47
4.4 Fourth Block of Code

The provided code includes four additional functions: `show_histogram`, `predict_turn`,


`draw_lines`, and `draw_arrows`. Here's an explanation of each function:

1. `show_histogram(image)`: This function takes an image as input and calculates the


histogram by summing the pixel values along the vertical axis
(`np.sum(image[image.shape[0]//2:,:],axis=0)`). The histogram is then plotted using
`plt.plot` and displayed using `plt.show`.

2. `predict_turn(center_coords, left_coords, right_coords)`: This function predicts the turn


based on the center, left, and right coordinates of the lane. It calculates the position of the
lane center (`lane_center_pos`) as the average of the left and right coordinates. If the
difference between `lane_center_pos` and `center_coords` is less than 0, it returns 'Turning
Smooth left'. If the difference is less than 8, it returns 'Straight'. Otherwise, it returns
'Turning Smooth right'.

3. `draw_lines(image, left_lane_coords, right_lane_coords)`: This function takes an image,


left lane coordinates, and right lane coordinates as input. It draws the left and right lane lines
on the image using `cv2.polylines` with the specified color and line thickness. The modified
image is then returned.

48
4. `draw_arrows(image, lane_center_pos)`: This function takes an image and the position of
the lane center (`lane_center_pos`) as input. It draws an arrow on the image using
`cv2.arrowedLine`, starting from the coordinates
(`lane_center_pos[0][0],lane_center_pos[1][1]`) and ending at
(`lane_center_pos[1][0],lane_center_pos[0][0]+50`). The arrow is drawn in blue color and
with a thickness of 5. The modified image is then returned.

4.5 Fifth Block of Code

The `radius of curvature` function calculates the radius of curvature for the left and right
lane lines in meters. Here's a breakdown of how it works:

1. It first determines the height of the image.


2. It calculates the conversion factor `y_per_pixel` to convert from pixel space to meters. In
this case, it assumes a lane length of 32 meters and divides it by the image height.
3. It uses the left and right polynomial coefficients (`left_fit` and `right_fit`) to calculate the
radius of curvature for each lane line using the formula:
- `left_curverad = ((1 + (2 * left_fit[0] * y_per_pixel + left_fit[1])**2)**1.5) /
np.absolute(2 * left_fit[0])`
- `right_curverad = ((1 + (2 * right_fit[0] * y_per_pixel + right_fit[1])**2)**1.5) /
np.absolute(2 * right_fit[0])`
These formulas calculate the radius of curvature based on the second order polynomial
coefficients. The result is in meters.
4. The function returns the calculated left and right lane line curvatures.

49
The `sliding_window` function performs lane line detection using the sliding window
technique. Here's how it works:

1. It takes the input `image` and `sliding_image` (an image for visualization) along with
optional parameters: `nwindows`, `margin`, `minpix`, and `draw_windows`.
2. It calculates the histogram of the image by summing the pixel values along the vertical
axis.
3. It determines the midpoint of the histogram and the image center position.
4. It initializes the current x-position for the left and right lanes based on the maximum
histogram peaks.
5. It creates an output image `out_img` by stacking the input image three times to form a
color image.
6. It sets up variables for window height and arrays to store the indices of non-zero pixels
for the left and right lanes.
7. It loops through the windows from bottom to top:
- For each window, it defines the window boundaries in x and y for the left and right lanes.
- If `draw_windows` is set to True, it draws rectangles on the `sliding_image` to visualize
the windows.
- It identifies the non-zero pixels within the current window for the left and right lanes.
- It appends the indices of these non-zero pixels to the respective arrays.
- If the number of non-zero pixels is greater than `minpix`, it updates the current
x-position based on the mean of the non-zero pixel indices.
8. It concatenates the arrays of left and right lane indices.
9. It extracts the corresponding pixel positions for the left and right lanes.
10. It fits a second-order polynomial to each lane using `np.polyfit` to obtain the polynomial
coefficients `left_fit` and `right_fit`.
11. It visualizes the detected lane lines on `out_img` by coloring the non-zero pixels
corresponding to the left lane as yellow and the right lane as cyan.
12. It predicts the turn direction based on the lane center and the current x-positions of the
left and right lanes using the `predict_turn` function.
13. It returns the `left_fit`, `right_fit`, `out_img`, `turn_prediction`, `left`, `right`, and
`sliding_image` for further processing and visualization.

50
4.6 Sixth Block of Code

The updated `sliding_window` function performs lane line detection using the sliding
window technique. Here's a breakdown of the changes:

1. It calculates the histogram of the image by summing the pixel values along the vertical
axis.
2. The midpoint of the histogram is calculated to divide the image into left and right halves.
3. The image center position is determined.
4. The current x-positions for the left and right lanes are initialized using the maximum
histogram peaks.
5. The `out_img` is created by stacking the input image three times to form a color image.
6. The window height is calculated based on the number of windows and the image height.
7. The `nonzero` array is used to find the non-zero pixels in the image.
8. Arrays `left_lane_inds` and `right_lane_inds` are created to store the indices of the left
51
and right lane pixels, respectively.
9. The function iterates through each window:
- It identifies the window boundaries in x and y for the left and right lanes.
- If `draw_windows` is set to True, it draws rectangles on the `sliding_image` to visualize
the windows.
- It identifies the non-zero pixels within the current window for the left and right lanes.
- The indices of these non-zero pixels are appended to the respective lists.
- If there are more non-zero pixels than `minpix`, the current x-positions are updated by
taking the mean of the non-zero pixel indices.
10. The arrays of indices `left_lane_inds` and `right_lane_inds` are concatenated.
11. The left and right line pixel positions are extracted using the concatenated indices.
12. The left and right lines are set as arrays `[leftx, lefty]` and `[rightx, righty]`, respectively.
13. Second-order polynomials are fit to the left and right line pixel positions using
`np.polyfit`, resulting in `left_fit` and `right_fit`.
14. The detected lane lines are visualized on `out_img` by coloring the non-zero pixels
corresponding to the left lane as yellow and the right lane as cyan.
15. The predicted turn direction is obtained by calling the `predict_turn` function with the
image center position, left current x-position, and right current x-position.
16. The function returns `left_fit`, `right_fit`, `out_img`, `turn_prediction`, `left`, `right`, and
`sliding_image` for further processing and visualization.

52
4.7 Seventh Block of Code

The `project_back` function performs the back projection of the detected lane lines onto the
original image and adds additional information such as the radius of curvature and predicted
turn direction. Here's an explanation of the function:

1. It generates a set of y-values evenly spaced from 0 to `image.shape[0]-1`.


2. Using the fitted polynomial coefficients (`left_fit` and `right_fit`), it calculates the
corresponding x-values for the left and right lanes at each y-value.
3. A blank image is created to draw the lane lines on.
4. The radius of curvature of the lane lines is computed using the `radiusofcurvature`
function.
5. A color channel is added to the blank image to form `warp_color_channel`.
6. The x and y points for the left and right lanes are reshaped into the appropriate format for
`cv2.fillPoly`.
7. The lane center position is calculated as the midpoint between the left and right lanes.
8. The lane area is filled with green color on the `warp_color_channel` using `cv2.fillPoly`.
9. The lane lines are drawn on the `warp_color_channel` using `draw_lines` function.
10. An arrow is drawn at the lane center position on the `warp_color_channel` using
`draw_arrows` function.

53
11. The `warp_color_channel` is warped back to the original perspective using the inverse
perspective matrix (`h_inv`) obtained from `cv2.getPerspectiveTransform`.
12. The final output image is created by blending the original image (`cropped_img`) and
the warped lane image (`final_warped_image`) using `cv2.addWeighted`.
13. The predicted turn direction and the radius of curvature for both the left and right lanes
are added as text on the `final_output` image using `cv2.putText`.
14. The function returns `final_output`, `left_points`, `right_points`, and
`warp_color_channel` for further analysis and visualization.

4.8 Eighth Block of Code

The `show_two_images` function takes two images as input and displays them side by side
with text labels. Here's how it works:
1. The function adds a white border around each image using `cv2.copyMakeBorder` to
provide some spacing between the images.
2. Text labels are added to each image using `cv2.putText`.
3. The two images are concatenated horizontally using `np.concatenate` and assigned to the
variable `Hor`.
4. The concatenated image is saved to a file named
`'problem3/output_images/output_image.jpg'` using `cv2.imwrite`.
5. The concatenated image is displayed using `cv2.imshow`.
54
6. The function waits for a key press to close the displayed image using `cv2.waitKey`.
7. Finally, the function destroys all open windows using `cv2.destroyAllWindows`.

The `show_output_video` function combines multiple images and arranges them into a final
output frame. Here's an explanation of the function:
1. A blank image (`final_img`) of size 1920x1080 is created.
2. The `org_img`, `edges_img`, `bird_img`, `sliding_img`, `final_output`, and `warped`
images are assigned to different sections of the `final_img` according to their positions in
the final layout.
3. Text labels are added to each section of the `final_img` using `cv2.putText`.
4. A neon-colored strip (`neon`) is created to separate the sections visually.
5. The `neon` strip and `final_img` are vertically concatenated using `cv2.vconcat`.
6. Text labels are added to the `final_img` to indicate the corresponding sections.
7. The function returns the `final_img` as the output.

4.9 Ninth Block of Code

This code is used in a video for a bird's eye view perspective. Here's an explanation of the
code:

1. The source points (`bird_eye_coords_`) and destination points (`world_coords_`) are


defined for the homography transformation. These points are used to map the bird's eye
55
view of the road to a flat world coordinate system.
2. The `filename_cal` variable specifies the path pattern for the calibration images used to
calibrate the camera.
3. The video file to process is loaded using `cv2.VideoCapture` with the specified filename.
4. A loop is started to process each frame of the video.
5. The frame is read using `cap.read()`, and if the frame is successfully read (`ret` is True),
the processing continues.
6. The frame number is incremented (`Frame+=1`) and printed.
7. The `img` frame is processed to extract the edges using the `process_image` function. The
resulting edges image is stored in `img_edges`.
8. The frame is cropped to focus on the region of interest using array slicing
(`cropped_img`).
9. The homography matrix `h_` and mask are calculated using `cv2.findHomography` with
the source and destination points.
10. The edges image (`img_edges`) and cropped image (`cropped_img`) are warped using
the calculated homography matrix to obtain the bird's eye view of the edges (`im2_n`) and
the corresponding sliding window image (`im_sliding`).
11. The sliding window technique is applied to detect the lane lines using the
`sliding_window` function. The resulting fitted lines, output image, and predicted turn
(`l_fit`, `r_fit`, `out_img`, `pred`) are obtained.
12. The fitted lines (`left` and `right`) and the output image (`warped_color`) are passed to
the `project_back` function to draw the lane lines, calculate the radius of curvature, and add
text overlays.
13. The `draw_lines` function is used to draw the detected lane lines on the sliding window
image (`im_sliding`), and the resulting image is stored in `lanes_img`.
14. The `show_output_video` function is called to combine different images into a final
output frame (`fin_img`).
15. The `fin_img` is displayed using `cv2.imshow` with the window title 'Final Output'.
16. The `fin_img` is written to the output video file using `result.write`.
17. The loop continues until all frames are processed.
18. Finally, the video writer is released (`result.release()`) and the OpenCV windows are
closed (`cv2.destroyAllWindows()`).

56
4.10 Tenth Block of Code

Lastly, we added some code for handling keyboard events, releasing resources, and printing
a success message after processing the video. Here's an explanation of the code:

1. Inside the frame processing loop, `cv2.waitKey(1)` is used to wait for a keyboard event
for 1 millisecond. The function returns a key code which is bitwise-ANDed with `0xFF` to
extract the lower 8 bits. If the key code corresponds to the letter 'q' (ord('q')), the loop is
broken using the `break` statement, effectively stopping the video processing.
2. If there are no more frames to process (ret is False), the loop is also broken using `break`
to exit the loop.
3. After the loop, `cap.release()` is called to release the resources associated with the video
capture object.
4. `result.release()` is called to release the resources associated with the video writer object.
5. `cv2.destroyAllWindows()` is called to close all OpenCV windows.
6. Finally, a success message is printed to indicate that the video has been processed
successfully.

57
5. Results Discussion
In the code, the output image firstly undergoes a process called image equalization. Image
equalization is a technique used to enhance the contrast of an image by spreading out the
intensity values across the entire dynamic range. It is commonly used to improve the visual
appearance of an image and make it more visually appealing.

Here's an explanation of what happens during the image equalization process in the code:

1. Grayscale Conversion:
- First, the input image is converted to grayscale using the `cv2.cvtColor()` function.
- Converting the image to grayscale simplifies the equalization process as it focuses only
on the intensity values rather than color information.

2. Histogram Calculation:
- Next, the histogram of the grayscale image is calculated using the `cv2.calcHist()`
function.
- A histogram is a graphical representation of the distribution of pixel intensities in an
image.
- In this step, the histogram represents the frequency of occurrence of each intensity value
in the grayscale image.

3. Cumulative Distribution Function (CDF) Calculation:


- The cumulative distribution function (CDF) is computed from the histogram values.
- The CDF represents the cumulative sum of probabilities of each intensity value in the
histogram.
- It provides information about the distribution of intensities and their cumulative
contribution to the image.

4. Histogram Equalization:
- Using the CDF, the histogram equalization process is performed on the grayscale image.
- Each pixel intensity value in the input image is transformed based on the CDF.
- The transformation spreads out the intensity values to cover the entire dynamic range,
enhancing the contrast.
58
5. Histogram Equalized Image:
- The result of the histogram equalization process is the equalized image.
- The equalized image has improved contrast compared to the original grayscale image.
- Dark regions become darker, and bright regions become brighter, resulting in a visually
enhanced image.

By equalizing the image, the goal is to improve the visibility and distinguishability of lane
markings and other details in the image. This can help the subsequent image processing
steps, such as edge detection or lane detection algorithms, to perform more accurately and
effectively.

5.1. Original Image

5.2. Image After Histogram Equalisation

59
Next, we apply Adaptive equalization to the image, also known as adaptive histogram
equalization (AHE). This is an extension of histogram equalization that addresses its
limitations in handling images with varying local contrast. While traditional histogram
equalization enhances global contrast, adaptive equalization adapts the equalization process
to different regions of the image based on their local characteristics.

In adaptive equalization, the image is divided into smaller regions, such as tiles or blocks.
For each region, the histogram equalization process is applied individually. This allows for a
more localized enhancement of contrast, which can preserve the details in different areas of
the image.

Here's an overview of what typically happens during adaptive equalization:

1. Image Partitioning:
- The input image is divided into smaller regions, usually using a fixed size or a sliding
window approach.
- Each region represents a local area of the image where contrast enhancement will be
applied individually.

2. Local Histogram Calculation:


- For each region, the histogram of the pixel intensities is computed.
- The histogram represents the frequency of occurrence of each intensity value within the
region.

3. Local Cumulative Distribution Function (CDF) Calculation:


- Based on the local histogram, the local cumulative distribution function (CDF) is
computed.
- The CDF provides information about the distribution of intensities within the specific
region.

4. Local Histogram Equalization:


- Using the local CDF, histogram equalization is applied to each region independently.
- The transformation of pixel intensities is localized to the specific region, adapting to its

60
local contrast characteristics.

5. Combining the Local Results:


- The locally equalized regions are combined to form the final output image.
- The final output image exhibits enhanced contrast in both global and local areas,
preserving the details and textures.

Adaptive equalization is particularly useful in images with non-uniform lighting conditions


or scenes containing areas with significantly different contrasts. It allows for more
fine-grained control over the enhancement process and can produce visually pleasing results
with improved detail preservation.

5.3. Original Image

5.4. Image After Adaptive Histogram Equalisation

61
When the CLAHE (Contrast Limited Adaptive Histogram Equalization) algorithm is applied
to the image, the output image undergoes contrast enhancement with adaptive equalization
while limiting the amplification of noise or artifacts.

1. Image Partitioning:
- The input image is divided into smaller tiles or blocks.
- Each tile represents a local region within the image.

2. Histogram Calculation:
- For each tile, the histogram of the pixel intensities is computed.
- The histogram represents the frequency of occurrence of each intensity value within the
tile.

3. Contrast Limiting:
- A contrast limiting mechanism is applied to the histogram.
- This prevents excessive amplification of noise or artifacts that may be present in the
image.

4. Histogram Equalization:
- The histogram equalization process is performed on each tile individually.
- This redistributes the pixel intensities within the tile, enhancing local contrast.

5. Interpolation:
- To avoid visible artifacts at the boundaries of the tiles, interpolation is applied.
- The pixel intensities are smoothly interpolated between adjacent tiles to ensure a
seamless transition.

6. Combining the Local Results:


- The locally equalized tiles are combined to form the final output image.
- The final output image exhibits enhanced contrast with adaptive equalization,
considering the local characteristics of different regions.

62
The CLAHE algorithm improves upon traditional histogram equalization by adapting the
equalization process to different regions of the image while limiting the amplification of
noise or artifacts. This ensures that the contrast enhancement is performed in a controlled
manner, leading to visually pleasing results.

5.5. Original Image

5.6. Image After CLAHE

63
In the above code, the final output video is generated by combining various stages of lane
detection and visualization, including- Video Processing, Lane Detection, Visualization,
Image Composition, Text Overlay, Output Generation, and Display.

By following these steps, the code combines the lane detection results, visualizations, and
relevant information into a cohesive video output. The final output video provides a visual
representation of the detected lanes, along with additional details such as curvature radius
and turn prediction, helping to analyze and understand the lane detection process.

The final output video generated by the code combines various visual elements to provide a
comprehensive representation of the lane detection process. Here's an explanation of what is
going on in the final output video:

1. Main Image:
- The original frames from the input video are displayed in the top-left portion of the
output video.
- This section shows the actual view of the road captured by the camera.

2. Detected Lanes:
- The detected lanes are displayed in the top-right portion of the output video.
- The lane lines are highlighted using colored lines or polygons, indicating their position
and extent on the road.

3. Bird's-eye View:
- A bird's-eye view of the detected lanes is shown in the bottom-right portion of the output
video.
- This view provides a top-down perspective of the lane markings, which helps in
understanding the lane geometry and curvature.

4. Sliding Window:
- The sliding window technique used for lane detection is visualized in the bottom-left
portion of the output video.
- This visualization shows the sliding window moving along the lane lines, demonstrating

64
the process of identifying and tracking the lanes.

5. Polynomial Fit:
- The process of fitting a polynomial curve to the detected lane lines is visualized in the
bottom-right portion of the output video.
- This visualization demonstrates how the polynomial curve accurately represents the
shape of the lanes.

6. Lane Detection:
- The final output of the lane detection algorithm, which includes the detected lanes
overlaid on the main image, is displayed in the center portion of the output video.
- This section combines the original frame, the detected lanes, and additional graphical
elements to provide a comprehensive view of the lane detection results.

5.7. Screenshot of Final Video Output

65
6. References

1. Lucas Tabelini, Rodrigo Berriel, Thiago M. Paixao, Claudine Badue, Alberto F. De Souza,
Thiago Oliveira-Santos, “Keep Your Eyes On The Lane: Real Time Attention Guided Lane
Detection” , Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern
Recognition(CVPR), 2021
2. Noa Garnett, Rafi Cohen, Tomer Pe'er, Roee Lahav, Dan Levi, “ 3D-LaneNet:End-to-End
3D Multiple Lane Detection” , Proceedings of the IEEE/CVF International Conference on
Computer Vision (ICCV), 2019
3. Wouter Van Gansbeke, Bert De Brabandere, Davy Neven, Marc Proesmans, Luc Van Gool,
“End-to-end Lane Detection through Differentiable Least-Squares Fitting”, Proceedings of
the IEEE/CVF International Conference on Computer Vision (ICCV), 2019
4. Yuenan Hou, Zheng Ma, Chunxiao Liu, Chen Change Loy, “Learning Lightweight Lane
Detection CNNs by Self Attention Distillation”, Proceedings of the IEEE/CVF International
Conference on Computer Vision (ICCV), 2019
5. Rama Sai Mamidala, Uday Uthkota, Mahamkali Bhavani Shankar, A. Joseph Antony, A. V.
Narasimha Dhan, “Dynamic Approach for Lane Detection using Google Street View and
CNN”, TENKON 2019 IEEE Region 10 Conference
6. Bin Liu, Hongzhe Liu, Jiazheng Yuan, “Lane Line Detection based on Mask R-CNN”, April
2019
7. Der-Hau Lee & Jinn-Liang Liu, “End-to-end deep learning of lane detection and path
prediction for real-time autonomous driving”, April 2022
8. Dan Qiao, Xiaoyu Wu, Tiantian Wang, “A Lane Recognition Based on Line-CNN
Network”, 2020 Asia-Pacific Conference on Image Processing, Electronics and Computers
(IPEC)
9. FAN Chao, XU Jing-bo, DI Shuai, “Lane Detection Based on Machine Learning
Algorithm”, TELKOMNIKA Indonesian Journal of Electrical Engineering 2014
10. Junfeng Li, Dehai Zhang, Yu Ma , Qing Liu, “Lane Image Detection Based on Convolution
Neural Network Multi-Task Learning”, 2021
11. R Shreyas, Sai Karthik P K, R Ajay, Prof. Karthik S A, “ Real Time Road Lane Detection
Using Deep Convolutional Neural Network”, 2021
12. Wei Wang, Junshu Wang, Hui Lin, “CNN based lane detection with instance segmentation
in edge-cloud computing”, 2020
66
13. Qin Zou, Hanwen Jiang, Qiyu Dai, Yuanhao Yue, Long Chen, and Qian Wang,
“Robust Lane Detection from Continuous Driving Scenes Using Deep Neural Networks”,
2019
14. Fan Chao , Song Yu-Pei , Jiao Ya-Jie, “Multi-Lane Detection Based on Deep Convolutional
Neural Network”, 2019
15. Elio Tuci, Frederic Labrosse, Muhanad H.M. Alkilabi, “Road Detection Using
Convolutional Neural Networks”, 2021

67

You might also like