-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
49 lines (39 loc) · 1.36 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
44
45
46
47
48
49
import cv2
import numpy as np
def open_image(image_path: str) -> np.ndarray:
"""
Open image as NumPy Array 64-bit float with 3 channels (BGR)
Parameters:
- image_path: string containing the path to the image file
Returns:
- image: 3D 64bit float array, shape is (height, width, channels)
"""
image_cv = cv2.imread(image_path, flags=cv2.IMREAD_COLOR)
return np.array(image_cv, np.float64)
def write_image(image_path: str, image: np.ndarray, transform=None) -> None:
"""
Write a NumPy Array as a png image. Will apply transform
to the array before writing
Parameters:
- image_path: string, output image file path
- image: numpy array containing the image data
- transform: function that will transform
"""
if transform:
pass
if type(image) != np.uint8:
image = np.clip(image, 0, 255).astype(np.uint8)
cv2.imwrite(image_path, image)
def kernel_from_image(image_path: str) -> np.ndarray:
"""
Open an image as NumPy Array 64-bit float 1 channel and
normalize values so that they sum 1
Parameters:
- image_path: string containing the path to the image file
Returns:
- kernel: 2D 64bit float array
"""
image_cv = cv2.imread(image_path, flags=cv2.IMREAD_GRAYSCALE)
kernel = np.array(image_cv)
kernel = kernel / np.sum(kernel)
return kernel