Projekt_Dyschromasie/Code/Dyschromasie.py
Max Sponsel c3d4a04017 Simulationsmatrizen der einzelnen Fehlsichtigkeiten implementiert
Simulationsmatrizen fuer Protanopie, Deuteranopie und Tritanopie hinzugefügt.
2020-06-27 10:40:18 +02:00

92 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2 # OpenCV fuer Bildbearbeitung
import tkinter # Zum Erstellen von GUIs
import numpy as np # Numpy Import
import sys
# Einlesen des Bildes
script_dir = sys.path[0]
path = script_dir[:-4] + "Beispielbilder\lena.jpg"
image = cv2.imread(path) # Einlesen des Bildes (noch hardcodiert, sollte dann in GUI gehen)
rows = image.shape[0] # Auslesen der Zeilenanzahl
cols = image.shape[1] # Auslesen der Spaltenanzahl
kanaele = image.shape[2] # Auslesen der Kanaele (3 fuer RGB, 1 fuer Graubild)
def gammaCorrection(v):
if v <= 0.04045 * 255:
return float(((v / 255) / 12.92))
elif v > 0.04045 * 255:
return float((((v / 255) + 0.055) / 1.055) ** 2.4)
else:
print("Ungültiger Wert!!")
return 1
def reverseGammaCorrection(v_reverse):
if v_reverse <= 0.0031308:
return int(255 * (12.92 * v_reverse))
elif v_reverse > 0.0031308:
return int(255 * (1.055 * v_reverse ** 0.41666 - 0.055))
else:
print("Ungültiger Wert!!!")
return 1
cb_image = np.copy(image) # Kopie des Bildarrays
cb_image = cb_image.astype('float64') # Casting des Arrays auf Float
# Korrektur des Gamma Faktors für alle Bildelemente
for i in range(rows):
for j in range(cols):
for x in range(3):
cb_image[i, j, x] = gammaCorrection(float(image[i, j, x]))
'''
0.31399022 0.63951294 0.04649755 Transformationsmatrix zum Konvertieren vom linearen RGB zum LMS Farbraum
T = 0.15537241 0.75789446 0.08670142 Multiplikation aus Brucelindbloom und Hunt-Pointer-Estevez Matrixen
0.01775239 0.10944209 0.87256922 T*RGB_Farbverktor = LMS_Farbvektor
'''
T = np.array([[0.31399022, 0.63951294, 0.04649755],
[0.15537241, 0.75789446, 0.08670142],
[0.01775239, 0.10944209, 0.87256922]])
'''
5.47221206 4.6419601 0.16963708 Rücktransformationsmatrix (Inverse von T)
T_reversed = -1.1252419 2.29317094 0.1678952 T_reversed Ü LMS_Farbvektor = RBG_Farbvektor
0.02980165 0.19318073 1.16364789
'''
T_reversed = np.array([[5.47221206, -4.6419601, 0.16963708],
[-1.1252419, 2.29317094, -0.1678952],
[0.02980165, -0.19318073, 1.16364789]])
# Multiplikation der einzelnen Pixelwerte
for i in range(rows):
for j in range(cols):
cb_image[i, j] = T.dot(cb_image[i, j])
# Simulationsmatrizen fuer Protanop
S_b = np.array([[0, 1.05118294, -0.05116099], #Simulationsmatrix fuer Protanopie
[0, 1, 0],
[0, 0, 1]])
S_d = np.array([[1, 0, 0], #Simulationsmatrix fuer Deuteranopie
[0.9513092, 0, 0.04866992],
[0, 0, 1]])
S_t = np.array([[1, 0, 0], #Simulationsmatrix fuer Tritanopie
[0, 1, 0],
[-0.86744736, 1.86727089, 0]])
#choice = input("Bitte geben Sie den Typ der zu simulierenden Farbblindheit an:(B,D,T)")
cv2.namedWindow("Display") # Displaywindow erstellen
cv2.imshow("Display", image) # Bild zeigen
cv2.waitKey(0) # Fenster offen halten