-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathimg_processing_utils.py
More file actions
64 lines (49 loc) · 1.62 KB
/
img_processing_utils.py
File metadata and controls
64 lines (49 loc) · 1.62 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import cv2
def three_channel_to_grayscale_img(img: np.ndarray) -> np.ndarray:
"""Converts a 3-channel image to grayscale (i.e. 1-channel)
Args:
img (np.ndarray): 3-channel image.
Returns:
np.ndarray: grayscale image.
"""
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def save_img(img: np.ndarray, path: str):
"""Saves an image to path.
Args:
img (np.ndarray): image to save.
path (str): path where to save the image.
"""
cv2.imwrite(path, img)
def is_2D_image(img: np.ndarray) -> bool:
"""True if an image has just two dimensions. False otherwise.
Args:
img (np.ndarray): image.
Returns:
bool: True if an image has just two dimensions. False otherwise.
"""
return img.shape == 2
def normalize_img(
img: np.ndarray,
min: float,
max: float,
a: float = 0,
b: float = 1,
tol: float = 1e-6,
) -> np.ndarray:
"""Normalizes an image from range [min, max] to range [a, b].
Args:
img (np.ndarray): image to normalize.
min (float): minimum of the previous range.
max (float): maximum of the previous range.
a (float, optional): minimum of the new range. Defaults to 0.
b (float, optional): maximum of the new range. Defaults to 1.
tol (float, optional): tolerance. Defaults to 1e-6.
Returns:
np.ndarray: normalized image.
"""
assert max != min
img_norm = ((b - a) * ((img - min) / (max - min))) + a
assert np.count_nonzero(img_norm < a - tol) <= 0
assert np.count_nonzero(img_norm > b + tol) <= 0
return img_norm