forked from WongKinYiu/yolov7
-
Notifications
You must be signed in to change notification settings - Fork 1
/
colorize.py
37 lines (31 loc) · 1.37 KB
/
colorize.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
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_HSV,
) -> np.ndarray:
if clipping_range[0] or clipping_range[1]:
img = image.clip(clipping_range[0], clipping_range[1]) # type: ignore
else:
img = image.copy()
img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
img = cv2.applyColorMap(img, colormap)
return img