Esempio n. 1
0
def generate_scene(num_views: int, num_points: int) -> Dict[str, torch.Tensor]:
    # Generate the 3d points
    points3d = torch.rand(1, num_points, 3)  # NxMx3

    # Create random camera matrix
    K = epipolar.random_intrinsics(0.0, 100.0)  # 1x3x3

    # Create random rotation per view
    ang = torch.rand(num_views, 1) * kornia.pi * 2.0

    rvec = torch.rand(num_views, 3)
    rvec = ang * rvec / torch.norm(rvec, dim=1, keepdim=True)  # Nx3
    rot_mat = kornia.angle_axis_to_rotation_matrix(rvec)  # Nx3x3
    # matches with cv2.Rodrigues -> yay !

    # Create random translation per view
    tx = torch.empty(num_views).uniform_(-0.5, 0.5)
    ty = torch.empty(num_views).uniform_(-0.5, 0.5)
    tz = torch.empty(num_views).uniform_(-1.0, 2.0)
    tvec = torch.stack([tx, ty, tz], dim=1)[..., None]

    # Make sure the shape is in front of the camera
    points3d_trans = (rot_mat @ points3d.transpose(-2, -1)) + tvec
    min_dist = torch.min(points3d_trans[:, 2], dim=1)[0]
    tvec[:, 2, 0] = torch.where(min_dist < 0, tz - min_dist + 1.0, tz)

    # compute projection matrices
    P = epipolar.projection_from_KRt(K, rot_mat, tvec)

    # project points3d and backproject to image plane
    points2d = kornia.transform_points(P, points3d.expand(num_views, -1, -1))

    return dict(K=K, R=rot_mat, t=tvec, P=P, points3d=points3d, points2d=points2d)
Esempio n. 2
0
 def test_shape(self, batch_size, device, dtype):
     B: int = batch_size
     K = torch.rand(B, 3, 3, device=device, dtype=dtype)
     R = torch.rand(B, 3, 3, device=device, dtype=dtype)
     t = torch.rand(B, 3, 1, device=device, dtype=dtype)
     P = epi.projection_from_KRt(K, R, t)
     assert P.shape == (B, 3, 4)
Esempio n. 3
0
    def test_projection_from_krt(self, device, dtype):
        K = torch.tensor(
            [[[17.006138, 122.441254, 390.211426],
              [0.0, 228.743622, 577.167480], [0.0, 0.0, 712.675232]]],
            device=device,
            dtype=dtype,
        )

        R = torch.tensor(
            [[[0.396559, 0.511023, -0.762625], [
                0.743249, -0.666318, -0.060006
            ], [0.538815, 0.543024, 0.644052]]],
            device=device,
            dtype=dtype,
        )

        t = torch.tensor([[[-6.477699], [1.129624], [0.143123]]],
                         device=device,
                         dtype=dtype)

        P_expected = torch.tensor(
            [[[308.0, 139.0, 231.0, 84.0], [481.0, 161.0, 358.0, 341.0],
              [384.0, 387.0, 459.0, 102.0]]],
            device=device,
            dtype=dtype,
        )

        P_estimated = epi.projection_from_KRt(K, R, t)
        assert_allclose(P_estimated, P_expected, atol=1e-4, rtol=1e-4)
Esempio n. 4
0
    def test_simple(self, device, dtype):
        K = torch.tensor([[
            [10., 0., 30.],
            [0., 20., 40.],
            [0., 0., 1.],
        ]],
                         device=device,
                         dtype=dtype)

        R = torch.tensor([[
            [1., 0., 0.],
            [0., 1., 0.],
            [0., 0., 1.],
        ]],
                         device=device,
                         dtype=dtype)

        t = torch.tensor([
            [[1.], [2.], [3.]],
        ], device=device, dtype=dtype)

        P_expected = torch.tensor([[
            [10., 0., 30., 100.],
            [0., 20., 40., 160.],
            [0., 0., 1., 3.],
        ]],
                                  device=device,
                                  dtype=dtype)

        P_estimated = epi.projection_from_KRt(K, R, t)
        assert_allclose(P_estimated, P_expected)
Esempio n. 5
0
    def test_simple(self, device, dtype):
        K = torch.tensor(
            [[[10.0, 0.0, 30.0], [0.0, 20.0, 40.0], [0.0, 0.0, 1.0]]],
            device=device,
            dtype=dtype)

        R = torch.tensor([[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]],
                         device=device,
                         dtype=dtype)

        t = torch.tensor([[[1.0], [2.0], [3.0]]], device=device, dtype=dtype)

        P_expected = torch.tensor(
            [[[10.0, 0.0, 30.0, 100.0], [0.0, 20.0, 40.0, 160.0],
              [0.0, 0.0, 1.0, 3.0]]],
            device=device,
            dtype=dtype)

        P_estimated = epi.projection_from_KRt(K, R, t)
        assert_allclose(P_estimated, P_expected, atol=1e-4, rtol=1e-4)
Esempio n. 6
0
 def test_smoke(self, device, dtype):
     K = torch.rand(1, 3, 3, device=device, dtype=dtype)
     R = torch.rand(1, 3, 3, device=device, dtype=dtype)
     t = torch.rand(1, 3, 1, device=device, dtype=dtype)
     P = epi.projection_from_KRt(K, R, t)
     assert P.shape == (1, 3, 4)