Back in China, I used to stand atop bridges that overlook the bustling roads of Beijing and recite the brand of every car that zoomed by. “Benz…Toyota…Nissan…Mazda…” My interest and passion for cars led me to conduct more research about these inventions. During my research, I encountered a tutorial on how to code a lane detection program, which looked very interesting, but quite challenging.

For detecting lane lines, this project uses OpenCV-Python. The first step in this process is **edge detection**, which identifies regions in an image with sharp changes in color intensity. Images are made up of individual pixels, and their intensity is represented on a scale of 0–255. Figure 0 represents the color black, while 255 is white. To make processing this image easier, convert this image into grayscale, or black and white. This way, there is only one channel of color, instead of the usual red, blue, and green.

Next, apply a **Gaussian Blur** to smooth out the image and reduce noise. Applying a Gaussian Blur will minimize errors when detecting the lane lines.

After applying the blur, use the **Canny function** to trace out areas with the strongest gradient, or change in color. Canny edge detection traces out areas with sharp changes in color intensity, which helps make the lane lines more discernible. The areas in black are the parts of the photo where the differences in intensity between pixels are small while the white lines represent parts with high changes in intensity.

With the lane lines marked clearly, focus on a specific region that encompasses the lane lines. The first step in doing so is using **Matplotlib**. Matplotlib is a plotting function in Python that creates coordinates. As shown in the image below, the three coordinates that encapsulate the region containing lane lines are as follows: (200, 700), (1100, 700), and (550, 250).

The three points create a triangular region, which we will create a mask for. Create this mask using a NumPy array to specify the three coordinates and enter the command “np.zeros_like” to set the pixel intensity to 0, or black. Then, use OpenCV’s fillPoly function to fill our region of interest with white; we use “250” to set pixel intensity to the max. The mask helps focus on the region of interest only, instead of the entire image.

Next, find lines that best fit each lane line through the Hough transform function. **Hough transform** is a technique that is used to identify lines, among other things, using a voting procedure. The Hough space is similar to a regular *x-y* coordinate plane, also known as the Cartesian coordinate plane. Straight lines are usually represented by the equation *y = mx + b*, with ‘m’ as the slope and ‘*b*’ as the *y*-intercept. Such equations can be plotted into the Hough space as a single point, with m being the *x* coordinate and *b* as the *y(m, b)*.

What if we had a single point, instead of a line? There are an infinite amount of lines that can pass through this point, each with a unique slope and *y*-intercept. For each line that passes through this point, plot a point using this line’s slope and *y*-intercept onto the Hough space. Eventually, the family of equations that passes through this point forms a line in the Hough space.

Now, add a point onto our coordinate plane, which allows for the plotting of the family of lines passing this point on the Hough space. Eventually, the Hough space will be filled with lines, formed by individual data points from linear equations in the *x-y* plane. However, the lane lines on the image are not perfectly straight lines due to the curvature of the road and other factors. To solve this issue, find the line of best fit: the line that passes through the most data points. The Hough space can be split into grids, and these lines are then graphed onto the Hough space. Whichever grid contains the most points of intersection will be the line we use. With the Hough transform, find the lines of best fit and apply them onto the image.

The final step is to blend the image with the lines of best fit with the original image so that the lines can show up on the lanes instead of a black screen.