def calculateLines(img, lines):
    '\n    Combines line segments into one or two lanes\n    Note: By looking at the slop of a line we can see if it is on the left side (m<0) or right (m>0)\n    '

    def calculateCoordinates(img, line_params):
        '\n        Calculates the coordinates for a road lane\n        '
        (height, width, _) = img.shape
        (m, b) = line_params
        y1 = height
        y2 = int((y1 * (1 / 2)))
        x1 = max((- width), min((2 * width), int(((y1 - b) / m))))
        x2 = max((- width), min((2 * width), int(((y2 - b) / m))))
        return np.array([x1, y1, x2, y2])
    lane_lines = []
    if (lines is None):
        return np.array(lane_lines)
    (height, width, _) = img.shape
    (left_lines, right_lines) = ([], [])
    boundary = (1 / 3)
    left_region_boundary = (width * (1 - boundary))
    right_region_boundary = (width * boundary)
    for line in lines:
        (x1, y1, x2, y2) = line.reshape(4)
        if (x1 == x2):
            continue
        line_params = np.polyfit((x1, x2), (y1, y2), 1)
        (slope, intercept) = (line_params[0], line_params[1])
        if (slope < 0):
            if ((x1 < left_region_boundary) and (x2 < left_region_boundary)):
                left_lines.append((slope, intercept))
        elif ((x1 > right_region_boundary) and (x2 > right_region_boundary)):
            right_lines.append((slope, intercept))
    left_lines_avg = np.average(left_lines, axis=0)
    right_lines_avg = np.average(right_lines, axis=0)
    if (len(left_lines) > 0):
        left_line = calculateCoordinates(img, left_lines_avg)
        lane_lines.append(left_line)
    if (len(right_lines) > 0):
        right_line = calculateCoordinates(img, right_lines_avg)
        lane_lines.append(right_line)
    return np.array(lane_lines)
Combines line segments into one or two lanes
Note: By looking at the slop of a line we can see if it is on the left side (m<0) or right (m>0)
def calculateLines(img, lines):
    '\n    Combines line segments into one or two lanes\n    Note: By looking at the slop of a line we can see if it is on the left side (m<0) or right (m>0)\n    '

    def calculateCoordinates(img, line_params):
        '\n        Calculates the coordinates for a road lane\n        '
        (height, width, _) = img.shape
        (m, b) = line_params
        y1 = height
        y2 = int((y1 * (1 / 2)))
        x1 = max((- width), min((2 * width), int(((y1 - b) / m))))
        x2 = max((- width), min((2 * width), int(((y2 - b) / m))))
        return np.array([x1, y1, x2, y2])
    lane_lines = []
    if (lines is None):
        return np.array(lane_lines)
    (height, width, _) = img.shape
    (left_lines, right_lines) = ([], [])
    boundary = (1 / 3)
    left_region_boundary = (width * (1 - boundary))
    right_region_boundary = (width * boundary)
    for line in lines:
        (x1, y1, x2, y2) = line.reshape(4)
        if (x1 == x2):
            continue
        line_params = np.polyfit((x1, x2), (y1, y2), 1)
        (slope, intercept) = (line_params[0], line_params[1])
        if (slope < 0):
            if ((x1 < left_region_boundary) and (x2 < left_region_boundary)):
                left_lines.append((slope, intercept))
        elif ((x1 > right_region_boundary) and (x2 > right_region_boundary)):
            right_lines.append((slope, intercept))
    left_lines_avg = np.average(left_lines, axis=0)
    right_lines_avg = np.average(right_lines, axis=0)
    if (len(left_lines) > 0):
        left_line = calculateCoordinates(img, left_lines_avg)
        lane_lines.append(left_line)
    if (len(right_lines) > 0):
        right_line = calculateCoordinates(img, right_lines_avg)
        lane_lines.append(right_line)
    return np.array(lane_lines)