-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_converter.py
28 lines (22 loc) · 949 Bytes
/
image_converter.py
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
import numpy as np
def depth_to_array(image):
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4)) # RGBA format
array = array[:, :, :3] # Take only RGB
array = array[:, :, ::-1] # BGR
array = array.astype(np.float32) # 2ms
gray_depth = ((array[:, :, 0] + array[:, :, 1] * 256.0 + array[:, :, 2] * 256.0 * 256.0) / (
(256.0 * 256.0 * 256.0) - 1)) # 2.5ms
gray_depth = 1000 * gray_depth
return gray_depth
def to_bgra_array(image):
"""Convert a CARLA raw image to a BGRA numpy array."""
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))
array = np.reshape(array, (image.height, image.width, 4))
return array
def to_rgb_array(image):
"""Convert a CARLA raw image to a RGB numpy array."""
array = to_bgra_array(image)
array = array[:, :, :3]
array = array[:, :, ::-1]
return array