-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_utils.py
More file actions
41 lines (36 loc) · 1.58 KB
/
Copy pathimage_utils.py
File metadata and controls
41 lines (36 loc) · 1.58 KB
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
import cv2
import numpy as np
def convert_bitdepth(image, bitdepth):
# Float input is assumed normalized to [0, 1]; integer paths are unchanged
if bitdepth == 8:
if image.dtype == np.uint8:
return np.uint8(image)
elif np.issubdtype(image.dtype, np.floating):
return (image * 255).astype(np.uint8)
return (image / np.iinfo(image.dtype).max * 255).astype(np.uint8)
elif bitdepth == 16:
if image.dtype == np.uint16:
return np.uint16(image)
elif np.issubdtype(image.dtype, np.floating):
return (image * 65535).astype(np.uint16)
return (image / np.iinfo(image.dtype).max * 65535).astype(np.uint16)
elif bitdepth == 32:
if image.dtype == np.uint32:
return np.uint32(image)
elif np.issubdtype(image.dtype, np.floating):
return (image * 4294967295).astype(np.uint32)
return (image / np.iinfo(image.dtype).max * 4294967295).astype(np.uint32)
return image
def read_grayscale_image(input_image, force_channel = -1, force_bit_depth = 0, minmax_norm = False):
np_img = cv2.imread(input_image, cv2.IMREAD_UNCHANGED)
if np_img is None:
raise FileNotFoundError(f"Could not read image: {input_image}")
if force_channel != -1:
np_img = np_img[:, :, force_channel]
elif np_img.ndim > 2:
np_img = np.max(np_img, axis=2)
if force_bit_depth != 0:
np_img = convert_bitdepth(np_img, force_bit_depth)
elif minmax_norm:
np_img = (np_img - np.amin(np_img)) / (np.amax(np_img) - np.amin(np_img))
return np_img