Exemple #1
0
    def create_calibration_directory(self, cam_ids, IdMat, points, Res, cam_calibrations=[], cam_centers=[], radial_distortion=0, square_pixels=1, num_cameras_fill=-1):
        assert len(Res) == len(cam_ids)
        if len(cam_calibrations): assert len(cam_ids) == len(cam_calibrations)
        if len(cam_centers): assert len(cam_ids) == len(cam_centers)

        LOG.debug("points.shape %r" % (points.shape,))
        LOG.debug('IdMat.shape %r' % (IdMat.shape,))
        LOG.debug('Res %r' % (Res,))

        self._write_cam_ids(cam_ids)

        if len(cam_calibrations):
            for i,url in enumerate(cam_calibrations):
                assert os.path.isfile(url)
                #i+1 because mcsc expects matlab numbering...
                dest = "%s/%s%d.rad" % (self.out_dirname, self.basename, i+1)
                if url.endswith('.yaml'):
                    camera_calibration_yaml_to_radfile(
                        url,
                        dest)
                elif url.endswith('.rad'):
                    shutil.copy(url,dest)
                else:
                    raise Exception("Calibration format %s not supported" % url)

                LOG.debug('wrote cam calibration file %s' % dest)

        if len(cam_centers):
            save_ascii_matrix(cam_centers,os.path.join(self.out_dirname,'original_cam_centers.dat'))

        save_ascii_matrix(Res, os.path.join(self.out_dirname,'Res.dat'), isint=True)
        save_ascii_matrix(IdMat, os.path.join(self.out_dirname,'IdMat.dat'), isint=True)
        save_ascii_matrix(points, os.path.join(self.out_dirname,'points.dat'))

        self._write_cfg(cam_ids, radial_distortion, square_pixels, num_cameras_fill, cam_centers)
Exemple #2
0
    def create_calibration_directory(self,
                                     cam_ids,
                                     IdMat,
                                     points,
                                     Res,
                                     cam_calibrations=[],
                                     cam_centers=[],
                                     radial_distortion=0,
                                     square_pixels=1,
                                     num_cameras_fill=-1):
        assert len(Res) == len(cam_ids)
        if len(cam_calibrations): assert len(cam_ids) == len(cam_calibrations)
        if len(cam_centers): assert len(cam_ids) == len(cam_centers)

        LOG.debug("points.shape %r" % (points.shape, ))
        LOG.debug('IdMat.shape %r' % (IdMat.shape, ))
        LOG.debug('Res %r' % (Res, ))

        self._write_cam_ids(cam_ids)

        if len(cam_calibrations):
            for i, url in enumerate(cam_calibrations):
                assert os.path.isfile(url)
                #i+1 because mcsc expects matlab numbering...
                dest = "%s/%s%d.rad" % (self.out_dirname, self.basename, i + 1)
                if url.endswith('.yaml'):
                    camera_calibration_yaml_to_radfile(url, dest)
                elif url.endswith('.rad'):
                    shutil.copy(url, dest)
                else:
                    raise Exception("Calibration format %s not supported" %
                                    url)

                LOG.debug('wrote cam calibration file %s' % dest)

        if len(cam_centers):
            save_ascii_matrix(
                cam_centers,
                os.path.join(self.out_dirname, 'original_cam_centers.dat'))

        save_ascii_matrix(Res,
                          os.path.join(self.out_dirname, 'Res.dat'),
                          isint=True)
        save_ascii_matrix(IdMat,
                          os.path.join(self.out_dirname, 'IdMat.dat'),
                          isint=True)
        save_ascii_matrix(points, os.path.join(self.out_dirname, 'points.dat'))

        self._write_cfg(cam_ids, radial_distortion, square_pixels,
                        num_cameras_fill, cam_centers)
    def create_from_cams(
        self, cam_ids=[], cam_resolutions={}, cam_points={}, cam_calibrations={}, num_cameras_fill=-1, **kwargs
    ):
        # num_cameras_fill = -1 means use all cameras (= len(cam_ids))

        if not cam_ids:
            cam_ids = cam_points.keys()

        # remove cameras with no points
        cams_to_remove = []
        for cam in cam_ids:
            nvalid = np.count_nonzero(np.nan_to_num(np.array(cam_points[cam])))
            if nvalid == 0:
                cams_to_remove.append(cam)
                LOG.warn("removing cam %s - no points detected" % cam)
        map(cam_ids.remove, cams_to_remove)

        self._write_cam_ids(cam_ids)

        resfd = open(os.path.join(self.out_dirname, "Res.dat"), "w")
        foundfd = open(os.path.join(self.out_dirname, "IdMat.dat"), "w")
        pointsfd = open(os.path.join(self.out_dirname, "points.dat"), "w")

        for i, cam in enumerate(cam_ids):
            points = np.array(cam_points[cam])
            assert points.shape[1] == 2
            npts = points.shape[0]

            # add colum of 1s (homogenous coords, Z)
            points = np.hstack((points, np.ones((npts, 1))))
            # multicamselfcal expects points rowwise (as multiple cams per file)
            points = points.T

            # detected points are those non-nan (just choose one axis, there is no
            # possible scenario where one axis is a valid coord and the other is nan
            # in my feature detection scheme
            found = points[0, :]
            # replace nans with 0 and numbers with 1
            found = np.nan_to_num(found).clip(max=1)

            res = np.array(cam_resolutions[cam])

            save_ascii_matrix(res.reshape((1, 2)), resfd, isint=True)
            save_ascii_matrix(found.reshape((1, npts)), foundfd, isint=True)
            save_ascii_matrix(points, pointsfd)

            # write camera rad files if supplied
            if cam in cam_calibrations:
                url = cam_calibrations[cam]
                assert os.path.isfile(url)
                # i+1 because mcsc expects matlab numbering...
                dest = "%s/%s%d.rad" % (self.out_dirname, self.basename, i + 1)
                if url.endswith(".yaml"):
                    camera_calibration_yaml_to_radfile(url, dest)
                elif url.endswith(".rad"):
                    shutil.copy(url, dest)
                else:
                    raise Exception("Calibration format %s not supported" % url)

        resfd.close()
        foundfd.close()
        pointsfd.close()

        undo_radial = all([cam in cam_calibrations for cam in cam_ids])
        self._write_cfg(cam_ids, undo_radial, True, num_cameras_fill, [])
        LOG.debug("dropped cams: %s" % ",".join(cams_to_remove))
Exemple #4
0
    def create_from_cams(self,
                         cam_ids=[],
                         cam_resolutions={},
                         cam_points={},
                         cam_calibrations={},
                         num_cameras_fill=-1,
                         **kwargs):
        #num_cameras_fill = -1 means use all cameras (= len(cam_ids))

        if not cam_ids:
            cam_ids = cam_points.keys()

        #remove cameras with no points
        cams_to_remove = []
        for cam in cam_ids:
            nvalid = np.count_nonzero(np.nan_to_num(np.array(cam_points[cam])))
            if nvalid == 0:
                cams_to_remove.append(cam)
                LOG.warn("removing cam %s - no points detected" % cam)
        map(cam_ids.remove, cams_to_remove)

        self._write_cam_ids(cam_ids)

        resfd = open(os.path.join(self.out_dirname, 'Res.dat'), 'w')
        foundfd = open(os.path.join(self.out_dirname, 'IdMat.dat'), 'w')
        pointsfd = open(os.path.join(self.out_dirname, 'points.dat'), 'w')

        for i, cam in enumerate(cam_ids):
            points = np.array(cam_points[cam])
            assert points.shape[1] == 2
            npts = points.shape[0]

            #add colum of 1s (homogenous coords, Z)
            points = np.hstack((points, np.ones((npts, 1))))
            #multicamselfcal expects points rowwise (as multiple cams per file)
            points = points.T

            #detected points are those non-nan (just choose one axis, there is no
            #possible scenario where one axis is a valid coord and the other is nan
            #in my feature detection scheme
            found = points[0, :]
            #replace nans with 0 and numbers with 1
            found = np.nan_to_num(found).clip(max=1)

            res = np.array(cam_resolutions[cam])

            save_ascii_matrix(res.reshape((1, 2)), resfd, isint=True)
            save_ascii_matrix(found.reshape((1, npts)), foundfd, isint=True)
            save_ascii_matrix(points, pointsfd)

            #write camera rad files if supplied
            if cam in cam_calibrations:
                url = cam_calibrations[cam]
                assert os.path.isfile(url)
                #i+1 because mcsc expects matlab numbering...
                dest = "%s/%s%d.rad" % (self.out_dirname, self.basename, i + 1)
                if url.endswith('.yaml'):
                    camera_calibration_yaml_to_radfile(url, dest)
                elif url.endswith('.rad'):
                    shutil.copy(url, dest)
                else:
                    raise Exception("Calibration format %s not supported" %
                                    url)

        resfd.close()
        foundfd.close()
        pointsfd.close()

        undo_radial = all([cam in cam_calibrations for cam in cam_ids])
        self._write_cfg(cam_ids, undo_radial, True, num_cameras_fill, [])
        LOG.debug("dropped cams: %s" % ','.join(cams_to_remove))