def camera_calibration(show_images=False):

    nx = 9
    ny = 6

    objPoints = []
    imgPoints = []
    shape = None
    images = glob.glob('./camera_cal/calibration*.jpg')

    # 3D Coordinates
    objP = np.zeros((nx*ny, 3), np.float32)
    objP[:, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)

    for filename in images:
        image = cv2.imread(filename)
        gray = grayscale(image)
        shape = gray.shape[::-1]
        ret, corners = cv2.findChessboardCorners(gray, (nx, ny), None)
        if show_images == True:
            image = cv2.drawChessboardCorners(
                image, (nx, ny), corners, ret)
            print(filename)
            # cv2.imshow('Cal', image)
            cv2.waitKey()

        if ret == True:
            imgPoints.append(corners)
            objPoints.append(objP)

    # print(objPoints)
    ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(
        objPoints, imgPoints, shape, None, None)

    return mtx, dist
Exemple #2
0
def filter():
    """Apply an instagram like filter"""
    # User reached route via POST (as by submitting a form via POST)
    if request.method == "POST":

        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)

        file = request.files['file']

        if file.filename == '':
            flash('No image selected for uploading')
            return redirect(request.url)

        # Ensure credit card number was submitted
        if not request.form["customRadio"]:
            flash('No filter selected')
            return redirect(request.url)

        # Get chosen filter
        filter = request.form["customRadio"]

        # Check if file format is allowed
        if not allowed_file(file.filename):
            flash('Allowed image types are: .bmp')
            return redirect(request.url)

        # Apply filter
        if filter == 'grayscale':
            filename = 'grayscale.bmp'
            filtername = 'Grayscale'
            filtered_image = grayscale(file)

        elif filter == 'reflect':
            filename = 'reflect.bmp'
            filtername = 'Reflect'
            filtered_image = reflect(file)

        elif filter == 'sepia':
            filename = 'sepia.bmp'
            filtername = 'Sepia'
            filtered_image = sepia(file)

        filtered_image.save(os.path.join(app.config['UPLOAD_FOLDER'],
                                         filename))

        flash('Image successfully uploaded and displayed')
        return render_template("filtered.html",
                               filename=filename,
                               filtername=filtername)

    # User reached route via GET (as by clicking a link or via redirect)
    else:
        return render_template("filters.html")
def process_image(image):

    # Pull out the x and y sizes and make a copy of the image
    ysize = image.shape[0]
    xsize = image.shape[1]
    region_select = np.copy(image)

    # Convert to Grayscale
    image_gray=helpers.grayscale(image)

    # Blurring
    blurred_image = helpers.gaussian_blur(image_gray, parameters.Blurring.kernel_size)

    # Canny Transform
    edges = helpers.canny(blurred_image, parameters.Canny.low_threshold, parameters.Canny.high_threshold)

    # Four sided polygon to mask
    imshape = image.shape
    lower_left = (50, imshape[0])
    upper_left = (400, 320)
    upper_right = (524, 320)
    lower_right = (916, imshape[0])
    parameters.Masking.vertices = np.array([[lower_left, upper_left, upper_right, lower_right]], dtype=np.int32)

    # masking
    masked_edges = helpers.region_of_interest(edges, parameters.Masking.vertices)

    # Run Hough on edge detected image
    hough_lines,raw_hough_lines_img = helpers.hough_lines(masked_edges, parameters.Hough.rho, parameters.Hough.theta, parameters.Hough.threshold,
                             parameters.Hough.min_line_length, parameters.Hough.max_line_gap)

    # classify left and right lane lines
    left_lane_lines, right_lane_lines = helpers.classify_left_right_lanes(hough_lines)

    # Raw hough_lines image
    helpers.draw_lines(raw_hough_lines_img, hough_lines, color=[255, 0, 0], thickness=2)

    # RANSAC fit left and right lane lines
    fitted_left_lane_points = helpers.ransac_fit_hough_lines(left_lane_lines)
    fitted_right_lane_points = helpers.ransac_fit_hough_lines(right_lane_lines)
    helpers.draw_model(image, fitted_left_lane_points, color=[255, 0, 0], thickness=2)
    helpers.draw_model(image, fitted_right_lane_points, color=[255, 0, 0], thickness=2)

    # 1D Interpolator - does not work as good as RANSAC so its commented out
    # interpolated_left_lane_line = helpers.interpolate_hough_lines(left_lane_lines)
    # interpolated_right_lane_line = helpers.interpolate_hough_lines(left_lane_lines)
    # helpers.draw_model(image, interpolated_left_lane_line, color=[255, 0, 0], thickness=2)
    # helpers.draw_model(image, interpolated_right_lane_line, color=[255, 0, 0], thickness=2)

    # superpose images
    # superposed_image = helpers.weighted_img(image, raw_hough_lines_img, α=0.8, β=1., λ=0.)

    return image
def process_image(image):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image where lines are drawn on lanes)
    hsvMasked = helpers.hsvMaskConv(image)
    gray = helpers.grayscale(hsvMasked)

    # Define a kernel size and apply Gaussian smoothing
    kernel_size = helpers.kernel_size
    blur_gray = helpers.gaussian_blur(gray, kernel_size)

    # Define our parameters for Canny and apply
    low_threshold = helpers.low_threshold
    high_threshold = helpers.high_threshold
    edges = helpers.canny(blur_gray, low_threshold, high_threshold)

    # Next we'll isolate the region of interest to apply the Hough transform upon
    mask = np.zeros_like(edges)
    ignore_mask_color = 255
    (imHeight, imWidth, __) = image.shape
    vertices = np.array([[(.10*imWidth,imHeight),(0.45*imWidth, 0.60*imHeight), (0.55*imWidth, 0.60*imHeight), (0.9*imWidth,imHeight)]], dtype=np.int32)
    masked_edges = helpers.region_of_interest(edges, vertices)

    # Define the Hough transform parameters
    # Make a blank the same size as our image to draw on
    rho = helpers.rho # distance resolution in pixels of the Hough grid
    theta = helpers.theta # angular resolution in radians of the Hough grid
    threshold = helpers.threshold   # minimum number of votes (intersections in Hough grid cell)
    min_line_length = helpers.min_line_length #minimum number of pixels making up a line
    max_line_gap = helpers.max_line_gap    # maximum gap in pixels between connectable line segments

    # Run Hough on edge detected image
    # Output "lines" is an array containing endpoints of detected line segments
    line_img, lines = helpers.hough_lines(masked_edges, rho, theta, threshold, min_line_length, max_line_gap)

    # Draw the lines on the edge image
    result = helpers.weighted_img(line_img, image, α=0.8, β=1., γ=0.)
    return result
def process_image(image):
    # CONVERT TO GRAYSCALE
    gray = helpers.grayscale(image)
    # APPLY GAUSSIAN BLUR
    kernel_size = 7  # Must be an odd number.
    blur_gray = helpers.gaussian_blur(gray, kernel_size)

    # APPLY CANNY EDGE DETECTOR
    low_threshold = 70
    high_threshold = 140
    edges = helpers.canny(blur_gray, low_threshold, high_threshold)

    # DEFINE REGION OF INTEREST
    imshape = image.shape
    line_height = 330
    vertices = np.array([[(0, imshape[0]), (435, line_height),
                          (540, line_height), (imshape[1], imshape[0])]],
                        dtype=np.int32)

    masked_edges = helpers.region_of_interest(edges, vertices)

    # APPLY HOUGH TRANSFORMATION
    rho = 2  # distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 50  # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 100  # minimum number of pixels making up a line
    max_line_gap = 100  # maximum gap in pixels between connectable line segments
    # Run Hough on edge detected image
    # Output "lines" is an array containing endpoints of detected line segments
    lines = helpers.hough_lines(masked_edges, rho, theta, threshold,
                                min_line_length, max_line_gap, line_height)

    # Draw the lines on the edge image
    lines_edges = helpers.weighted_img(lines, image, 0.8, 1, 0)

    plt.imshow(lines_edges, cmap='gray')
    return lines_edges
Exemple #6
0
    'solidYellowCurve.jpg', 'solidYellowCurve2.jpg', 'solidYellowLeft.jpg',
    'solidWhiteCurve.jpg', 'solidWhiteRight.jpg', 'whiteCarLaneSwitch.jpg'
]
image_files = [
    img for img in image_filesList if any(x in img for x in imgList)
]

fig = plt.figure(figsize=(12, 8))

for idx, image in enumerate(image_files[:6]):

    os.chdir(
        "/Users/sumedhinamdar/Documents/GitHub/CarND-LaneLines-P1/test_images")
    currentImage = mpimg.imread(image)
    hsvMasked = helpers.hsvMaskConv(currentImage)
    gray = helpers.grayscale(hsvMasked)

    # Define a kernel size and apply Gaussian smoothing
    kernel_size = helpers.kernel_size
    blur_gray = helpers.gaussian_blur(gray, kernel_size)

    # Define our parameters for Canny and apply
    low_threshold = helpers.low_threshold
    high_threshold = helpers.high_threshold
    edges = helpers.canny(blur_gray, low_threshold, high_threshold)

    # Next we'll isolate the region of interest to apply the Hough transform upon
    mask = np.zeros_like(edges)
    ignore_mask_color = 255
    (imHeight, imWidth, __) = currentImage.shape
    vertices = np.array([[(.10 * imWidth, imHeight),
Exemple #7
0
imageSourceDir = "test_images/"
imageTestDir = "test_images/"

for i in os.listdir(imageSourceDir):

    # Make copies into the test_images directory
    image = mpimg.imread(os.path.join(imageSourceDir, i))

    # Pull out the x and y sizes and make a copy of the image
    ysize = image.shape[0]
    xsize = image.shape[1]
    region_select = np.copy(image)

    # Convert to Grayscale
    image_gray = helpers.grayscale(image)

    # Blurring
    blurred_image = helpers.gaussian_blur(image_gray,
                                          parameters.Blurring.kernel_size)

    # Canny Transform
    edges = helpers.canny(blurred_image, parameters.Canny.low_threshold,
                          parameters.Canny.high_threshold)

    # Four sided polygon to mask
    imshape = image.shape
    lower_left = (50, imshape[0])
    upper_left = (400, 320)
    upper_right = (524, 320)
    lower_right = (916, imshape[0])
def buildLines(image):
    # Apply a gray scale
    grayScaleImage = grayscale(image)

    # Apply the gausian blur
    blurredImage = gaussian_blur(grayScaleImage, 15)

    # Apply Canny Edge detection
    cannyImage = canny(blurredImage, 70, 100)

    # Make a ROI mask
    h = image.shape[0]
    w = image.shape[1]

    p1 = (w / 10, h - 10)
    p2 = (int(w * 3 / 7), int(h * 0.6))
    p3 = (int(w * 4 / 7), int(h * 0.6))
    p4 = (w * 9 / 10, h - 10)

    # Mask canny edges with ROI
    cullVerts = np.array([[p1, p2, p3, p4]], dtype=np.int32)
    cannyImage = region_of_interest(cannyImage, cullVerts)

    # Get lines from Hough space
    rho = 2
    theta = np.pi / 180
    threshold = 1
    minLineLength = 15
    maxLineGap = 5

    lines = cv2.HoughLinesP(cannyImage, rho, theta, threshold, np.array([]),
                            minLineLength, maxLineGap)

    # Transform into numpy arrays for easy processing
    # Each array represents [x1, y1, x2, y2]
    nplines = []

    for l in lines:
        nplines.append(
            np.array([
                np.float32(l[0][0]),
                np.float32(l[0][1]),
                np.float32(l[0][2]),
                np.float32(l[0][3])
            ]))

    nplines = [
        l for l in nplines if 0.5 <= np.abs((l[3] - l[1]) / (l[2] - l[0])) <= 2
    ]

    # Sort the lines based on whether they are likely to be the left or right lane marker
    leftLaneLines = []
    rightLaneLines = []

    for l in nplines:
        sortLine(l, leftLaneLines, rightLaneLines)

    # Calculate the left lane line
    # We use the median here because it gives us better performance for video
    leftLaneOffset = np.median([(l[1] - (l[3] - l[1]) * l[0] / (l[2] - l[0]))
                                for l in leftLaneLines]).astype(int)
    leftLaneSlope = np.median([((l[3] - l[1]) / (l[2] - l[0]))
                               for l in leftLaneLines])

    # We use basic line algebra here, y_n = slope * x_n + offset
    leftLaneLine = np.array(
        [0, leftLaneOffset, -int(np.round(leftLaneOffset / leftLaneSlope)), 0])

    # Calculate the right lane line
    rightLaneOffset = np.median([(l[1] - (l[3] - l[1]) * l[0] / (l[2] - l[0]))
                                 for l in rightLaneLines]).astype(int)
    rightLaneSlope = np.median([((l[3] - l[1]) / (l[2] - l[0]))
                                for l in rightLaneLines])

    # Must account for image origin being at the top left corner here
    rightLaneLine = np.array([
        0, rightLaneOffset,
        int(np.round(
            (cannyImage.shape[0] - rightLaneOffset) / rightLaneSlope)),
        cannyImage.shape[0]
    ])

    return leftLaneLine, rightLaneLine