Exemplo n.º 1
0
    def test_point_map(self):
        h_f = Homography('f')
        h_d = Homography('d')

        p_af = EigenArray.from_array([[2.2, 3.3]], 'f')
        p_f = p_af.get_matrix()[0]
        p_ad = EigenArray.from_array([[5.5, 6.6]], 'd')
        p_d = p_ad.get_matrix()[0]

        # float-float
        numpy.testing.assert_almost_equal(
            h_f.map(p_f), p_f
        )
        # float-double
        numpy.testing.assert_almost_equal(
            h_f.map(p_d), p_d
        )
        # double-float
        numpy.testing.assert_almost_equal(
            h_d.map(p_f), p_f
        )
        # double-double
        numpy.testing.assert_almost_equal(
            h_d.map(p_d), p_d
        )

        # Code to generate truth
        h = numpy.random.rand(3,3)
        h = h/numpy.linalg.norm(h)
        p0 = numpy.random.rand(3); p0[2] = 1
        p1 = numpy.dot(h, p0)
        p1 = p1[:2]/p1[2]
        h_d = Homography.from_matrix(h, 'd')

        # map from Numpy array.
        numpy.testing.assert_almost_equal(
            h_d.map(p0[:2]).ravel(), p1
        )

        # map from EigenArray
        p0 = EigenArray.from_array([p0[:2]])
        numpy.testing.assert_almost_equal(
            h_d.map(p0.get_matrix()[0]).ravel(), p1
        )

        # Another explicit case.
        p0 = numpy.array([1923.47,645.676,1])
        h = numpy.array([[5.491496261770000276e-01,-1.125428185150000038e-01,
                          1.358427031619999923e+02],
                         [-1.429513389049999993e-02	,6.035527375529999849e-01,
                          5.923971959490000216e+01],
                         [-2.042570000000000164e-06,-2.871670000000000197e-07,
                          1]])
        p1 = numpy.dot(h, p0);      p1 = p1[:2]/p1[2]
        H = Homography.from_matrix(h)
        P = EigenArray.from_array([p0[:2]])
        numpy.testing.assert_almost_equal(
            H.map(P.get_matrix()[0]).ravel(), p1
        )
Exemplo n.º 2
0
 def test_normalize(self):
     h = Homography.from_matrix([[-.5, -2.5, 1.5],
                                 [-1.5, 1.5, -.5],
                                 [1.5,   .5, -.5]])
     e = Homography.from_matrix([[1,   5, -3],
                                 [3,  -3,  1],
                                 [-3, -1,  1]])
     numpy.testing.assert_array_equal(h.normalize(), e.as_matrix())
Exemplo n.º 3
0
    def test_equal(self):
        # Identity should be equal to itself
        h1 = Homography()
        h2 = Homography()
        nose.tools.assert_equal(h1, h2)

        # manually constructed homographies should be equal
        m = [[.1, .2, .3],
             [.4, .5, .6],
             [.7, .8, .9]]
        h1 = Homography.from_matrix(m)
        h2 = Homography.from_matrix(m)
        nose.tools.assert_equal(h1, h2)
        # and should also be different than identity
        nose.tools.assert_not_equal(h1, Homography())
        nose.tools.assert_not_equal(h2, Homography())
Exemplo n.º 4
0
    def test_numeric_invertibility(self):
        exp_result = Homography.from_matrix([[-.5, -2.5, 1.5],
                                             [-1.5, 1.5, -.5],
                                             [1.5,   .5, -.5]])

        h = Homography.from_matrix([[1, 1, 2],
                                    [3, 4, 5],
                                    [6, 7, 9]])
        h_inv = h.inverse()
        numpy.testing.assert_array_equal(h_inv, exp_result.as_matrix())

        h = Homography.from_matrix([[1, 1, 2],
                                    [3, 4, 5],
                                    [6, 7, 9]],
                                   'f')
        h_inv = h.inverse()
        numpy.testing.assert_array_equal(h_inv, exp_result.as_matrix())
Exemplo n.º 5
0
    def test_clone(self):
        m = [[.1, .2, .3],
             [.4, .5, .6],
             [.7, .8, .9]]
        # Cloning a matrix should yield a new instance that has the same value
        # as the original
        h1 = Homography.from_matrix(m, ctypes.c_double)
        h2 = h1.clone()
        nose.tools.assert_false(h1 is h2)
        nose.tools.assert_not_equal(ctypes.addressof(h1.c_pointer.contents),
                                    ctypes.addressof(h2.c_pointer.contents))
        numpy.testing.assert_almost_equal(h1.as_matrix(), h2.as_matrix())

        # Cloning should carry over data type
        nose.tools.assert_equal(h1.type_name, h2.type_name)

        h3 = Homography.from_matrix(m, ctypes.c_float)
        h4 = h3.clone()
        nose.tools.assert_equal(h3.type_name, h4.type_name)
Exemplo n.º 6
0
    def test_multiply(self):
        # Test multiplying homographies together
        h_ident = Homography()
        h_valued = Homography.from_matrix([[1, 0, 1], [0, 1, 1], [0, 0, .5]])

        r1 = h_ident * h_ident
        nose.tools.assert_equal(h_ident, r1)

        r2 = h_ident * h_valued
        nose.tools.assert_equal(r2, h_valued)

        r3 = h_valued * h_ident
        nose.tools.assert_equal(r3, h_valued)

        # Failure, multiplying against non-homography
        def h_mult(h, v):
            return h * v

        nose.tools.assert_raises(ValueError, h_mult, h_ident, 1)
        nose.tools.assert_raises(TypeError, h_mult, h_ident, 'a string')
Exemplo n.º 7
0
    def test_point_map(self):
        h_f = Homography(ctypes.c_float)
        h_d = Homography(ctypes.c_double)

        p_f = EigenArray.from_iterable([2.2, 3.3], ctypes.c_float)
        p_d = EigenArray.from_iterable([5.5, 6.6], ctypes.c_double)

        # float-float
        numpy.testing.assert_almost_equal(h_f.map(p_f), p_f)
        # float-double
        numpy.testing.assert_almost_equal(h_f.map(p_d), p_d)
        # double-float
        numpy.testing.assert_almost_equal(h_d.map(p_f), p_f)
        # double-double
        numpy.testing.assert_almost_equal(h_d.map(p_d), p_d)
Exemplo n.º 8
0
    def test_point_map_zero_div(self):
        test_p = [1, 1]

        for dtype, e in [['f', numpy.finfo('f').min],
                         ['d', sys.float_info.min]]:
            print("Dtype:", dtype)
            print("E:", e)

            # where [2,2] = 0
            h = Homography.from_matrix([[1, 0, 1],
                                        [0, 1, 1],
                                        [0, 0, 0]],
                                       dtype)
            nose.tools.assert_raises(
                RuntimeError,
                h.map, test_p
            )

            # Where [2,2] = e which is approximately 0
            e = sys.float_info.min
            h = Homography.from_matrix([[1, 0, 1],
                                        [0, 1, 1],
                                        [0, 0, e]],
                                       dtype)
            print("E Matrix:", h.as_matrix())
            nose.tools.assert_raises(
                RuntimeError,
                h.map, test_p
            )

            # Where [2,2] = 0.5, which should be valid
            h = Homography.from_matrix([[1, 0,  1],
                                        [0, 1,  1],
                                        [0, 0, .5]],
                                       dtype)
            r = h.map(test_p)
            nose.tools.assert_almost_equal(r[0], 4)
            nose.tools.assert_almost_equal(r[1], 4)
Exemplo n.º 9
0
    def test_point_map_zero_div(self):
        test_p = [1, 1]

        for dtype, e in [[ctypes.c_float, numpy.finfo(ctypes.c_float).min],
                         [ctypes.c_double, sys.float_info.min]]:
            print "Dtype:", dtype
            print "E:", e

            # where [2,2] = 0
            h = Homography.from_matrix([[1, 0, 1],
                                        [0, 1, 1],
                                        [0, 0, 0]],
                                       dtype)
            nose.tools.assert_raises(
                PointMapsToInfinityException,
                h.map, test_p
            )

            # Where [2,2] = e which is approximately 0
            e = sys.float_info.min
            h = Homography.from_matrix([[1, 0, 1],
                                        [0, 1, 1],
                                        [0, 0, e]],
                                       dtype)
            print "E Matrix:", h.as_matrix()
            nose.tools.assert_raises(
                PointMapsToInfinityException,
                h.map, test_p
            )

            # Where [2,2] = 0.5, which should be valid
            h = Homography.from_matrix([[1, 0,  1],
                                        [0, 1,  1],
                                        [0, 0, .5]],
                                       dtype)
            r = h.map(test_p)
            nose.tools.assert_almost_equal(r[0], 4)
            nose.tools.assert_almost_equal(r[1], 4)
Exemplo n.º 10
0
    def test_read_from_file(self):
        # Use a random string filename to avoid name collision.
        fname = 'temp_homography_test_read_from_file.txt'

        # Reference homography
        h = Homography.from_matrix([[1, 0, 1], [0, 12.01321561, 1], [0, 0,
                                                                     .5]])

        try:
            h.write_to_file(fname)

            # Try reading with the string filename.
            h2 = Homography.read_from_file(fname)
            assert h == h2

            # Try reading from an open file.
            with open(fname, 'r') as f:
                h2 = Homography.read_from_file(f)

            numpy.testing.assert_almost_equal(h.as_matrix(), h2.as_matrix())
        finally:
            if os.path.isfile(fname):
                os.remove(fname)
Exemplo n.º 11
0
    def test_as_matrix(self):
        numpy.testing.assert_almost_equal(
            Homography('d').as_matrix(),
            [[1, 0, 0],
             [0, 1, 0],
             [0, 0, 1]]
        )
        numpy.testing.assert_almost_equal(
            Homography('f').as_matrix(),
            [[1, 0, 0],
             [0, 1, 0],
             [0, 0, 1]]
        )

        m = [[1, 2, 3],
             [4, 5, 6],
             [7, 8, 9]]
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'd').as_matrix(),
            m
        )
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'f').as_matrix(),
            m
        )

        m = [[.1, .2, .3],
             [.4, .5, .6],
             [.7, .8, .9]]
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'd').as_matrix(),
            m
        )
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'f').as_matrix(),
            m
        )
Exemplo n.º 12
0
    def test_multiply(self):
        # Test multiplying homographies together
        h_ident = Homography()
        h_valued = Homography.from_matrix([[1, 0,  1],
                                           [0, 1,  1],
                                           [0, 0, .5]])

        r1 = h_ident * h_ident
        nose.tools.assert_equal(h_ident, r1)

        r2 = h_ident * h_valued
        nose.tools.assert_equal(r2, h_valued)

        r3 = h_valued * h_ident
        nose.tools.assert_equal(r3, h_valued)
Exemplo n.º 13
0
    def test_write_to_file(self):
        # Use a random string filename to avoid name collision.
        fname = 'temp_homography_test_write_to_file.txt'

        h = Homography.from_matrix([[1, 0, 1], [0, 12.01321561, 1], [0, 0,
                                                                     .5]])

        try:
            # Try writing with the string filename.
            h.write_to_file(fname)

            # Try writing with an open file.
            with open(fname, 'w') as f:
                h.write_to_file(f)
        finally:
            if os.path.isfile(fname):
                os.remove(fname)
Exemplo n.º 14
0
    def test_equal(self):
        # Identity should be equal to itself
        h1 = Homography()
        h2 = Homography()
        nose.tools.assert_equal(h1, h2)

        # manually constructed homographies should be equal
        m = [[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]]
        h1 = Homography.from_matrix(m)
        h2 = Homography.from_matrix(m)
        nose.tools.assert_equal(h1, h2)
        # and should also be different than identity
        nose.tools.assert_not_equal(h1, Homography())
        nose.tools.assert_not_equal(h2, Homography())
Exemplo n.º 15
0
    def test_as_matrix(self):
        numpy.testing.assert_almost_equal(
            Homography('d').as_matrix(), [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        numpy.testing.assert_almost_equal(
            Homography('f').as_matrix(), [[1, 0, 0], [0, 1, 0], [0, 0, 1]])

        m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'd').as_matrix(), m)
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'f').as_matrix(), m)

        m = [[.1, .2, .3], [.4, .5, .6], [.7, .8, .9]]
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'd').as_matrix(), m)
        numpy.testing.assert_almost_equal(
            Homography.from_matrix(m, 'f').as_matrix(), m)
Exemplo n.º 16
0
    def test_typename(self):
        h_d = Homography(ctypes.c_double)
        h_f = Homography(ctypes.c_float)

        nose.tools.assert_equal(h_d.type_name, 'd')
        nose.tools.assert_equal(h_f.type_name, 'f')
Exemplo n.º 17
0
 def random(self):
     Homography.random(ctypes.c_double)
     Homography.random(ctypes.c_float)
Exemplo n.º 18
0
    def test_matrix_init(self):
        # Test that construction does not fail when passing valid matrices as
        # initializer
        m1 = [[0, 1, 3], [0.3, 0.1, 10], [-1, 8.1, 4.7]]
        m2_d = EigenArray.from_iterable(m1, ctypes.c_double, (3, 3))
        m2_f = EigenArray.from_iterable(m1, ctypes.c_float, (3, 3))

        Homography.from_matrix(m1, ctypes.c_double)
        Homography.from_matrix(m1, ctypes.c_float)
        Homography.from_matrix(m2_d, ctypes.c_double)
        Homography.from_matrix(m2_d, ctypes.c_float)
        Homography.from_matrix(m2_f, ctypes.c_double)
        Homography.from_matrix(m2_f, ctypes.c_float)
Exemplo n.º 19
0
 def test_ident_init(self):
     h_d = Homography()
     h_f = Homography(ctypes.c_float)
Exemplo n.º 20
0
 def test_normalize(self):
     h = Homography.from_matrix([[-.5, -2.5, 1.5], [-1.5, 1.5, -.5],
                                 [1.5, .5, -.5]])
     e = Homography.from_matrix([[1, 5, -3], [3, -3, 1], [-3, -1, 1]])
     numpy.testing.assert_array_equal(h.normalize(), e.as_matrix())
Exemplo n.º 21
0
 def test_ident_init(self):
     h_d = Homography()
     h_f = Homography('f')
Exemplo n.º 22
0
    def test_point_map(self):
        h_f = Homography(ctypes.c_float)
        h_d = Homography(ctypes.c_double)

        p_f = EigenArray.from_iterable([2.2, 3.3], ctypes.c_float)
        p_d = EigenArray.from_iterable([5.5, 6.6], ctypes.c_double)

        # float-float
        numpy.testing.assert_almost_equal(h_f.map(p_f), p_f)
        # float-double
        numpy.testing.assert_almost_equal(h_f.map(p_d), p_d)
        # double-float
        numpy.testing.assert_almost_equal(h_d.map(p_f), p_f)
        # double-double
        numpy.testing.assert_almost_equal(h_d.map(p_d), p_d)

        # Code to generate truth
        h = numpy.random.rand(3, 3)
        h = h / numpy.linalg.norm(h)
        p0 = numpy.random.rand(3)
        p0[2] = 1
        p1 = numpy.dot(h, p0)
        p1 = p1[:2] / p1[2]
        h_d = Homography.from_matrix(h, ctypes.c_double)

        # map from Numpy array.
        numpy.testing.assert_almost_equal(h_d.map(p0[:2]).ravel(), p1)

        # map from EigenArray
        p0 = EigenArray.from_iterable(p0[:2])
        numpy.testing.assert_almost_equal(h_d.map(p0).ravel(), p1)

        # Another explicit case.
        p0 = numpy.array([1923.47, 645.676, 1])
        h = numpy.array(
            [[
                5.491496261770000276e-01, -1.125428185150000038e-01,
                1.358427031619999923e+02
            ],
             [
                 -1.429513389049999993e-02, 6.035527375529999849e-01,
                 5.923971959490000216e+01
             ], [-2.042570000000000164e-06, -2.871670000000000197e-07, 1]])
        p1 = numpy.dot(h, p0)
        p1 = p1[:2] / p1[2]
        H = Homography.from_matrix(h)
        P = EigenArray.from_iterable(p0[:2])
        numpy.testing.assert_almost_equal(H.map(P).ravel(), p1)
Exemplo n.º 23
0
 def test_normalize(self):
     h = Homography.from_matrix([[-.5, -2.5, 1.5], [-1.5, 1.5, -.5],
                                 [1.5, .5, -.5]])
     e = Homography.from_matrix([[1, 5, -3], [3, -3, 1], [-3, -1, 1]])
     nose.tools.assert_equal(h.normalize(), e)
Exemplo n.º 24
0
    def test_matrix_init(self):
        # Test that construction does not fail when passing valid matrices as
        # initializer
        m1 = [[0, 1, 3], [0.3, 0.1, 10], [-1, 8.1, 4.7]]
        m2_d = EigenArray.from_array(m1, 'd')
        m2_f = EigenArray.from_array(m1, 'f')

        Homography.from_matrix(m1, 'd')
        Homography.from_matrix(m1, 'f')
        Homography.from_matrix(m2_d.get_matrix(), 'd')
        Homography.from_matrix(m2_d.get_matrix(), 'f')
        Homography.from_matrix(m2_f.get_matrix(), 'd')
        Homography.from_matrix(m2_f.get_matrix(), 'f')
Exemplo n.º 25
0
 def test_random(self):
     Homography.random('d')
     Homography.random('f')
Exemplo n.º 26
0
 def test_random(self):
     Homography.random('d')
     Homography.random('f')
Exemplo n.º 27
0
    def test_matrix_init(self):
        # Test that construction does not fail when passing valid matrices as
        # initializer
        m1 = [[0,     1,   3],
              [0.3, 0.1,  10],
              [-1,  8.1, 4.7]]
        m2_d = EigenArray.from_array(m1, 'd')
        m2_f = EigenArray.from_array(m1, 'f')

        Homography.from_matrix(m1, 'd')
        Homography.from_matrix(m1, 'f')
        Homography.from_matrix(m2_d.get_matrix(), 'd')
        Homography.from_matrix(m2_d.get_matrix(), 'f')
        Homography.from_matrix(m2_f.get_matrix(), 'd')
        Homography.from_matrix(m2_f.get_matrix(), 'f')
Exemplo n.º 28
0
    def test_typename(self):
        h_d = Homography('d')
        h_f = Homography('f')

        nose.tools.assert_equal(h_d.type_name, 'd')
        nose.tools.assert_equal(h_f.type_name, 'f')