-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolour_blindness.py
109 lines (69 loc) · 3.22 KB
/
colour_blindness.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import numpy as np
from PIL import Image
import sys
class ColourBlindness(object):
def __init__(self,
image,
phosphors = './phosphors.dat',
fundamentals = './2deg_StockmanSharpe.csv'):
self.image = Image.open(image)
wv1, self.phosphors = self.get_phosphors(phosphors)
wv2, self.fundamentals = self.get_fundamentals(fundamentals)
self.gamma = np.array([2.38, 2.30, 2.17])
self.RGB_LMS = np.dot(self.fundamentals.T, self.phosphors)
self.LMS_RGB = np.linalg.inv(self.RGB_LMS)
# blue = self.rgb2lms([0,0,1])
blue = np.array([0.54, 0.21, 0.79])
white = self.rgb2lms([1,1,1])
self.a = white[1] * blue[2] - blue[1] * white[2]
self.b = white[2] * blue[0] - blue[2] * white[0]
self.c = white[0] * blue[1] - blue[0] * white[1]
def get_phosphors(self, filename):
with open(filename, 'rb') as f:
lines = [l.rstrip().split('\t') for l in f][33:]
lines = np.array( [map(float, l) for l in lines] )
wavelengths = lines[:,0]
power = lines[:,1:]
mask = np.logical_and(390 <= wavelengths, wavelengths <= 780)
return wavelengths, power[mask, :]
def get_fundamentals(self, filename):
with open(filename, 'rb') as f:
lines = [l.rstrip().split(',') for l in f]
lines = np.array( [map(float, l) for l in lines] )
wavelengths = lines[:,0]
power = lines[:,1:]
mask = np.logical_and(390 <= wavelengths, wavelengths <= 780)
return wavelengths, power[mask, :]
def rgb2lms(self, rgb):
lms = np.dot(self.RGB_LMS, rgb)
return lms
def lms2rgb(self, lms):
rgb = np.dot(self.LMS_RGB, lms)
return rgb
def protanopia(self):
rgb_data = (np.array(self.image) / 255.0) ** self.gamma
lms_data = np.apply_along_axis(self.rgb2lms, 2, rgb_data)
# Apply Eqs.(9)
lms_data[:,:,0] = -(self.b * lms_data[:,:,1] + self.c * lms_data[:,:,2]) / self.a
rgb_protan = np.apply_along_axis(self.lms2rgb, 2, lms_data)
rgb_final = 255 * rgb_protan**(1.0/self.gamma)
rgb_final = np.around(rgb_final, 0)
return Image.fromarray(rgb_final.astype(np.uint8))
def deuteranopia(self):
rgb_data = (np.array(self.image) / 255.0) ** self.gamma
lms_data = np.apply_along_axis(self.rgb2lms, 2, rgb_data)
# Apply Eqs.(10)
lms_data[:,:,1] = -(self.a * lms_data[:,:,0] + self.c * lms_data[:,:,2]) / self.b
rgb_protan = np.apply_along_axis(self.lms2rgb, 2, lms_data)
rgb_final = 255 * rgb_protan**(1.0/self.gamma)
rgb_final = np.around(rgb_final, 0)
return Image.fromarray(rgb_final.astype(np.uint8))
def tritanopia(self):
rgb_data = (np.array(self.image) / 255.0) ** self.gamma
lms_data = np.apply_along_axis(self.rgb2lms, 2, rgb_data)
# Apply Eqs.(11)
lms_data[:,:,2] = -(self.a * lms_data[:,:,0] + self.b * lms_data[:,:,1]) / self.c
rgb_protan = np.apply_along_axis(self.lms2rgb, 2, lms_data)
rgb_final = 255 * rgb_protan**(1.0/self.gamma)
rgb_final = np.around(rgb_final, 0)
return Image.fromarray(rgb_final.astype(np.uint8))