Lecture_08_Lane Detection & Assignment
Lecture_08_Lane Detection & Assignment
Source - towardsdatascience
Lane Detections using OpenCV
Applying Canny Detector
• We will feed in our sample video for lane detection as a series of continuous frames (images)
• The Canny Detector is a multi-stage algorithm optimized for fast real-time edge detection. The
fundamental goal of the algorithm is to detect sharp changes in luminosity (large gradients),
such as a shift from white to black, and defines them as edges, given a set of thresholds. The
Canny algorithm has four main stages:
1. Noise Reduction
2. Intensity gradient
3. Non-maximum suppression
4. Hysteresis thresholding
Source - towardsdatascience
Lane Detections using OpenCV
Applying Canny Detector
1. Noise reduction -
As with all edge detection algorithms, noise is a crucial issue that often leads to false
detection. A 5x5 Gaussian filter is applied to convolve (smooth) the image to lower the
detector’s sensitivity to noise. This is done by using a kernel (in this case, a 5x5 kernel) of
normally distributed numbers to run across the entire image, setting each pixel value
equal to the weighted average of its neighbouring pixels.
Source - towardsdatascience
Lane Detections using OpenCV
Applying Canny Detector
2. Intensity gradient -
Lane Detections using OpenCV
Applying Canny Detector
2. Intensity gradient -
Source - docs.opencv.org
Lane Detections using OpenCV
Applying Canny Detector
2. Intensity gradient -
The edge "jump" can be seen more easily if we take the first derivative (actually, here
appears as a maximum)
So, from the explanation above, we can deduce that a method to detect edges in an
image can be performed by locating pixel locations where the gradient is higher than
its neighbors (or to generalize, higher than a threshold) Source - docs.opencv.org
Lane Detections using OpenCV
Applying Canny Detector
2. Intensity gradient -
Vertical changes: This is computed by convolving I with a kernel Gy with odd size.
For example for a kernel size of 3, Gy would be computed as:
Source - towardsdatascience
Lane Detections using OpenCV
Applying Canny Detector
3. Non Maximum Supression -
Non-maximum suppression is applied to “thin” and effectively sharpen the edges. For
each pixel, the value is checked if it is a local maximum in the direction of the gradient
calculated previously.
A is on the edge with a vertical direction. As gradient is normal to the edge direction,
pixel values of B and C are compared with pixel values of A to determine if A is a local
maximum. If A is local maximum, non-maximum suppression is tested for the next
point. Otherwise, the pixel value of A is set to zero and A is suppressed.
Source - towardsdatascience
Lane Detections using OpenCV
Applying Canny Detector
4. Hysterisis-
After non-maximum suppression, strong pixels are confirmed to be in the final map of
edges. However, weak pixels should be further analyzed to determine whether it
constitutes as edge or noise. Applying two pre-defined minVal and maxVal threshold
values, we set that any pixel with intensity gradient higher than maxVal are edges and
any pixel with intensity gradient lower than minVal are not edges and discarded. Pixels
with intensity gradient in between minVal and maxVal are only considered edges if they
are connected to a pixel with intensity gradient above maxVal.
Source - towardsdatascience
Lane Detections using OpenCV
Hough Transform
• In the Cartesian coordinate system, we can represent a straight line as y = mx + b by plotting y
against x. However, we can also represent this line as a single point in Hough space by plotting
b against m. For example, a line with the equation y = 2x + 1 may be represented as (2, 1) in
Hough space.
Source - towardsdatascience
Lane Detections using OpenCV
Hough Transform
• For example, a point at (2, 12) can be passed by y = 2x + 8, y = 3x + 6, y = 4x + 4, y = 5x + 2, y
= 6x, and so on. These possible lines can be plotted in Hough space as (2, 8), (3, 6), (4, 4), (5,
2), (6, 0). Notice that this produces a line of m against b coordinates in Hough space.
Source - towardsdatascience
𝜌 = 𝑥 cos 𝜃 + 𝑦 sin 𝜃
Automatic Detection
● Grayscale conversion of image: The video frames are in RGB format, RGB is converted to grayscale because
processing a single channel image is faster than processing a three-channel colored image.
● Reduce noise: Noise can create false edges, therefore before going further, it’s imperative to perform image smoothening.
Gaussian filter is used to perform this process.
● Canny Edge Detector: It computes gradient in all directions of our blurred image and traces the edges with large changes
in intensity.
5 5 5
4 4 4
3 3 3
● Region of Interest: This step is to take into account only the region covered by the road lane. A mask is created here,
which is of the same dimension as our road image. Furthermore, bitwise AND operation is performed between each
pixel of our canny image and this mask. It ultimately masks the canny image and shows the region of interest traced by
the polygonal contour of the mask.
• Hough Line Transform: The Hough Line Transform is a transform used to detect straight lines. The Probabilistic
Hough Line Transform is used here, which gives output as the extremes of the detected lines
Lane Detections using OpenCV
Demonstration !