Exemplo n.º 1
0
def calib_for_date(date, data_dir='D:\\RawData'):

    base_dir = os.path.join(data_dir, date)
    img_dir = os.path.join(base_dir, date + '_calib', 'images')

    if not os.path.exists(img_dir):
        raise ValueError("Data not found!")

    img_names = sorted(os.listdir(img_dir))

    w, h, camera_matrix, dist_coefs, rvecs, tvecs = calib.camera_calibration(
        img_dir, img_names)

    print("Camera calibration finished! Saving results...")

    yaml_name = os.path.join(base_dir, 'cam_calib.yaml')
    calib.save_cam_calib_yaml(yaml_name, w, h, camera_matrix, dist_coefs,
                              rvecs, tvecs)
Exemplo n.º 2
0
ax.imshow(np.array(I1).astype(np.uint8))
plt.show()
print("fundamental matrix")
print("{}\n".format(F))

#--------------------camera.  calibration----------------------
# Load 3D points, and their corresponding locations in
# the two images.
pts_3d = np.loadtxt('./data/lab_3d.txt')
matches = np.loadtxt('./data/lab_matches.txt')
pt1_2d = matches[:, :2]
pt2_2d = matches[:, 2:]

# <YOUR CODE> print lab camera projection matrices:
lab1_proj, lab1_K, lab1_R, lab1_c = \
              camera_calibration.camera_calibration(pts_3d, pt1_2d)# <YOUR CODE>
lab2_proj, lab2_K, lab2_R, lab2_c = \
              camera_calibration.camera_calibration(pts_3d, pt2_2d)# <YOUR CODE>

print('lab 1 camera projection P \n {} \n'.format(lab1_proj))
print('lab 1 intrinsic matrix K \n {} \n'.format(lab1_K))
print('lab 1 rotation matrix R \n {} \n'.format(lab1_R))
print('lab 1 camera center c \n {} \n'.format(lab1_c))

print('lab 2 camera projection P \n {} \n'.format(lab2_proj))
print('lab 2 intrinsic matrix K \n {} \n'.format(lab2_K))
print('lab 2 rotation matrix R \n {} \n'.format(lab2_R))
print('lab 2 camera center c \n {} \n'.format(lab2_c))

# evaluate the residuals for both estimated cameras
_, lab1_res = util.evaluate_points(lab1_proj, matches[:, :2], pts_3d)
Exemplo n.º 3
0
    #cv2.waitKey(0)
    return output


if __name__ == '__main__':
    # Running calibration on calibration images
    do_calibration = False
    if do_calibration:
        calibration_images = "camera_cal/"
        dimension_list = [[5, 9], [6, 9], [6, 9], [6, 9], [6, 9], [6,
                                                                   9], [6, 9],
                          [6, 9], [6, 9], [6, 9], [6, 9], [6, 9], [6,
                                                                   9], [6, 9],
                          [6, 8], [6, 7], [6, 9], [6, 9], [6, 9], [6, 9]]
        mtx, dist = camera_calibration(calibration_images,
                                       dimension_list,
                                       display_corners=False)
    # Using previously created calibration coefficients
    else:
        mtx = np.array([[1.16067642 * 1000, 0.00000000, 6.65724920 * 100],
                        [0.00000000, 1.15796966 * 1000, 3.88971790 * 100],
                        [0.00000000, 0.00000000, 1.00000000]])
        dist = np.array(
            [[-0.25427498, 0.04987807, -0.00043039, 0.00027334, -0.1336389]])
    # Loading Test images
    #test_images = "test_images/"
    #all_files = os.listdir(test_images)
    #image_names = [file for file in all_files if file[-4:] == '.jpg']
    #init = True

    # Loading Video and running process_image function on each frame
    right_top_dst = [img_width - offset, 0]
    left_bottom_dst = [offset, img_height]
    right_bottom_dst = [img_width - offset, img_height]

    src = np.float32(
        [left_top_src, right_top_src, right_bottom_src, left_bottom_src])
    dst = np.float32(
        [left_top_dst, right_top_dst, right_bottom_dst, left_bottom_dst])

    M = cv2.getPerspectiveTransform(src, dst)
    Minv = cv2.getPerspectiveTransform(dst, src)
    warped = cv2.warpPerspective(img, M, img_size)
    return warped, M, Minv


objpoints, imgpoints = my_cc.camera_calibration()


def pipeline(img):
    undistored = my_cc.undistort(img, objpoints, imgpoints)

    sobelx = abs_sobel(undistored, 'x', (50, 255))
    sobely = abs_sobel(undistored, 'y', (25, 255))
    color_binary = color_select(undistored,
                                sthresh=(100, 255),
                                vtresh=(50, 255))

    combined = np.zeros_like(sobelx)
    combined[((sobelx == 1) & (sobely == 1) | (color_binary == 1))] = 1

    transformed, M, Minv = transform(combined)
def get_avarage_xfitted(fitsx, smooth=5):
    average = 0
    if len(fitsx) == smooth:
        for fitx in fitsx:
            average += fitx
        average /= 5
    return average


rightLine = [0] * 4
leftLine = [0] * 4
count = 0
smooth = 5

mtx, dist = camera_calibration()

leftLines = []
rightLines = []
lastLeftLine = Line()
lastRightLine = Line()

cap = cv2.VideoCapture('./project_video.mp4')
# cap = cv2.VideoCapture('./challenge_video.mp4')
# cap = cv2.VideoCapture('./harder_challenge_video.mp4')

detection = LaneDetection(nwindows=20)

fourcc = cv2.VideoWriter_fourcc('X', '2', '6', '4')
out = cv2.VideoWriter('output_video.mp4', fourcc, cap.get(cv2.CAP_PROP_FPS),
                      (1280, 720))
Exemplo n.º 6
0
def pipeline(write_images, show_images, show_images2, write_images2,
             show_images3, write_images3, show_images4, write_images4):
    """
    This is the pipeline used to develop the image annotation algoorithm used in main.py
    See main.py for mor information regarding the code
    :return: None
    """

    # Best guess initial polynomial
    left_fit = np.array([0, 0, 217])
    right_fit = np.array([0, 0, 950])

    # Calibration
    do_calibration = False
    if do_calibration:
        calibration_images = "camera_cal/"
        dimension_list = [[5, 9], [6, 9], [6, 9], [6, 9], [6, 9], [6,
                                                                   9], [6, 9],
                          [6, 9], [6, 9], [6, 9], [6, 9], [6, 9], [6,
                                                                   9], [6, 9],
                          [6, 8], [6, 7], [6, 9], [6, 9], [6, 9], [6, 9]]
        mtx, dist = camera_calibration(calibration_images,
                                       dimension_list,
                                       display_corners=False)
    else:
        mtx = np.array([[1.16067642 * 1000, 0.00000000, 6.65724920 * 100],
                        [0.00000000, 1.15796966 * 1000, 3.88971790 * 100],
                        [0.00000000, 0.00000000, 1.00000000]])
        dist = np.array(
            [[-0.25427498, 0.04987807, -0.00043039, 0.00027334, -0.1336389]])

    # Load Test Images
    test_images = "test_images/"
    all_files = os.listdir(test_images)
    image_names = [file for file in all_files if file[-4:] == '.jpg']
    init = True  # init flag decides if box algorithm or search near polynomial is used to find lane lines
    for image in image_names:
        print(test_images + image)
        img = cv2.imread(test_images + image)
        undistorted = cv2.undistort(img, mtx, dist, None, mtx)
        line_image = thresholds(undistorted,
                                s_thresh=(150, 255),
                                sx_thresh=(30, 100))
        if write_images:
            cv2.imwrite(test_images + "filtered/" + image, line_image * 255)
        if show_images:
            compare_images(undistorted, line_image, False, True,
                           'Undistorted Image', 'Filtered Image')
        warped, M = perspective_transform(line_image)
        if show_images2:
            compare_images(line_image, warped, True, True, 'Filtered Image',
                           'Warped Image')
        if write_images2:
            cv2.imwrite(test_images + "warped/" + image, warped * 255)
        if init:
            out_img, left_fit, right_fit, ploty, left_fitx, right_fitx, lefty, leftx, righty, rightx = fit_polynomial(
                warped)
            if show_images3:
                #print(out_img)
                compare_images(undistorted, out_img, True, True,
                               'Undistorted Image', 'Annotated Image')
            if write_images3:
                cv2.imwrite(test_images + "fitted/" + image, out_img)
        else:
            out_img, left_fit, right_fit, ploty, left_fitx, right_fitx, lefty, leftx, righty, rightx = \
                search_around_poly(warped, left_fit, right_fit)
            if show_images3:
                #print(out_img)
                compare_images(undistorted, out_img, True, True,
                               'Undistorted Image', 'Annotated Image')
            if write_images3:
                cv2.imwrite(test_images + "fitted/" + image, out_img)
        left_curv, right_curv, left_fit_cr, right_fit_cr = measure_curvature_real(
            warped, copy.copy(left_fit), copy.copy(right_fit))
        #print("Left Fit: {}".format(left_fit))
        #print("Right Fit: {}".format(right_fit))

        print("Left curvature: {}".format(left_curv))
        print("Right curvature: {}".format(right_curv))
        right_base = poly_result(right_fit, 720) * XM_PER_PIX
        left_base = poly_result(left_fit, 720) * XM_PER_PIX
        print("Left Base: {}".format(left_base))
        print("Right Base: {}".format(right_base))
        offset = lane_center_offset(left_base, right_base)
        print("Lane Center Offset: {}".format(offset))
        # Checking that they have similar curvature
        # Checking that they are separated by approximately the right distance horizontally
        # Checking that they are roughly parallel ???????
        if (left_curv / right_curv) < 1.5 and (left_curv / right_curv) > 0.66 \
                and right_fit_cr[0] - left_fit_cr[0] > 3 and right_fit_cr[0] - left_fit_cr[0] < 4.5:
            valid = True
        output = draw_image(warped, left_fitx, right_fitx, ploty, img, inv(M),
                            undistorted)
        if show_images4:
            compare_images(line_image, output, True, False, 'Line',
                           'Annotated Image')
        if write_images4:
            cv2.imwrite(test_images + "final/" + image, output)
Exemplo n.º 7
0
            plot_images(undist, out_img, 1, 'Undistored Image',
                        'Binary image in bird view', 20,
                        file_name + '_binary_bird_view',
                        (left_line.bestx, right_line.bestx, ploty))
            plot_images(undist, result, 0, 'Undistored Image',
                        'Processed Image', 20, file_name + '_final')

    return result


# Main function
if __name__ == '__main__':

    # 1. If needed compute the camera calibration matrix and distortion coefficients given a set of chessboard images.
    if cfg.compute_calib_params:
        camera_calibration()

    # 2. A test image processing
    if 0 == cfg.video_mode:
        images = glob.glob(cfg.test_img_folder + '*.jpg')
        for idx, fname in enumerate(images):
            img = mpimg.imread(fname)
            pipeline(img, fname)
    else:
        # 3. Processing a video frame

        # 3.1 Store a annotated video frame into a file
        if 1 == cfg.store_video:
            if cfg.clip_video:
                clip = VideoFileClip(cfg.video_file_name + '.mp4').subclip(
                    cfg.clip_start, cfg.clip_end)