udacity / sdc-issue-reports

29 stars 6 forks source link

Lesson 16: "Camera Calibration" - Step 18: "Undistort and Transform" - grader too sensitive #1405

Closed megaserg closed 5 years ago

megaserg commented 6 years ago

If one uses the (supposedly correct) solution code from Step 19: "How I did it", but only changes offset to 200 (roughly what my solution was), the grader complains: "Try again", "Oops! looks like you got a different transform matrix than I was expecting..."

# Define a function that takes an image, number of x and y points, 
# camera matrix and distortion coefficients
def corners_unwarp(img, nx, ny, mtx, dist):
    # Use the OpenCV undistort() function to remove distortion
    undist = cv2.undistort(img, mtx, dist, None, mtx)
    # Convert undistorted image to grayscale
    gray = cv2.cvtColor(undist, cv2.COLOR_BGR2GRAY)
    # Search for corners in the grayscaled image
    ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)

    if ret == True:
        # If we found corners, draw them! (just for fun)
        cv2.drawChessboardCorners(undist, (nx, ny), corners, ret)
        # Choose offset from image corners to plot detected corners
        # This should be chosen to present the result at the proper aspect ratio
        # My choice of 100 pixels is not exact, but close enough for our purpose here
        offset = 200 # offset for dst points <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< changed
        # Grab the image shape
        img_size = (gray.shape[1], gray.shape[0])

        # For source points I'm grabbing the outer four detected corners
        src = np.float32([corners[0], corners[nx-1], corners[-1], corners[-nx]])
        # For destination points, I'm arbitrarily choosing some points to be
        # a nice fit for displaying our warped result 
        # again, not exact, but close enough for our purposes
        dst = np.float32([[offset, offset], [img_size[0]-offset, offset], 
                                     [img_size[0]-offset, img_size[1]-offset], 
                                     [offset, img_size[1]-offset]])
        # Given src and dst points, calculate the perspective transform matrix
        M = cv2.getPerspectiveTransform(src, dst)
        # Warp the image using OpenCV warpPerspective()
        warped = cv2.warpPerspective(undist, M, img_size)

    # Return the resulting image and matrix
    return warped, M
mvirgo commented 5 years ago

200 is not actually a working answer here - 100 pixels makes the undistorted image essentially fill a new image, while much higher introduces more and more significant blank space to the new image. This is working as intended.