def get_tv_to_rgb_matrix( rgb_calibration_file_names, tv_calibration_file_names, rgb_relative_file_names, tv_relative_file_names, chessboard_cell_width_meters, inner_width, inner_height, on_calibrated_signal=None): ret_rgb, mtx_rgb, dist_rgb, rvecs_rgb, tvecs_rgb, img_points_rgb, objpoints, used_rgb_files = \ calib.calibrate_camera( rgb_calibration_file_names, inner_width, inner_height, on_calibrated_signal) ret_tv, mtx_tv, dist_tv, rvecs_tv, tvecs_tv, img_points_tv, objpoints, used_tv_files = \ calib.calibrate_camera( tv_calibration_file_names, inner_width, inner_height, on_calibrated_signal) if rgb_calibration_file_names is not None: image_size = calib.get_image_size(rgb_calibration_file_names[0]) else: image_size = calib.get_image_size(rgb_relative_file_names[0]) objpoints = calib.build_obj_points(inner_width, inner_height) solo_calibrated_points_tv = img_points_tv solo_calibrated_points_rgb = img_points_rgb img_points_tv = [] img_points_rgb = [] for fname_rgb, fname_tv in zip(rgb_relative_file_names, tv_relative_file_names): tv_points = solo_calibrated_points_tv[ tv_calibration_file_names.index(fname_tv)] rgb_points = solo_calibrated_points_rgb[ rgb_calibration_file_names.index(fname_rgb)] # rgb_points = calib.get_image_points(fname_rgb, inner_width, inner_height) # tv_points = calib.get_image_points(fname_tv, inner_width, # inner_height) if (rgb_points is not None and tv_points is not None): img_points_rgb.append(rgb_points) img_points_tv.append(tv_points) if len(img_points_rgb) == 0: print("Considered photos can't be used to determine relative position") return None retval, cameraMatrix1, distCoeffs1, cameraMatrix2, \ distCoeffs2, R, T, E, F = calib.calibrate_rgb_and_tv( objpoints, img_points_rgb, img_points_tv, image_size, mtx_rgb, dist_rgb, mtx_tv, dist_tv) T *= chessboard_cell_width_meters # now translations is in meters A = np.append(np.append(R, T, axis=1), np.array([[0, 0, 0, 1]]), axis=0) return A, cameraMatrix1, distCoeffs1, cameraMatrix2, distCoeffs2
def lane_finder(args, thresholds, persp_params): image_logger = ImageLogger(args.output_dir) if not os.path.isfile('{}.npz'.format(args.cam_calib_filename)): mtx, dist_coeff = calibrate_camera(args.is_test, args.camera_img_dir, image_logger) np.savez_compressed(args.cam_calib_filename, mtx=mtx, coeff=dist_coeff) else: print('Reading calibration data from file') cam_data = np.load('{}.npz'.format(args.cam_calib_filename)) mtx = cam_data['mtx'] dist_coeff = cam_data['coeff'] M, Minv = get_perspective_matrix(persp_params['corners_src'], persp_params['corners_dst']) pipeline = LaneFinderPipeline(args, mtx, dist_coeff, image_logger, thresholds, M, Minv) if args.is_test: test_image = load_image(os.path.join(args.test_dir, 'test2.jpg')) result = pipeline.execute(test_image) image_logger.plot_results([[{ 'title': 'Pipelined Img', 'data': result }]]) image_logger.save_image(result, 'binary_combo') else: print('Processing video...') clip2 = VideoFileClip(args.video_filename) vid_clip = clip2.fl_image(pipeline.execute) vid_clip.write_videofile(args.video_output_filename, audio=False)
def __init__(self): # initialize all the default values needed self.pers_mat = None self.inv_pers_mat = None self.processed_frames = 0 self.lane_detected = False self.prev_overlay = None self.prev_left_rad = None self.prev_right_rad = None self.left_fit_list = [] self.right_fit_list = [] self.left_fit_conf_list = [] self.right_fit_conf_list = [] self.offset_list = [] self.prev_conf_left_fit = None self.prev_conf_right_fit = None self.current_roi_src = [] self.current_roi_dest = [] self.conf_left_fit = None self.conf_right_fit = None self.total_confidence = 1 self.lane_distances = {} self.lane_weights = {} # calibrate the camera and get the matrix and distortion co-efficients self.mtx, self.dist = calibration.calibrate_camera() src, dest = self.__get_default_roi() self.__load_perspective_matrix(src, dest)
def main(): global obj global camera_matrix args = defineFlags() camera_matrix = calibration.calibrate_camera() print(camera_matrix) if not args.obj: raise Exception('Require .obj file path') obj = OBJ(args.obj, swapyz=True) if args.video: output_video = 'pygame_video_output.mp4' output_clip = mpy.VideoClip(make_frame, duration=10) # 10 seconds output_clip.write_videofile(output_video, audio=False, fps=24) else: game()
def camera_calib(loc="camera_cal/calibration*.jpg", camera_calib_data_file='cam_calib.p'): # Remove Lens Destortion 1 time per camera only no need to do again and again calib_images = glob.glob(loc) # get object and image points by calibration images objpoints, imgpoints = cal.calibrate_using_checkerd_images(calib_images) index = 0 for cimg in calib_images: img = plt.imread(cimg) img_size = (img.shape[1], img.shape[0]) if (len(objpoints) > 0 and len(imgpoints) > 0): cal.save_calibration(objpoints, imgpoints, img_size, camera_calib_data_file) # Calibrate Camera uimg = cal.calibrate_camera(img, objpoints, imgpoints) # plt.imshow(uming) plt.imsave('output_images/output_camera_calib_{}'.format(index), uimg) index += 1
def __init__(self): super(LineDetector, self).__init__() # STEP 1: CAMERA CALIBRATION self.mtx, self.dist = calibrate_camera("camera_cal") self.previous_left_position = [] self.previous_right_position = [] # Last windows to search lines self.left_area = None self.right_area = None self.areas_set = False self.previous_left_curverad = None self.previous_right_curverad = None self.left_fit_cr = [] self.right_fit_cr = [] self.nwindows = 9 self.inc = 0 # Variables for lines detection # Set the width of the windows +/- margin and set minimum number of pixels found to # recenter window self.margin, self.minpix = 150, 400
square_size = dset_calib.attrs['square_size'] # units = dset_calib.attrs['units'] # 0(b). Create destination hdf5 file and its internal structure. print('Creating destination hdf5 file') hdf5_output_filename = input_dataset_name + '-PRO' + '.hdf5' hdf5_complete_filename_and_path = os.path.join('C:/Users/Samantha/Documents/Facultad/Laboratorio_6/Mediciones/', hdf5_output_filename) hdf5_output_file = h5py.File(hdf5_complete_filename_and_path, 'w') # cal_results_dset = hdf5_output_file.create_dataset('calibration_results') height_grp = hdf5_output_file.create_group('height_fields') # rectangle_dset = hdf5_output_file.create_dataset('height_fields/rectangle') # FTP PROCESSING # 1. Calibrate camera. print('Calibrating camera') cam_mtx, roi, mapx, mapy = calibrate_camera(dset_calib, [8, 6], square_size) # 2. Generate one gray image by averaging gray images. print('Generate averages gray and reference, and generate mask') gray = generate_average_image(dset_ftp_g) # 3. Generate one reference image by averaging references. ref = generate_average_image(dset_ftp_r) # 4. Undistort gray image. # gray = undistort_image(gray, mapx, mapy) # 5. From gray image, determine mask and disk and rectangle properties mask, c_disk, R_disk, c_rect, sl_rect, mask_of_disk_alone, mask_of_rect_alone = generate_mask(gray) N_vertical_slices = int(N_images/100) + (1 - (N_images/100).is_integer() )
for point in src: axarray[0].plot(*point, '.') axarray[1].set_title('After perspective transform') axarray[1].imshow(warped, cmap='gray') for point in dst: axarray[1].plot(*point, '.') for axis in axarray: axis.set_axis_off() plt.show() return warped, M, Minv if __name__ == '__main__': ret, mtx, dist, rvecs, tvecs = calibrate_camera( calib_images_dir='camera_cal') # show result on test images for test_img in glob.glob('test_images/*.jpg'): img = cv2.imread(test_img) undistorted_img = undistort(img, mtx, dist, testing=False) binary_img = binarize(undistorted_img, testing=False) birdeye_img, M, Minv = birdeye(cv2.cvtColor(undistorted_img, cv2.COLOR_BGR2RGB), testing=True)
func = sys.argv[1].lower() if func == "capture": direc = sys.argv[2] if len(sys.argv) > 2 else IMAGE_DIR images = capture_images(direc) elif func == "calib": direc = sys.argv[2] if len(sys.argv) > 2 else IMAGE_DIR images = read_images(direc) else: print("invalid function") exit(1) if len(images) == 0: print("no images captured or read") exit(1) # get grid data objpoints, imgpoints = collect_calib_data(images) if len(objpoints) == 0: print("No grids found in images") exit(1) # calibrate mtx, dist, rvecs, tvecs = calibration.calibrate_camera(objpoints, imgpoints, (images[0].shape[1::-1])) calibf = sys.argv[3] if len(sys.argv) > 3 else "camera_calib.json" print("saving calibration data to %s" % calibf) calibration.dump_calibration(calibf, mtx, dist, rvecs, tvecs)
output = clip.fl_image(pipeline) output.write_videofile(output_name, audio=False) def image_preview(file_name): image = mpimg.imread(file_name) warped = proc.warp(image) detector.lane_width = 800 # bypass lane width estimation stage detector.detect_lane(warped, debug=True) image = detector.draw_final_lane(image) plt.figure() plt.imshow(image) plt.show() if __name__ == '__main__': # calibrate the camera if not already done so data_file = './calibration_data.p' if not os.path.exists(data_file): mtx, dist = calibration.calibrate_camera('./camera_cal', 9, 6) calibration.save_data(data_file, mtx, dist) # load calibration data mtx, dist = calibration.load_data(data_file) #utils.plot_all('./test_images', calibration.undistort_image, mtx, dist) detector = detector.Detector() image_preview('./test_images/test4.jpg') #video_preview('project_video.mp4', pipeline) #video_output('challenge_video.mp4', pipeline)