camera.py 7.29 KB
Newer Older
1
2
3
4
5
6
import json

import numpy as np
import cv2


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#def depth_to_rgb(depth_frame, min_depth_value, max_depth_value,
#                 colormap=cv2.COLORMAP_HSV, range_min=None, range_max=None):
#  """
#  
#  """
#  valid_mask = cv2.inRange(depth_frame, min_depth_value, max_depth_value)
#  if range_min is None or range_max is None:
#      range_min, range_max, _, _ = cv2.minMaxLoc(depth_frame, valid_mask)
#  if range_max != range_min:
#      scale = np.iinfo(np.uint8).max / (range_max - range_min)
#  else:
#      scale = 1.0
#  depth_frame_view = cv2.convertScaleAbs(depth_frame, alpha=scale,
#                                         beta=-scale * range_min)
#  return cv2.applyColorMap(depth_frame_view, colormap)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127


def points_to_color(points, intrinsics):
    x = points[0] / points[2]
    y = points[1] / points[2]
    r2 = x * x + y * y
    f = 1 + intrinsics.k[0] * r2 + intrinsics.k[1] * r2 * r2 +\
        intrinsics.k[4] * r2 * r2 * r2
    x *= f
    y *= f
    dx = x + 2 * intrinsics.k[2] * x * y + intrinsics.k[3] * (r2 + 2 * x * x)
    dy = y + 2 * intrinsics.k[3] * x * y + intrinsics.k[2] * (r2 + 2 * y * y)
    x = dx
    y = dy
    return (x * intrinsics.f_c + intrinsics.c_0,
            y * intrinsics.f_r + intrinsics.r_0)


def point_to_color(point, intrinsics):
    x = point[0] / point[2]
    y = point[1] / point[2]
    r2 = x * x + y * y
    f = 1 + intrinsics.k[0] * r2 + intrinsics.k[1] * r2 * r2 +\
        intrinsics.k[4] * r2 * r2 * r2
    x *= f
    y *= f
    dx = x + 2 * intrinsics.k[2] * x * y + intrinsics.k[3] * (r2 + 2 * x * x)
    dy = y + 2 * intrinsics.k[3] * x * y + intrinsics.k[2] * (r2 + 2 * y * y)
    x = dx
    y = dy
    return np.float32([x * intrinsics.f_c + intrinsics.c_0,
                       y * intrinsics.f_r + intrinsics.r_0])


def depth_to_points(depth_map, scale, intrinsics):
    c, r = np.meshgrid(range(depth_map.shape[1]), range(depth_map.shape[0]))
    x = (c - intrinsics.c_0) / intrinsics.f_c
    y = (r - intrinsics.r_0) / intrinsics.f_r
    r2 = x * x + y * y
    f = 1 + intrinsics.k[0] * r2 +\
        intrinsics.k[1] * r2 * r2 + intrinsics.k[4] * r2 * r2 * r2
    ux = x * f + 2 * intrinsics.k[2] * x * y +\
        intrinsics.k[3] * (r2 + 2 * x * x)
    uy = y * f + 2 * intrinsics.k[3] * x * y +\
        intrinsics.k[2] * (r2 + 2 * y * y)
    x = ux * scale * depth_map
    y = uy * scale * depth_map
    z = scale * depth_map
    return x, y, z


def depth_to_point(r, c, depth, scale, intrinsics):
    x = (c - intrinsics.c_0) / intrinsics.f_c
    y = (r - intrinsics.r_0) / intrinsics.f_r
    r2 = x * x + y * y
    f = 1 + intrinsics.k[0] * r2 +\
        intrinsics.k[1] * r2 * r2 + intrinsics.k[4] * r2 * r2 * r2
    ux = x * f + 2 * intrinsics.k[2] * x * y +\
        intrinsics.k[3] * (r2 + 2 * x * x)
    uy = y * f + 2 * intrinsics.k[3] * x * y +\
        intrinsics.k[2] * (r2 + 2 * y * y)
    x = ux
    y = uy
    return np.float32([depth * scale * x, depth * scale * y, depth * scale])


def transform_points(points, extrinsics):
    return (extrinsics.rotation[0] * points[0] +
            extrinsics.rotation[3] * points[1] +
            extrinsics.rotation[6] * points[2] +
            extrinsics.translation[0],
            extrinsics.rotation[1] * points[0] +
            extrinsics.rotation[4] * points[1] +
            extrinsics.rotation[7] * points[2] +
            extrinsics.translation[1],
            extrinsics.rotation[2] * points[0] +
            extrinsics.rotation[5] * points[1] +
            extrinsics.rotation[8] * points[2] +
            extrinsics.translation[2])


def transform_point(point, extrinsics):
    return np.float32([extrinsics.rotation[0] * point[0] +
                       extrinsics.rotation[3] * point[1] +
                       extrinsics.rotation[6] * point[2] +
                       extrinsics.translation[0],
                       extrinsics.rotation[1] * point[0] +
                       extrinsics.rotation[4] * point[1] +
                       extrinsics.rotation[7] * point[2] +
                       extrinsics.translation[1],
                       extrinsics.rotation[2] * point[0] +
                       extrinsics.rotation[5] * point[1] +
                       extrinsics.rotation[8] * point[2] +
                       extrinsics.translation[2]])


def get_UV_map(depth_image, min_depth, depth_intrinsics, depth_scale,
               color_intrinsics, depth2color):
    depth_points = depth_to_points(depth_image, depth_scale, depth_intrinsics)
    depth_points = np.where(depth_image >= min_depth, depth_points, np.nan)
    color_points = transform_points(depth_points, depth2color)
    color_pixels = points_to_color(color_points, color_intrinsics)
    return (np.clip(color_pixels[0] / color_intrinsics.width, 0, 1),
            np.clip(color_pixels[1] / color_intrinsics.height, 0, 1))


128
129
130
131
def get_correspondences(points, UV_map, color_width, color_height, depth_width, depth_height):

    x = points[:, 0].reshape((1, -1)) - np.array(UV_map[0] * color_width).ravel().reshape((-1, 1))
    y = points[:, 1].reshape((1, -1)) - np.array(UV_map[1] * color_height).ravel().reshape((-1, 1))
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    dist2 = x * x + y * y
    return np.nanmin(dist2, 0), np.nanargmin(dist2, 0)


class IntrinsicParameters:
    def __init__(self, width=None, height=None, c_0=None, r_0=None, f_c=None,
                 f_r=None, k=None, model=None, fov_h=None, fov_v=None):
        self.width = width
        self.height = height
        self.c_0 = c_0
        self.r_0 = r_0
        self.f_c = f_c
        self.f_r = f_r
        self.k = k
        self.model = model
        self.fov_h = fov_h
        self.fov_v = fov_v

    def read_json(self, filepath, key):
        with open(filepath) as json_file:
            device_info = json.load(json_file)
        device_info = device_info['device']
        if key not in device_info:
            raise ValueError('{} not in {}'.format(key, filepath))
        device_info = device_info[key]
        resolution = device_info['resolution']
        principal_point = device_info['principal_point']
        focal = device_info['focal']
        if 'distortion' in device_info:
            dist_coeffs = np.array(device_info['distortion']['coeffs'])
            if 'type' in device_info['distortion']:
                dist_model = device_info['distortion']['type']
            else:
                dist_model = 'unknown'
        else:
            dist_coeffs = np.zeros((5, 1), dtype=np.float32)
            dist_model = None
        fov = device_info['field_of_view']
        self.width = resolution[0]
        self.height = resolution[1]
        self.c_0 = principal_point[0]
        self.r_0 = principal_point[1]
        self.f_c = focal[0]
        self.f_r = focal[1]
        self.k = dist_coeffs
        self.model = dist_model
        self.fov_h = fov[0]
        self.fov_v = fov[1]


class ExtrinsicParameters:
    def __init(self, translation=None, rotation=None):
        self.translation = translation
        self.rotation = rotation

    def read_json(self, filepath, key):
        with open(filepath) as json_file:
            device_info = json.load(json_file)
        device_info = device_info['device']
        if key not in device_info:
            raise ValueError('{} not in {}'.format(key, filepath))
        device_info = device_info[key]
        self.translation = np.float32(device_info['translation']) * 1e-3
        self.rotation = np.float32(device_info['rotation'])