Beispiel #1
0
    def _new(self, center, rotation, intrinsics):
        cam_new = self.VITAL_LIB['vital_camera_new']
        cam_new.argtypes = [
            EigenArray.c_ptr_type(3, 1, ctypes.c_double),
            Rotation.c_ptr_type(ctypes.c_double),
            CameraIntrinsics.c_ptr_type(),
            VitalErrorHandle.c_ptr_type()
        ]
        cam_new.restype = self.c_ptr_type()

        # Fill in parameter gaps
        if center is None:
            center = EigenArray(3)
            center[:] = 0
        else:
            center = EigenArray.from_iterable(center)

        if rotation is None:
            rotation = Rotation()

        if intrinsics is None:
            intrinsics = CameraIntrinsics()

        with VitalErrorHandle() as eh:
            return cam_new(center, rotation, intrinsics, eh)
Beispiel #2
0
    def test_equal(self):
        ci1 = CameraIntrinsics()
        ci2 = CameraIntrinsics()
        ntools.assert_true(ci1 == ci2)
        ntools.assert_false(ci1 != ci2)

        ci1 = CameraIntrinsics(2, (10, 10), 3, 1)
        ci2 = CameraIntrinsics(2, (11, 10), 3, 0.1)
        ntools.assert_false(ci1 == ci2)
        ntools.assert_true(ci1 != ci2)
Beispiel #3
0
 def intrinsics(self):
     """
     :return: a reference to this camera's intrinsics object
     :rtype: CameraIntrinsics
     """
     cam_int = self.VITAL_LIB['vital_camera_intrinsics']
     cam_int.argtypes = [self.c_ptr_type(), VitalErrorHandle.c_ptr_type()]
     cam_int.restype = CameraIntrinsics.c_ptr_type()
     with VitalErrorHandle() as eh:
         c_ptr = cam_int(self, eh)
     return CameraIntrinsics(from_cptr=c_ptr)
Beispiel #4
0
    def test_new(self):
        # just seeing that basic construction doesn't blow up
        cam = Camera()

        c = EigenArray(3)
        r = Rotation()
        ci = CameraIntrinsics()
        cam = Camera(c, r, ci)
Beispiel #5
0
 def intrinsics(self):
     """
     :return: a reference to this camera's intrinsics object
     :rtype: CameraIntrinsics
     """
     cam_int = self.VITAL_LIB['vital_camera_intrinsics']
     cam_int.argtypes = [self.c_ptr_type(), VitalErrorHandle.c_ptr_type()]
     cam_int.restype = CameraIntrinsics.c_ptr_type()
     with VitalErrorHandle() as eh:
         c_ptr = cam_int(self, eh)
     return CameraIntrinsics(from_cptr=c_ptr)
Beispiel #6
0
    def test_clone(self):
        # clone should have the same values as original but be a different
        # memory address.
        R = Rotation()
        K = CameraIntrinsics(1000, (640, 480))

        c1 = Camera((0, 3, 5), R, K)
        c2 = c1.clone()

        # Different mem address
        nose.tools.assert_not_equal(ctypes.addressof(c1.c_pointer.contents),
                                    ctypes.addressof(c2.c_pointer.contents))
        numpy.testing.assert_equal(c1.center, c2.center)
        nose.tools.assert_equal(c1.rotation, c2.rotation)
        nose.tools.assert_equal(c1.intrinsics, c2.intrinsics)
Beispiel #7
0
    def test_clone_look_at(self):
        pp = EigenArray.from_iterable([300, 400])
        k = CameraIntrinsics(1000, pp)
        focus = EigenArray.from_iterable([0, 1, -2])

        base = Camera([3, -4, 7], Rotation(), k)
        cam = base.clone_look_at(focus)
        nose.tools.assert_not_equal(base, cam)

        ifocus = cam.project(focus)
        nose.tools.assert_almost_equal(numpy.linalg.norm(ifocus - pp, 2), 0.,
                                       12)

        ifocus_up = cam.project(focus + EigenArray.from_iterable([0, 0, 2]))
        tmp = ifocus_up - pp
        nose.tools.assert_almost_equal(tmp[0], 0., 12)
        nose.tools.assert_true(tmp[1] < 0.)
Beispiel #8
0
    def test_clone_look_at(self):
        pp = EigenArray.from_array([[300], [400]])
        k = CameraIntrinsics(1000, [300, 400])
        focus = EigenArray.from_array([[0], [1], [-2]])
        center = EigenArray.from_array([[3], [-4], [7]])

        base = Camera(center, Rotation(), k)
        cam = base.clone_look_at(numpy.array([0, 1, 2]))
        nose.tools.assert_not_equal(base, cam)

        ifocus = cam.project([0, 1, 2])
        nose.tools.assert_almost_equal(
            numpy.linalg.norm(ifocus - pp.get_matrix().T, 2), 0., 12)

        ifocus_up = cam.project([0, 1, 4])
        tmp = (ifocus_up - pp.get_matrix().T)[0]
        nose.tools.assert_almost_equal(tmp[0], 0., 12)
        nose.tools.assert_true(tmp[1] < 0.)
Beispiel #9
0
def init_cameras(num_cams=20, intrinsics=None):
    """
    Initialize camera sequence with all cameras at the same location (0, 0, 1)
    and looking at origin.

    :param num_cams: Number of cameras to create, default 20.
    :param intrinsics: Intrinsics to use for all cameras.
    :return: Camera map of initialize cameras

    """
    if intrinsics is None:
        intrinsics = CameraIntrinsics(1000, (640, 480))
    r = Rotation()
    c = EigenArray.from_iterable((0, 0, 1))
    d = {}
    for i in range(num_cams):
        cam = Camera(c, r, intrinsics).clone_look_at([0, 0, 0], [0, 1, 0])
        d[i] = cam
    return CameraMap(d)
Beispiel #10
0
def camera_seq(num_cams=20, k=None):
    """
    Create a camera sequence (elliptical path)
    :param num_cams: Number of cameras. Default is 20
    :param k: Camera intrinsics to use for all created cameras. Default has
        focal length = 1000 and principle point of (640, 480).
    :return:
    """
    if k is None:
        k = CameraIntrinsics(1000, [640, 480])
    d = {}
    r = Rotation()  # identity
    for i in xrange(num_cams):
        frac = float(i) / num_cams
        x = 4 * math.cos(2 * frac)
        y = 3 * math.sin(2 * frac)
        d[i] = Camera([x, y, 2 + frac], r, k).clone_look_at([0, 0, 0])

    return CameraMap(d)
Beispiel #11
0
    def test_read_write_krtd_file(self):
        # Use a random string filename to avoid name collision.
        fname = 'temp_camera_test_read_write_krtd_file.txt'

        try:
            for _ in range(100):
                c = (rand(3) * 2 - 1) * 100
                center = EigenArray.from_iterable(c)
                rotation = Rotation.random()
                intrinsics = CameraIntrinsics(focal_length=rand(1) * 1e4,
                                              principle_point=rand(2) * 1000,
                                              aspect_ratio=rand(1),
                                              skew=0.,
                                              dist_coeffs=rand(3))
                c1 = Camera(center=center,
                            rotation=rotation,
                            intrinsics=intrinsics)

                c1.write_krtd_file(fname)
                c2 = Camera.from_krtd_file(fname)

                err = numpy.linalg.norm(c1.center - c2.center)
                assert err < 1e-9, ''.join(
                    ['Centers are different by ',
                     str(err)])

                c1.rotation.angle_from(c2.rotation) < 1e-12

                attr = [
                    'focal_length', 'aspect_ratio', 'principle_point', 'skew',
                    'dist_coeffs'
                ]
                for att in attr:
                    v1 = numpy.array(getattr(c1.intrinsics, att))
                    v2 = numpy.array(getattr(c2.intrinsics, att))
                    err = numpy.linalg.norm(v1 - v2)
                    assert err < 1e-8, ''.join(
                        ['Difference ',
                         str(err), ' for attribute: ', att])
        finally:
            if os.path.isfile(fname):
                os.remove(fname)
Beispiel #12
0
    def _new(self, center, rotation, intrinsics):
        cam_new = self.VITAL_LIB['vital_camera_new']
        cam_new.argtypes = [EigenArray.c_ptr_type(3, 1, ctypes.c_double),
                            Rotation.c_ptr_type(ctypes.c_double),
                            CameraIntrinsics.c_ptr_type(),
                            VitalErrorHandle.c_ptr_type()]
        cam_new.restype = self.c_ptr_type()

        # Fill in parameter gaps
        if center is None:
            center = EigenArray(3)
            center[:] = 0
        else:
            center = EigenArray.from_iterable(center)

        if rotation is None:
            rotation = Rotation()

        if intrinsics is None:
            intrinsics = CameraIntrinsics()

        with VitalErrorHandle() as eh:
            return cam_new(center, rotation, intrinsics, eh)
Beispiel #13
0
 def test_get_focal_length(self):
     ntools.assert_equal(CameraIntrinsics().focal_length, 1.)
     ntools.assert_equal(CameraIntrinsics(5.2).focal_length, 5.2)
Beispiel #14
0
 def test_as_matrix(self):
     numpy.testing.assert_equal(CameraIntrinsics().as_matrix(),
                                numpy.eye(3))
     numpy.testing.assert_equal(
         CameraIntrinsics(10, (2, 3), 2, 5).as_matrix(),
         [[10, 5, 2], [0, 5, 3], [0, 0, 1]])
Beispiel #15
0
 def test_get_dist_coeffs(self):
     numpy.testing.assert_array_equal(CameraIntrinsics().dist_coeffs,
                                      numpy.zeros((0, 1)))
     numpy.testing.assert_array_equal(
         CameraIntrinsics(dist_coeffs=(10, 4, 32, 1.1)).dist_coeffs,
         [[10], [4], [32], [1.1]])
Beispiel #16
0
 def test_get_skew(self):
     ntools.assert_equal(CameraIntrinsics().skew, 0.)
     ntools.assert_equal(CameraIntrinsics(skew=1.).skew, 1.)
Beispiel #17
0
 def test_get_aspect_ratio(self):
     ntools.assert_equal(CameraIntrinsics().aspect_ratio, 1.)
     ntools.assert_equal(
         CameraIntrinsics(aspect_ratio=2.1).aspect_ratio, 2.1)
Beispiel #18
0
 def test_get_principle_point(self):
     numpy.testing.assert_array_equal(CameraIntrinsics().principle_point,
                                      [[0], [0]])
     numpy.testing.assert_array_equal(
         CameraIntrinsics(principle_point=(10, 2.3)).principle_point,
         [[10], [2.3]])
Beispiel #19
0
 def test_default_init(self):
     CameraIntrinsics()
 def test_get_principal_point(self):
     numpy.testing.assert_array_equal(CameraIntrinsics().principal_point,
                                      [0, 0])
     numpy.testing.assert_array_equal(
         CameraIntrinsics(principal_point=(10, 2.3)).principal_point,
         [10, 2.3])
Beispiel #21
0
 def test_full_init(self):
     CameraIntrinsics(10, (5, 5), 1.2, 3.1, [4, 5, 6])