Example #1
0
def check_mcsc_roundtrip(with_rad_files=False,align_existing=False):
    np.random.seed(3)
    mcscdir = os.path.join( os.path.dirname(mcsce.__file__),
                            '..', '..', 'MultiCamSelfCal' )
    mcscdir = os.path.abspath(mcscdir)
    out_dirname = tempfile.mkdtemp()
    try:
        mcsc = mcsce.MultiCamSelfCal(out_dirname=out_dirname,mcscdir=mcscdir)
        if with_rad_files:
            orig_cams = make_default_system(with_separate_distorions=True)
        else:
            orig_cams = make_default_system()

        cam_resolutions = dict(  (n['name'],  (n['width'],n['height']))
                               for n in orig_cams.to_dict()['camera_system'])

        pts_3d = get_default_points()

        if 0:
            import matplotlib.pyplot as plt
            from mpl_toolkits.mplot3d import Axes3D

            from pymvg.plot_utils import plot_camera
            np.set_printoptions(precision=3, suppress=True)
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')
            for name,cam in orig_cams.get_camera_dict().iteritems():
                print()
                print(name,'-'*80)
                print('  center:',cam.get_camcenter())
                print('  lookat:',cam.get_lookat())
                print('  up:',cam.get_up())
                print('  P')
                print(cam.P)
                print()
                plot_camera( ax, cam)#, scale = dim/5.0 )
            ax.plot( pts_3d[:,0], pts_3d[:,1], pts_3d[:,2], 'k.')
            plt.show()

        cam_points=defaultdict(list)

        nan_tup = (np.nan, np.nan)
        for pt_3d in pts_3d:
            valid = True
            for name in orig_cams.get_names():
                w,h = cam_resolutions[name]

                x,y = orig_cams.find2d( name, pt_3d )

                if 1:
                    # add noise to the 2D images
                    xn, yn = 0.01*np.random.randn( 2 )

                    x += xn
                    y += yn

                if (0 <= x) and (x < w) and (0 <= y) and (y < h):
                    cam_points[name].append((x,y))
                else:
                    cam_points[name].append(nan_tup)
                    valid = False
            if not valid:
                for name in orig_cams.get_names():
                    cam_points[name].pop()

        cam_ids=orig_cams.get_names()
        if with_rad_files:
            cam_calibrations = {} # dictionary of .yaml filenames with ROS yaml format
            raise NotImplementedError
        else:
            cam_calibrations = {}

        if align_existing:
            cam_centers = np.array([ orig_cams.get_cam(n).get_camcenter() for n in orig_cams.get_names() ]).T
        else:
            cam_centers = []

        mcsc.create_from_cams(cam_ids=cam_ids,
                              cam_resolutions=cam_resolutions,
                              cam_points=cam_points,
                              cam_calibrations=cam_calibrations,
                              cam_centers=cam_centers,
                              )

        result = mcsc.execute(silent=True)
        raw_cams = MultiCameraSystem.from_mcsc( result )
        if align_existing:
            aligned_cams = raw_cams
        else:
            aligned_cams = raw_cams.get_aligned_copy( orig_cams )

        assert is_close(orig_cams,aligned_cams,pts_3d)
    finally:
        shutil.rmtree(out_dirname)
Example #2
0
def test_load_mcsc():
    mydir = os.path.dirname(__file__)
    mcsc_dirname = os.path.join(mydir,'mcsc_output_20130726')
    cam_system = MultiCameraSystem.from_mcsc( mcsc_dirname )
Example #3
0
def check_mcsc_roundtrip(with_rad_files=False, align_existing=False):
    np.random.seed(3)
    mcscdir = os.path.join(os.path.dirname(mcsce.__file__), '..', '..',
                           'MultiCamSelfCal')
    mcscdir = os.path.abspath(mcscdir)
    out_dirname = tempfile.mkdtemp()
    try:
        mcsc = mcsce.MultiCamSelfCal(out_dirname=out_dirname, mcscdir=mcscdir)
        if with_rad_files:
            orig_cams = make_default_system(with_separate_distorions=True)
        else:
            orig_cams = make_default_system()

        cam_resolutions = dict((n['name'], (n['width'], n['height']))
                               for n in orig_cams.to_dict()['camera_system'])

        pts_3d = get_default_points()

        if 0:
            import matplotlib.pyplot as plt
            from mpl_toolkits.mplot3d import Axes3D

            from pymvg.plot_utils import plot_camera
            np.set_printoptions(precision=3, suppress=True)
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')
            for name, cam in orig_cams.get_camera_dict().iteritems():
                print()
                print(name, '-' * 80)
                print('  center:', cam.get_camcenter())
                print('  lookat:', cam.get_lookat())
                print('  up:', cam.get_up())
                print('  P')
                print(cam.P)
                print()
                plot_camera(ax, cam)  #, scale = dim/5.0 )
            ax.plot(pts_3d[:, 0], pts_3d[:, 1], pts_3d[:, 2], 'k.')
            plt.show()

        cam_points = defaultdict(list)

        nan_tup = (np.nan, np.nan)
        for pt_3d in pts_3d:
            valid = True
            for name in orig_cams.get_names():
                w, h = cam_resolutions[name]

                x, y = orig_cams.find2d(name, pt_3d)

                if 1:
                    # add noise to the 2D images
                    xn, yn = 0.01 * np.random.randn(2)

                    x += xn
                    y += yn

                if (0 <= x) and (x < w) and (0 <= y) and (y < h):
                    cam_points[name].append((x, y))
                else:
                    cam_points[name].append(nan_tup)
                    valid = False
            if not valid:
                for name in orig_cams.get_names():
                    cam_points[name].pop()

        cam_ids = orig_cams.get_names()
        if with_rad_files:
            cam_calibrations = {
            }  # dictionary of .yaml filenames with ROS yaml format
            raise NotImplementedError
        else:
            cam_calibrations = {}

        if align_existing:
            cam_centers = np.array([
                orig_cams.get_cam(n).get_camcenter()
                for n in orig_cams.get_names()
            ]).T
        else:
            cam_centers = []

        mcsc.create_from_cams(
            cam_ids=cam_ids,
            cam_resolutions=cam_resolutions,
            cam_points=cam_points,
            cam_calibrations=cam_calibrations,
            cam_centers=cam_centers,
        )

        result = mcsc.execute(silent=True)
        raw_cams = MultiCameraSystem.from_mcsc(result)
        if align_existing:
            aligned_cams = raw_cams
        else:
            aligned_cams = raw_cams.get_aligned_copy(orig_cams)

        assert is_close(orig_cams, aligned_cams, pts_3d)
    finally:
        shutil.rmtree(out_dirname)
Example #4
0
def test_load_mcsc():
    mydir = os.path.dirname(__file__)
    mcsc_dir = os.path.join(mydir, 'external', 'mcsc')
    mcsc_dirname = os.path.join(mcsc_dir, 'mcsc_output_20130726')
    cam_system = MultiCameraSystem.from_mcsc(mcsc_dirname)
Example #5
0
def test_pymvg_roundtrip():
    from pymvg.camera_model import CameraModel
    from pymvg.multi_camera_system import MultiCameraSystem
    from flydra_core.reconstruct import Reconstructor

    # ----------- with no distortion ------------------------
    center1 = np.array( (0, 0.0, 5) )
    center2 = np.array( (1, 0.0, 5) )

    lookat = np.array( (0,1,0))

    cam1 = CameraModel.load_camera_simple(fov_x_degrees=90,
                                          name='cam1',
                                          eye=center1,
                                          lookat=lookat)
    cam2 = CameraModel.load_camera_simple(fov_x_degrees=90,
                                          name='cam2',
                                          eye=center2,
                                          lookat=lookat)
    mvg = MultiCameraSystem( cameras=[cam1, cam2] )
    R = Reconstructor.from_pymvg(mvg)
    mvg2 = R.convert_to_pymvg()

    cam_ids = ['cam1','cam2']
    for distorted in [True,False]:
        for cam_id in cam_ids:
            v1 = mvg.find2d(  cam_id, lookat, distorted=distorted )
            v2 = R.find2d(    cam_id, lookat, distorted=distorted )
            v3 = mvg2.find2d( cam_id, lookat, distorted=distorted )
            assert np.allclose(v1,v2)
            assert np.allclose(v1,v3)

    # ----------- with distortion ------------------------
    cam1dd = cam1.to_dict()
    cam1dd['D'] = (0.1, 0.2, 0.3, 0.4, 0.0)
    cam1d = CameraModel.from_dict(cam1dd)

    cam2dd = cam2.to_dict()
    cam2dd['D'] = (0.11, 0.21, 0.31, 0.41, 0.0)
    cam2d = CameraModel.from_dict(cam2dd)

    mvgd = MultiCameraSystem( cameras=[cam1d, cam2d] )
    Rd = Reconstructor.from_pymvg(mvgd)
    mvg2d = Rd.convert_to_pymvg()
    cam_ids = ['cam1','cam2']
    for distorted in [True,False]:
        for cam_id in cam_ids:
            v1 = mvgd.find2d(  cam_id, lookat, distorted=distorted )
            v2 = Rd.find2d(    cam_id, lookat, distorted=distorted )
            v3 = mvg2d.find2d( cam_id, lookat, distorted=distorted )
            assert np.allclose(v1,v2)
            assert np.allclose(v1,v3)

    # ------------ with distortion at different focal length ------
    mydir = os.path.dirname(__file__)
    caldir = os.path.join(mydir,'sample_calibration')
    print mydir
    print caldir
    R3 = Reconstructor(caldir)
    mvg3 = R3.convert_to_pymvg()
    #R4 = Reconstructor.from_pymvg(mvg3)
    mvg3b = MultiCameraSystem.from_mcsc( caldir )

    for distorted in [True,False]:
        for cam_id in R3.cam_ids:
            v1 = R3.find2d(   cam_id, lookat, distorted=distorted )
            v2 = mvg3.find2d( cam_id, lookat, distorted=distorted )
            #v3 = R4.find2d(   cam_id, lookat, distorted=distorted )
            v4 = mvg3b.find2d( cam_id, lookat, distorted=distorted )
            assert np.allclose(v1,v2)
            #assert np.allclose(v1,v3)
            assert np.allclose(v1,v4)