-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_models.py
110 lines (75 loc) · 2.84 KB
/
color_models.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
110
# color_models.py
import math
import numpy as np
from skimage.util import img_as_float
from skimage.util import img_as_ubyte
def rgb_to_grayscale(image):
gray_image = np.zeros((image.shape[0], image.shape[1]))
for i in range(image.shape[0]):
for j in range(image.shape[1]):
r = image[i, j, 0]
g = image[i, j, 1]
b = image[i, j, 2]
gray = (r + g + b) / 3.0
gray_image[i, j] = gray
return gray_image
def rgb_to_cmy(image):
image = img_as_float(image)
cmk_image = np.zeros(image.shape)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
cmk = 1 - image[i, j, :]
cmk_image[i, j, :] = cmk
cmk_image = img_as_ubyte(cmk_image)
return cmk_image
def rgb_to_hsi(image):
image = img_as_float(image)
hsi_image = np.zeros(image.shape)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
r, g, b = image[i, j, :]
# epsilon to avoid a division by zero
ep = 0.000001
intensity = (r + g + b) / 3.0
saturation = 1 - ((3 * min([r, g, b])) / (r + g + b))
rmg = r - g
rmb = r - b
gmb = g - b
w = ((rmg + rmb) * 0.5) / (math.sqrt((rmg ** 2) + (rmb * gmb)) + ep)
hue = math.acos(w)/math.pi
if b > g:
hue = 1 - (hue/(2*math.pi))
# print(theta)
hsi = np.array([hue, saturation, intensity])
# print(hsi)
hsi_image[i, j, :] = hsi
return hsi_image
def hsi_to_rgb(image):
rgb_image = np.zeros(image.shape)
for i in range(image.shape[0]):
for j in range(image.shape[1]):
hue = image[i, j, 0] * math.pi
sat = image[i, j, 1]
intens = image[i, j, 2]
if 0 <= hue < math.radians(120):
b = intens*(1 - sat)
r = intens*(1 + ((sat * math.cos(hue)) / math.cos(math.radians(60) - hue)))
g = (3 * intens) - (r + b)
rgb = np.array([r, g, b])
rgb_image[i, j, :] = rgb
elif math.radians(120) <= hue < math.radians(240):
hue -= math.radians(120)
r = intens * (1 - sat)
g = intens * (1 + ((sat * math.cos(hue)) / math.cos(math.radians(60) - hue)))
b = (3 * intens) - (r + g)
rgb = np.array([r, g, b])
rgb_image[i, j, :] = rgb
else:
hue -= math.radians(240)
g = intens * (1 - sat)
b = intens * (1 + ((sat * math.cos(hue)) / math.cos(math.radians(60) - hue)))
r = (3 * intens) - (g + b)
rgb = np.array([r, g, b])
rgb_image[i, j, :] = rgb
# print(rgb)
return rgb_image