Projekt_Dyschromasie/Code/Dyschromasie.py
2020-08-15 09:18:51 +02:00

94 lines
3.4 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
from PIL import Image, ImageTk #Wichtig zum Anzeigen der Bilder im GUI
# Einlesen des Bildes
script_dir = sys.path[0]
path = script_dir[:-4] + "Beispielbilder\grocery_store.jpg"
image = cv2.cvtColor(cv2.imread(path),cv2.COLOR_BGR2RGB) # 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]])
S_p = 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]])
#Multiplikation der einzelnen Pixel
for i in range(rows):
for j in range(cols):
cb_image[i,j] = T_reversed.dot(S_p).dot(T).dot(cb_image[i,j])
sim_image = np.copy(cb_image)
sim_image = sim_image.astype('uint8')
#Rücktransformation der Gammawerte
for i in range(rows):
for j in range(cols):
for x in range(3):
sim_image[i, j, x] = reverseGammaCorrection(cb_image[i, j, x])
cv2.namedWindow("Display") # Displaywindow erstellen
cv2.imshow("Display", cv2.cvtColor(sim_image,cv2.COLOR_RGB2BGR)) # Bild zeigen
cv2.waitKey(0) # Fenster offen halten