-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·67 lines (53 loc) · 2.22 KB
/
utils.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import cv2
from PIL import Image
class Transforms:
"""
Holds transformation matrices.
All the details for transformation matrices can be found at: https://arxiv.org/pdf/1711.10662.pdf.
"""
@staticmethod
def rgb_to_lms():
return np.array([[17.8824, 43.5161, 4.11935],
[3.45565, 27.1554, 3.86714],
[0.0299566, 0.184309, 1.46709]]).T
@staticmethod
def lms_to_rgb():
return np.array([[0.0809, -0.1305, 0.1167],
[-0.0102, 0.0540, -0.1136],
[-0.0004, -0.0041, 0.6935]]).T
@staticmethod
def lms_protanopia(degree: float = 1.0):
return np.array([[1 - degree, 2.02344 * degree, -2.52581 * degree],
[0, 1, 0],
[0, 0, 1]]).T
@staticmethod
def lms_deutranopia(degree: float = 1.0):
return np.array([[1, 0, 0],
[0.494207 * degree, 1 - degree, 1.24827 * degree],
[0, 0, 1]]).T
@staticmethod
def lms_tritanopia(degree: float = 1.0):
return np.array([[1, 0, 0],
[0, 1, 0],
[-0.395913 * degree, 0.801109 * degree, 1 - degree]]).T
@staticmethod
def hybrid_protanomaly_deuteranomaly(degree_p: float = 1.0, degree_d: float = 1.0):
return np.array([[1 - degree_p, 2.02344 * degree_p, -2.52581 * degree_p],
[0.494207 * degree_d, 1 - degree_d, 1.24827 * degree_d],
[0, 0, 1]]).T
@staticmethod
def correction_matrix(protanopia_degree, deutranopia_degree):
return np.array([[1 - deutranopia_degree/2, deutranopia_degree/2, 0],
[protanopia_degree/2, 1 - protanopia_degree/2, 0],
[protanopia_degree/4, deutranopia_degree/4, 1 - (protanopia_degree + deutranopia_degree)/4]]).T
class Utils:
@staticmethod
def load_rgb(path):
img_rgb = np.array(Image.open(path)) / 255
return img_rgb
@staticmethod
def load_lms(path):
img_rgb = np.array(Image.open(path)) / 255
img_lms = np.dot(img_rgb[:,:,:3], Transforms.rgb_to_lms())
return img_lms