Esempio n. 1
0
    def launch_calibration(self, method, vdisp ):

        def _pump_ui():
            if os.environ.get('RUNNING_NOSE') != '1':
                Gtk.main_iteration_do(False) #run once, no block
                while Gtk.events_pending():  #run remaining
                    Gtk.main_iteration()

        orig_data = []
        for row in self.point_store:
            if row[VDISP]==vdisp:
                orig_data.append( [ row[TEXU], row[TEXV], row[DISPLAYX], row[DISPLAYY] ] )
        orig_data = np.array(orig_data)
        uv = orig_data[:,:2]
        XYZ = self.geom.model.texcoord2worldcoord(uv)
        xy = orig_data[:,2:4]

        assert method in EXTRINSIC_CALIBRATION_METHODS

        if method in ('DLT','RANSAC DLT'):
            ransac = method.startswith('RANSAC')
            r = dlt.dlt(XYZ, xy, ransac=ransac )
            c1 = CameraModel.load_camera_from_M( r['pmat'],
                                                       width=self.dsc.width,
                                                       height=self.dsc.height,
                                                       )

            _pump_ui()

            if 0:
                c2 = c1.get_flipped_camera()

                # slightly hacky way to find best camera direction
                obj = self.geom.model.get_center()

                d1 = np.sqrt( np.sum( (c1.get_lookat() - obj)**2 ))
                d2 = np.sqrt( np.sum( (c2.get_lookat() - obj)**2 ))
                if d1 < d2:
                    #print 'using normal camera'
                    camera = c1
                else:
                    print 'using flipped camera'
                    camera = c2
            elif 1:
                farr = self.geom.compute_for_camera_view( c1,
                                                          what='texture_coords' )

                _pump_ui()

                u = farr[:,:,0]
                good = ~np.isnan( u )
                npix=np.sum( np.nonzero( good ) )
                if npix==0:
                    print 'using flipped camera, otherwise npix = 0'
                    camera = c1.get_flipped_camera()
                else:
                    camera = c1
            else:
                camera = c1
        elif method in ['extrinsic only','iterative extrinsic only']:
            assert self.display_intrinsic_cam is not None, 'need intrinsic calibration'

            di = self.dsc.get_display_info()

            mirror = None
            if 'virtualDisplays' in di:
                found = False
                for d in di['virtualDisplays']:
                    if d['id'] == vdisp:
                        found = True
                        mirror = d.get('mirror',None)
                        break
                assert found


            if mirror is not None:
                cami = self.display_intrinsic_cam.get_mirror_camera(axis=mirror)
            else:
                cami = self.display_intrinsic_cam

            _pump_ui()

            if method == 'iterative extrinsic only':
                result = fit_extrinsics_iterative(cami,XYZ,xy)
            else:
                result = fit_extrinsics(cami,XYZ,xy)

            _pump_ui()

            c1 = result['cam']
            if 1:
                farr = self.geom.compute_for_camera_view( c1,
                                                          what='texture_coords' )

                _pump_ui()

                u = farr[:,:,0]
                good = ~np.isnan( u )
                npix=np.sum( np.nonzero( good ) )
                if npix==0:
                    print 'using flipped camera, otherwise npix = 0'
                    camera = c1.get_flipped_camera()
                else:
                    camera = c1
            else:
                camera = c1
            del result
        else:
            raise ValueError('unknown calibration method %r'%method)

        _pump_ui()

        projected_points = camera.project_3d_to_pixel( XYZ )
        reproj_error = np.sum( (projected_points - xy)**2, axis=1)
        mre = np.mean(reproj_error)

        for row in self.vdisp_store:
            if row[VS_VDISP]==vdisp:
                row[VS_MRE] = mre
                row[VS_CAMERA_OBJECT] = camera

        _pump_ui()

        self.update_bg_image()
Esempio n. 2
0
def test_ransac_dlt():
    np.random.seed(3) # try to prevent following from failing occasionally
    results = dlt.dlt(XYZ, xy, ransac=True)
    assert results['mean_reprojection_error'] < 5.0
Esempio n. 3
0
    def launch_calibration(self, method, vdisp):
        def _pump_ui():
            if os.environ.get('RUNNING_NOSE') != '1':
                Gtk.main_iteration_do(False)  #run once, no block
                while Gtk.events_pending():  #run remaining
                    Gtk.main_iteration()

        orig_data = []
        for row in self.point_store:
            if row[VDISP] == vdisp:
                orig_data.append(
                    [row[TEXU], row[TEXV], row[DISPLAYX], row[DISPLAYY]])
        orig_data = np.array(orig_data)
        uv = orig_data[:, :2]
        XYZ = self.geom.model.texcoord2worldcoord(uv)
        xy = orig_data[:, 2:4]

        assert method in EXTRINSIC_CALIBRATION_METHODS

        if method in ('DLT', 'RANSAC DLT'):
            ransac = method.startswith('RANSAC')
            r = dlt.dlt(XYZ, xy, ransac=ransac)
            c1 = CameraModel.load_camera_from_M(
                r['pmat'],
                width=self.dsc.width,
                height=self.dsc.height,
            )

            _pump_ui()

            if 0:
                c2 = c1.get_flipped_camera()

                # slightly hacky way to find best camera direction
                obj = self.geom.model.get_center()

                d1 = np.sqrt(np.sum((c1.get_lookat() - obj)**2))
                d2 = np.sqrt(np.sum((c2.get_lookat() - obj)**2))
                if d1 < d2:
                    #print 'using normal camera'
                    camera = c1
                else:
                    print 'using flipped camera'
                    camera = c2
            elif 1:
                farr = self.geom.compute_for_camera_view(c1,
                                                         what='texture_coords')

                _pump_ui()

                u = farr[:, :, 0]
                good = ~np.isnan(u)
                npix = np.sum(np.nonzero(good))
                if npix == 0:
                    print 'using flipped camera, otherwise npix = 0'
                    camera = c1.get_flipped_camera()
                else:
                    camera = c1
            else:
                camera = c1
        elif method in ['extrinsic only', 'iterative extrinsic only']:
            assert self.display_intrinsic_cam is not None, 'need intrinsic calibration'

            di = self.dsc.get_display_info()

            mirror = None
            if 'virtualDisplays' in di:
                found = False
                for d in di['virtualDisplays']:
                    if d['id'] == vdisp:
                        found = True
                        mirror = d.get('mirror', None)
                        break
                assert found

            if mirror is not None:
                cami = self.display_intrinsic_cam.get_mirror_camera(
                    axis=mirror)
            else:
                cami = self.display_intrinsic_cam

            _pump_ui()

            if method == 'iterative extrinsic only':
                result = fit_extrinsics_iterative(cami, XYZ, xy)
            else:
                result = fit_extrinsics(cami, XYZ, xy)

            _pump_ui()

            c1 = result['cam']
            if 1:
                farr = self.geom.compute_for_camera_view(c1,
                                                         what='texture_coords')

                _pump_ui()

                u = farr[:, :, 0]
                good = ~np.isnan(u)
                npix = np.sum(np.nonzero(good))
                if npix == 0:
                    print 'using flipped camera, otherwise npix = 0'
                    camera = c1.get_flipped_camera()
                else:
                    camera = c1
            else:
                camera = c1
            del result
        else:
            raise ValueError('unknown calibration method %r' % method)

        _pump_ui()

        projected_points = camera.project_3d_to_pixel(XYZ)
        reproj_error = np.sum((projected_points - xy)**2, axis=1)
        mre = np.mean(reproj_error)

        for row in self.vdisp_store:
            if row[VS_VDISP] == vdisp:
                row[VS_MRE] = mre
                row[VS_CAMERA_OBJECT] = camera

        _pump_ui()

        self.update_bg_image()
Esempio n. 4
0
def test_basic_dlt():
    results = dlt.dlt(XYZ, xy, ransac=False)
    assert results['mean_reprojection_error'] < 6.0
    c1 = CameraModel.load_camera_from_M(  results['pmat']  )