def get_optimal_gphoto2_homography(path, delay_guess=1, save=True, recalculate=False):
    config = read_hdf5_file_to_pandas.load_config_from_path(path)
    s = config.identifiercode + '_' + 'gphoto2'
    gphoto2directory = os.path.join(config.path, s)
    fname = os.path.join(gphoto2directory, 'tracker_to_gphoto2_homography_matrix.pickle')

    if not os.path.exists(fname) or recalculate:
        data_filename = read_hdf5_file_to_pandas.get_filename(path, 'trackedobjects.hdf5')
        tracking_pd_frame, config = read_hdf5_file_to_pandas.load_and_preprocess_data(data_filename)
        flyimg_dict = find_flies_in_image_directory.load_flyimg_dict_from_path(path)
        unique_flyimg_dict = __compile_unique_flyimg_dict(flyimg_dict)

        delay_opt = __find_optimal_delay(unique_flyimg_dict, tracking_pd_frame, delay=delay_guess)
        points_2d_tracker, points_2d_gphoto2 = __find_homologous_flies(unique_flyimg_dict, tracking_pd_frame, delay=delay_opt)
        Hm = __calc_homography_from_tracker_to_gphoto2(points_2d_tracker, points_2d_gphoto2)
        results = {'tracker_to_gphoto2_homography': Hm, 'delay_optimal': delay_opt}
        f = open(fname, 'w')
        pickle.dump(results, f)
        f.close()
    else:
        f = open(fname)
        results = pickle.load(f)
        f.close()
        Hm = results['tracker_to_gphoto2_homography']
        delay_opt = results['delay_optimal']

    return Hm, delay_opt
def __test(delay=0):

    path = '/home/riffelluser/demo/demo_1/gphoto_test_2'

    data_filename = read_hdf5_file_to_pandas.get_filename(path, 'trackedobjects.hdf5')
    tracking_pd_frame, config = read_hdf5_file_to_pandas.load_and_preprocess_data(data_filename)

    gphoto2_flies = load_gphoto2_flies(path)


    unique_gphoto2_flies = __choose_frames_with_a_single_fly(gphoto2_flies)
    points_2d_tracker, points_2d_gphoto2 = __find_homologous_flies(unique_gphoto2_flies, tracking_pd_frame, delay=delay)

    Hm = __calc_homography_from_tracker_to_gphoto2(points_2d_tracker, points_2d_gphoto2)

    points_2d_reprojected = reproject_tracker_point_onto_gphoto2(points_2d_tracker, Hm)

    print Hm
    print points_2d_reprojected
    print
    print np.linalg.norm( np.array(points_2d_gphoto2) - np.array(points_2d_reprojected))

    __plot_reprojected_points(points_2d_tracker, points_2d_gphoto2, Hm)

    __plot_reprojected_points(points_2d_gphoto2, points_2d_tracker, numpy.linalg.inv(Hm))
Beispiel #3
0
def find_best_model_for_outliers(roi_directory,
                                 difference_threshold=DIFFERENCE_THRESHOLD):
    outlier_directory = mta_read.get_filename(roi_directory, 'model_outliers')
    outlier_filenames = mta_read.get_filenames(outlier_directory, '.jpg')
    for outlier_filename in outlier_filenames:
        assign_fly_to_model_and_update_models_from_filenames(
            roi_directory,
            outlier_filename,
            difference_threshold=difference_threshold)
        os.remove(outlier_filename)
def load_2d_points(path):
    config = read_hdf5_file_to_pandas.load_config_from_path(path)
    s = config.identifiercode + '_' + 'gphoto2'
    gphoto2directory = os.path.join(config.path, s)
    fname = os.path.join(gphoto2directory, 'tracker_to_gphoto2_homography_matrix.pickle')

    data_filename = read_hdf5_file_to_pandas.get_filename(path, 'trackedobjects.hdf5')
    tracking_pd_frame, config = read_hdf5_file_to_pandas.load_and_preprocess_data(data_filename)
    flyimg_dict = find_flies_in_image_directory.load_flyimg_dict_from_path(path)
    unique_flyimg_dict = __compile_unique_flyimg_dict(flyimg_dict)

    Hm, delay_opt = get_optimal_gphoto2_homography(path, delay_guess=1, save=True, recalculate=False)
    points_2d_tracker, points_2d_gphoto2 = __find_homologous_flies(unique_flyimg_dict, tracking_pd_frame, delay=delay_opt)
    return points_2d_tracker, points_2d_gphoto2
    print np.linalg.norm( np.array(points_2d_gphoto2) - np.array(points_2d_reprojected))

    __plot_reprojected_points(points_2d_tracker, points_2d_gphoto2, Hm)

    __plot_reprojected_points(points_2d_gphoto2, points_2d_tracker, numpy.linalg.inv(Hm))

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option('--path', type=str, default='none', help="option: path that points to standard named filename, background image, dvbag, config. If using 'path', no need to provide filename, bgimg, dvbag, and config. Note")
    parser.add_option('--delay', type=float, default=1.0, help="option: delay guess for optimizing calibration")    
    (options, args) = parser.parse_args()

    Hm, delay_opt = get_optimal_gphoto2_homography(options.path, delay_guess=options.delay, save=True, recalculate=True)

    path = options.path    
    config = read_hdf5_file_to_pandas.load_config_from_path(path)
    s = config.identifiercode + '_' + 'gphoto2'
    gphoto2directory = os.path.join(config.path, s)
    fname = os.path.join(gphoto2directory, 'tracker_to_gphoto2_homography_matrix.pickle')
    data_filename = read_hdf5_file_to_pandas.get_filename(path, 'trackedobjects.hdf5')
    tracking_pd_frame, config = read_hdf5_file_to_pandas.load_and_preprocess_data(data_filename)
    gphoto2_flies = load_gphoto2_flies(path)
    unique_gphoto2_flies = __choose_frames_with_a_single_fly(gphoto2_flies)
    points_2d_tracker, points_2d_gphoto2 = __find_homologous_flies(unique_gphoto2_flies, tracking_pd_frame, delay=delay_opt)


    __plot_reprojected_points(points_2d_tracker, points_2d_gphoto2, Hm, 'tracker to gphoto2 reprojection')

    __plot_reprojected_points(points_2d_gphoto2, points_2d_tracker, numpy.linalg.inv(Hm), 'gphoto2 to tracker reprojection')

    plt.show()