-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
43 lines (35 loc) · 1.54 KB
/
helpers.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import Optional, Tuple
import cv2
import numpy as np
from pyk4a import ImageFormat
def convert_to_bgra_if_required(color_format: ImageFormat, color_image):
# examples for all possible pyk4a.ColorFormats
if color_format == ImageFormat.COLOR_MJPG:
color_image = cv2.imdecode(color_image, cv2.IMREAD_COLOR)
elif color_format == ImageFormat.COLOR_NV12:
color_image = cv2.cvtColor(color_image, cv2.COLOR_YUV2BGRA_NV12)
# this also works and it explains how the COLOR_NV12 color color_format is stored in memory
# h, w = color_image.shape[0:2]
# h = h // 3 * 2
# luminance = color_image[:h]
# chroma = color_image[h:, :w//2]
# color_image = cv2.cvtColorTwoPlane(luminance, chroma, cv2.COLOR_YUV2BGRA_NV12)
elif color_format == ImageFormat.COLOR_YUY2:
color_image = cv2.cvtColor(color_image, cv2.COLOR_YUV2BGRA_YUY2)
return color_image
def colorize(
image: np.ndarray,
clipping_range: Tuple[Optional[int], Optional[int]] = (None, None),
colormap: int = cv2.COLORMAP_RAINBOW
) -> np.ndarray:
invalid_mask = image == 0
if clipping_range[0] or clipping_range[1]:
img = image.clip(clipping_range[0], clipping_range[1])
else:
img = image.copy()
img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
# logarithmic colouring
img_log = np.array(255 * np.log(img, where=np.invert(invalid_mask)) / np.log(255), dtype=np.uint8)
img = cv2.applyColorMap(img_log, colormap)
img[invalid_mask] = 0
return img