File size: 2,446 Bytes
2252f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-

# Max-Planck-Gesellschaft zur Förderung der Wissenschaften e.V. (MPG) is
# holder of all proprietary rights on this computer program.
# You can only use this computer program if you have closed
# a license agreement with MPG or you get the right to use the computer
# program from someone who is authorized to grant you that right.
# Any use of the computer program without a valid license is prohibited and
# liable to prosecution.
#
# Copyright©2019 Max-Planck-Gesellschaft zur Förderung
# der Wissenschaften e.V. (MPG). acting on behalf of its Max Planck Institute
# for Intelligent Systems. All rights reserved.
#
# Contact: ps-license@tuebingen.mpg.de

import os
from numpy.testing._private.utils import print_assert_equal
import torch
import numpy as np
import joblib

from .geometry import batch_euler2matrix


def f_pix2vfov(f_pix, img_h):

    if torch.is_tensor(f_pix):
        fov = 2. * torch.arctan(img_h / (2. * f_pix))
    else:
        fov = 2. * np.arctan(img_h / (2. * f_pix))

    return fov


def vfov2f_pix(fov, img_h):

    if torch.is_tensor(fov):
        f_pix = img_h / 2. / torch.tan(fov / 2.)
    else:
        f_pix = img_h / 2. / np.tan(fov / 2.)

    return f_pix


def read_cam_params(cam_params, orig_shape=None):
    # These are predicted camera parameters
    # cam_param_folder = CAM_PARAM_FOLDERS[dataset_name][cam_param_type]

    cam_pitch = cam_params['pitch'].item()
    cam_roll = cam_params['roll'].item() if 'roll' in cam_params else None

    cam_vfov = cam_params['vfov'].item() if 'vfov' in cam_params else None

    cam_focal_length = cam_params['f_pix']

    orig_shape = cam_params['orig_resolution']

    # cam_rotmat = batch_euler2matrix(torch.tensor([[cam_pitch, 0., cam_roll]]).float())[0]
    cam_rotmat = batch_euler2matrix(torch.tensor([[cam_pitch, 0., 0.]]).float())[0]

    pred_cam_int = torch.zeros(3, 3)

    cx, cy = orig_shape[1] / 2, orig_shape[0] / 2

    pred_cam_int[0, 0] = cam_focal_length
    pred_cam_int[1, 1] = cam_focal_length

    pred_cam_int[:-1, -1] = torch.tensor([cx, cy])

    cam_int = pred_cam_int.float()

    return cam_rotmat, cam_int, cam_vfov, cam_pitch, cam_roll, cam_focal_length


def homo_vector(vector):
    """
    vector: B x N x C
    h_vector: B x N x (C + 1)
    """

    batch_size, n_pts = vector.shape[:2]

    h_vector = torch.cat([vector, torch.ones((batch_size, n_pts, 1)).to(vector)], dim=-1)
    return h_vector