The goals / steps of this project are the following:
- Compute the camera calibration matrix and distortion coefficients given a set of chessboard images.
- Apply a distortion correction to raw images.
- Use color transforms, gradients, etc., to create a thresholded binary image.
- Apply a perspective transform to rectify binary image ("birds-eye view").
- Detect lane pixels and fit to find the lane boundary.
- Determine the curvature of the lane and vehicle position with respect to center.
- Warp the detected lane boundaries back onto the original image.
- Output visual display of the lane boundaries and numerical estimation of lane curvature and vehicle position.
The code for this step is contained in the first code cell of the IPython notebook located in ./Advanced-Lane-Finder.ipynb
.
I start by preparing "object points", which will be the (x, y, z)
coordinates of the chessboard corners in the world.
Here I am assuming the chessboard is fixed on the (x, y)
plane at z = 0
, such that the object points are the same for each calibration image.
Thus, objp
is just a replicated array of coordinates, and objpoints
will be appended with a copy of it every time I successfully detect
all chessboard corners in a test image. imgpoints
will be appended with the (x, y)
pixel position of each of the corners in the image plane
with each successful chessboard detection.
I then used the output objpoints
and imgpoints
to compute the camera calibration and distortion coefficients using the cv2.calibrateCamera()
function. I applied this distortion correction to the test image using the cv2.undistort()
function and obtained this result:
To demonstrate this step, I will describe how I apply the distortion correction to one of the test images like this one:
I used a combination of color and gradient thresholds to generate a binary image. Here's an example of my output for this step.
The lightness channel worked well for white lines but not yellow linee and blue channel worked very well for picking up the yellow lines.
I filtered the image using these two channels and then combined them to get the final binary image.
The code for my perspective transform includes a function called persp_transform()
. The function takes as inputs an image (img
), as well as source (src
) and destination (dst
) points. I chose the hardcode the source and destination points so I could get the most accurate transformation:
offset = 400
w = img.shape[1]
h = img.shape[0]
src = np.float32([
(575, 464),
(710, 464),
(220, 710),
(1088, 710)
])
dst = np.float32([
(offset, 0),
(w - offset, 0),
(offset, h),
(w - offset, h)
])
This resulted in the following source and destination points:
Source | Destination |
---|---|
575, 464 | 400, 0 |
710, 464 | 880, 0 |
220, 710 | 400, 720 |
1088, 710 | 880, 720 |
I verified that my perspective transform was working as expected by drawing the src
and dst
points onto a test image and its warped counterpart to verify that the lines appear parallel in the warped image.
Then I used a sliding window search to find lane line pixels within a margin around the histogram peaks.
Then a 2nd order polyfit is used to find equations of the lane lines. The pipeline keeps track of the last n
line fits and updates it's best fit property depending on the percent difference of each estimation.
I calculate curvature and offset in calculate_curve_offset()
. Example output:
I implemented this step in the function draw_lane()
. Here is an example of my result on a test image:
Here's a link to the project video output
I started with straightforward steps of camera calibration, undistorting and perspective transform. The gradient and color thresholds took some trial and error. I found that the gradient in the x-direction worked best on the lightness channel. I also combined two color thresholds: blue and lightness channels. The blue channel worked very well at extracting the yellow lines and the lightness channel picked up white lane lines (and occasional bright wheel rims).
The pipeline worked well on the project video. I tried the same pipeline on the harder challenge videos but they were total failures. The color channel thresholds were capturing too much peripheral noise from shadows, cars and scenery, resulting in very poor lane line approximations.
Some tweaking of the color thresholds would help but it would be ideal if cropping could be used to completely eliminate peripheral objects. Perhaps a tighter perspective transform but of course this would make assumptions about the lane widths and road conditions.
Perhaps using a method similar to the sliding window search but applied to the color thresholds could help. Knowing that as the region moves up the image toward the horizon, more noise is expected and color thresholds can be adjusted.