-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaletteGen.py
145 lines (108 loc) · 3.16 KB
/
PaletteGen.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import cv2
import numpy as np
#DICTIONARY TONALITA'
COLOR_HUES = {
"Orange": [0,15],
"Yellow": [15,30],
"Lime": [30,45],
"Green": [45,60],
"Mint Green": [60,75],
"Acquamarine": [75,90],
"Blue": [90,105],
"Indigo": [105,120],
"Violet": [120,135],
"Purple": [135,150],
"Pink": [150,165],
"Red": [165,180]
}
COLOR_SAT = {
"1": [0,102],
#"2": [51,102],
"3": [102,153],
#"4": [153,204],
"5": [153,255],
}
# DICTIONARY SCHEMA BRIGHTNESS
COLOR_VAL = {
"1": [0,51],
"2": [51,102],
"3": [102,153],
"4": [153,204],
"5": [204,255],
}
def hue_present(img, hue):
total = img.shape[0] * img.shape[1]
(H, S, V) = cv2.split(img)
hist = cv2.calcHist([H],[0],None,[180],[0,180])
if((hist[hue[0]:hue[1]].max() / total) > 0.0025):
return True
else:
return False
def sat_present(img, sat):
total = img.shape[0] * img.shape[1]
(H, S, V) = cv2.split(img)
hist = cv2.calcHist([S],[0],None,[256],[0,255])
if((hist[sat[0]:sat[1]].max() / total) > 0.008):
return True
else:
return False
def val_present(img, val):
total = img.shape[0] * img.shape[1]
(H, S, V) = cv2.split(img)
hist = cv2.calcHist([V],[0],None,[256],[0,255])
if((hist[val[0]:val[1]].max() / total) > 0.008):
return True
else:
return False
def hue_counter(img):
k = 0
for key in COLOR_HUES:
#print(key, hue_present(img, COLOR_HUES[key]))
if(hue_present(img, COLOR_HUES[key])):
k += 1
#print('Numero cluster hue:', k)
return k
def sat_counter(img):
k = 0
for key in COLOR_SAT:
#print(key, sat_present(img, COLOR_SAT[key]))
if(sat_present(img, COLOR_SAT[key])):
k += 1
#print('Numero cluster saturation:', k)
return k
def val_counter(img):
k = 0
for key in COLOR_VAL:
#print(key, val_present(img, COLOR_VAL[key]))
if(val_present(img, COLOR_VAL[key])):
k += 1
#print('Numero cluster val:', k)
return k
def k_means(img, k):
shape = img.shape
if(img.ndim == 3):
img = np.float32(img).reshape((-1, 3))
#Per ottimizzare il processo rispetto ai valori da documentazione scelgo un criterio con Halt=15, epsilon=0.01 e 5 tentativi
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 15, 0.01)
ret, label, center = cv2.kmeans(img, k, None, criteria, 5, cv2.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
#print(label.ravel())
result = result.reshape(shape)
return result
def my_kmeans(img):
hue_factor = 2
img_hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
num_colors = np.ceil(hue_counter(img_hsv)*hue_factor)
num_colors = int(num_colors)
#print("Num colors:", num_colors)
img_rgb_out= k_means(img, num_colors)
return img_rgb_out
def palette_extractor(img):
#Applicazione dell'algoritmo di clustering
img_out_rgb = my_kmeans(img)
#Estraggo i colori unici dall'immagine
img_flatten = img_out_rgb.reshape(img_out_rgb.shape[0]*img_out_rgb.shape[1], 3)
color_unique = np.unique(img_flatten, axis=0)
#Ottengo una palette di colori RGB in uscita
return color_unique