Der Dalt Algorithmus wird nun von dem Dalt-Button ausgeführt. Gleichzeitig wurde die Funktion showImage geschrieben, um Code zu verschlanken und lediglich die Switchfunktionen simulate und daltonize für die Auswahl der Simulationsart zu nutzen. PS: Mehrere Bilder zum Testen eingefügtmaster
rows, cols, kanaele = img.shape | rows, cols, kanaele = img.shape | ||||
def showImage(img, rows, cols, kanaele, sim_faktor, sim_kind, sim_art): | |||||
color_blindness = Dyschromasie(img, rows, cols, kanaele, sim_faktor, sim_kind) | |||||
if sim_art == 'Sim': | |||||
color_blindness_mat = np.copy(color_blindness.Simulate()).astype('uint8') | |||||
else: | |||||
color_blindness_mat = np.copy(color_blindness.daltonize()).astype('uint8') | |||||
T = tk.Text(SB.frame, height=1, width=15) | |||||
T.grid(columnspan=5) | |||||
print_string = sim_kind + ' ' + str(round(color_blindness.sim_faktor * 100)) + "%)" | |||||
T.insert('current', print_string) | |||||
conv_SimulationPic = ImageTk.PhotoImage(image=PIL.Image.fromarray(color_blindness_mat)) | |||||
sim_pic = tk.Label(SB.frame, image=conv_SimulationPic) | |||||
sim_pic.Image = conv_SimulationPic | |||||
sim_pic.grid(columnspan=5) | |||||
def simulate(): | def simulate(): | ||||
global img, rows, cols, kanaele, sim_pro, sim_deut, sim_tri | global img, rows, cols, kanaele, sim_pro, sim_deut, sim_tri | ||||
if sim_deut.get(): | if sim_deut.get(): | ||||
d = Dyschromasie(img, rows, cols, kanaele, simGrad.get() / 100, 'd') | |||||
display_array_deut = np.copy(d.Simulate()).astype('uint8') | |||||
T = tk.Text(SB.frame, height=1, width=15) | |||||
T.grid(columnspan=5) | |||||
print_string = "Deutranop(" + str(round(d.sim_faktor * 100)) + "%)" | |||||
T.insert('current', print_string) | |||||
conv_SimulationPic_deut = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_deut)) | |||||
sim_pic_deut = tk.Label(SB.frame, image=conv_SimulationPic_deut) | |||||
sim_pic_deut.Image = conv_SimulationPic_deut | |||||
sim_pic_deut.grid(columnspan=5) | |||||
showImage(img, rows, cols, kanaele, simGrad.get()/100, 'Deuteranop', 'Sim') | |||||
elif sim_tri.get(): | elif sim_tri.get(): | ||||
t = Dyschromasie(img, rows, cols, kanaele, simGrad.get() / 100, 't') | |||||
display_array_tri = np.copy(t.Simulate()).astype('uint8') | |||||
T = tk.Text(SB.frame, height=1, width=15) | |||||
T.grid(columnspan=5) | |||||
print_string = "Tritanop(" + str(round(t.sim_faktor * 100)) + "%)" | |||||
T.insert('current', print_string) | |||||
conv_SimulationPic_tri = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_tri)) | |||||
sim_pic_tri = tk.Label(SB.frame, image=conv_SimulationPic_tri) | |||||
sim_pic_tri.Image = conv_SimulationPic_tri | |||||
sim_pic_tri.grid(columnspan=5) | |||||
showImage(img, rows, cols, kanaele, simGrad.get() / 100, 'Tritanop', 'Sim') | |||||
elif sim_pro.get(): | elif sim_pro.get(): | ||||
p = Dyschromasie(img, rows, cols, kanaele, simGrad.get() / 100, 'p') | |||||
display_array_pro = np.copy(p.Simulate()).astype('uint8') | |||||
showImage(img, rows, cols, kanaele, simGrad.get() / 100, 'Protanop', 'Sim') | |||||
T = tk.Text(SB.frame, height=1, width=15) | |||||
T.grid(columnspan=5) | |||||
print_string = "Protanop(" + str(round(p.sim_faktor * 100)) + "%)" | |||||
T.insert('current', print_string) | |||||
conv_SimulationPic_pro = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_pro)) | |||||
sim_pic_pro = tk.Label(SB.frame, image=conv_SimulationPic_pro) | |||||
sim_pic_pro.Image = conv_SimulationPic_pro | |||||
sim_pic_pro.grid(columnspan=5) | |||||
def daltonize(): | |||||
global img, rows, cols, kanaele, sim_pro, sim_deut, sim_tri | |||||
if sim_deut.get(): | |||||
showImage(img, rows, cols, kanaele, simGrad.get()/100, 'Deuteranop', 'Dalt') | |||||
elif sim_tri.get(): | |||||
showImage(img, rows, cols, kanaele, simGrad.get() / 100, 'Tritanop', 'Dalt') | |||||
def daltonize(): | |||||
#ToDo | |||||
print("working") | |||||
elif sim_pro.get(): | |||||
showImage(img, rows, cols, kanaele, simGrad.get() / 100, 'Protanop', 'Dalt') | |||||
browseButton = tk.Button(SB.frame, text="Browse", width=25, command=browse, bg='light blue') | browseButton = tk.Button(SB.frame, text="Browse", width=25, command=browse, bg='light blue') |
cb_image = np.array([]).astype('float64') | cb_image = np.array([]).astype('float64') | ||||
sim_image = np.array([]).astype('int64') | sim_image = np.array([]).astype('int64') | ||||
def __init__(self, img_mat=np.array([]), rows=0, cols=0, kanaele=0, sim_faktor=0, sim_kind='d'): | |||||
def __init__(self, img_mat=np.array([]), rows=0, cols=0, kanaele=0, sim_faktor=0, sim_kind='Deuteranop'): | |||||
self.rows = rows | self.rows = rows | ||||
self.cols = cols | self.cols = cols | ||||
self.kanaele = kanaele | self.kanaele = kanaele | ||||
def Simulate(self): | def Simulate(self): | ||||
removeGammaCorrectionLUT = createGammaLookup() | removeGammaCorrectionLUT = createGammaLookup() | ||||
if self.sim_kind == 'p': | |||||
if self.sim_kind == 'Protanop': | |||||
sim_mat = np.array([[(1 - self.sim_faktor), 1.05118294 * self.sim_faktor, -0.05116099 * self.sim_faktor], | sim_mat = np.array([[(1 - self.sim_faktor), 1.05118294 * self.sim_faktor, -0.05116099 * self.sim_faktor], | ||||
[0, 1, 0], | [0, 1, 0], | ||||
[0, 0, 1]]) | [0, 0, 1]]) | ||||
elif self.sim_kind == 'd': | |||||
elif self.sim_kind == 'Deuteranop': | |||||
sim_mat = np.array([[1, 0, 0], | sim_mat = np.array([[1, 0, 0], | ||||
[0.9513092 * self.sim_faktor, (1 - self.sim_faktor), 0.04866992 * self.sim_faktor], | [0.9513092 * self.sim_faktor, (1 - self.sim_faktor), 0.04866992 * self.sim_faktor], | ||||
[0, 0, 1]]) | [0, 0, 1]]) | ||||
else: | |||||
elif self.sim_kind == 'Tritanop': | |||||
sim_mat = np.array([[1, 0, 0], | sim_mat = np.array([[1, 0, 0], | ||||
[0, 1, 0], | [0, 1, 0], | ||||
[-0.86744736 * self.sim_faktor, 1.86727089 * self.sim_faktor, (1 - self.sim_faktor)]]) | [-0.86744736 * self.sim_faktor, 1.86727089 * self.sim_faktor, (1 - self.sim_faktor)]]) | ||||
for x in range(3): | for x in range(3): | ||||
E[i, j, x] = abs(int(self.img_mat[i, j, x]) - int(simulated_image[i, j, x])) | E[i, j, x] = abs(int(self.img_mat[i, j, x]) - int(simulated_image[i, j, x])) | ||||
ERR = np.zeros_like(image).astype('int32') | |||||
ERR = np.zeros_like(self.img_mat).astype('int32') | |||||
for i in range(self.rows): | for i in range(self.rows): | ||||
for j in range(self.cols): | for j in range(self.cols): | ||||
err = E[i, j, :3] | err = E[i, j, :3] | ||||
ERR[i, j, :3] = np.dot(err2mod, err) | ERR[i, j, :3] = np.dot(err2mod, err) | ||||
dtpn = np.copy(image).astype('int32') | |||||
dtpn = np.copy(self.img_mat).astype('int32') | |||||
for i in range(self.rows): | for i in range(self.rows): | ||||
for j in range(self.cols): | for j in range(self.cols): | ||||
for x in range(3): | for x in range(3): | ||||
dtpn[i, j, x] = abs(int(ERR[i, j, x]) + int(image[i, j, x])) | |||||
dtpn[i, j, x] = abs(int(ERR[i, j, x]) + int(self.img_mat[i, j, x])) | |||||
for i in range(rows): | |||||
for j in range(cols): | |||||
for i in range(self.rows): | |||||
for j in range(self.cols): | |||||
dtpn[i, j, 0] = max(0, dtpn[i, j, 0]) | dtpn[i, j, 0] = max(0, dtpn[i, j, 0]) | ||||
dtpn[i, j, 0] = min(255, dtpn[i, j, 0]) | dtpn[i, j, 0] = min(255, dtpn[i, j, 0]) | ||||
dtpn[i, j, 1] = max(0, dtpn[i, j, 1]) | dtpn[i, j, 1] = max(0, dtpn[i, j, 1]) | ||||
dalt = Dyschromasie(result, self.rows, self.cols, self.kanaele, self.sim_faktor, self.sim_kind) | dalt = Dyschromasie(result, self.rows, self.cols, self.kanaele, self.sim_faktor, self.sim_kind) | ||||
return dalt.Simulate() | return dalt.Simulate() | ||||
def main(): | |||||
script_dir = sys.path[0] | |||||
path = script_dir[:-4] + r'Beispielbilder\rot-gruen-schwaeche-test-bild.jpg' | |||||
image = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB) | |||||
rows, cols, kanaele = image.shape | |||||
dalt = Dyschromasie(image, rows, cols, kanaele, 1, 'p') | |||||
cv2.imshow('Dalt_Img', cv2.cvtColor(dalt.daltonize(), cv2.COLOR_RGB2BGR)) | |||||
cv2.waitKey(0) | |||||
# main() |