Dieses Projekt dient der digitalen Umwandlung von Bildern in simulierte Darstellung aus Sicht eines rot-grün-blinden Menschen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Farbaenderung.py 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import numpy as np
  2. import cv2
  3. def createGammaLookup():
  4. return np.array([removeGammaCorrection(i) for i in np.arange(0, 256)]).astype("float64")
  5. def createReverseGammaLookup():
  6. return np.array([applyGammaCorrection(i/255) for i in np.arange(0.0, 256.0)]).astype("uint8")
  7. def removeGammaCorrection(v):
  8. if v <= 0.04045 * 255:
  9. return (v / 255) / 12.92
  10. elif v > 0.04045 * 255:
  11. return (((v / 255) + 0.055) / 1.055) ** 2.4
  12. def applyGammaCorrection(v_reverse):
  13. if abs(v_reverse) <= 0.0031308:
  14. return round(255 * (12.92 * abs(v_reverse)))
  15. elif abs(v_reverse) > 0.0031308:
  16. if (round(255 * (1.055 * abs(v_reverse) ** 0.41666 - 0.055))) > 255:
  17. return 255 - (round(255 * (1.055 * abs(v_reverse) ** 0.41666 - 0.055)) - 255)
  18. else:
  19. return round(255 * (1.055 * abs(v_reverse) ** 0.41666 - 0.055))
  20. class Dyschromasie:
  21. cb_image = np.array([]).astype('float64')
  22. sim_image = np.array([]).astype('uint8')
  23. def __init__(self, img_mat=np.array([]), rows=0, cols=0, kanaele=0, sim_faktor=0, sim_kind='d'):
  24. self.rows = rows
  25. self.cols = cols
  26. self.kanaele = kanaele
  27. self.img_mat = img_mat
  28. self.sim_faktor = sim_faktor
  29. self.sim_kind = sim_kind
  30. T = np.array([[0.31399022, 0.63951294, 0.04649755],
  31. [0.15537241, 0.75789446, 0.08670142],
  32. [0.01775239, 0.10944209, 0.87256922]])
  33. T_reversed = np.array([[5.47221206, -4.6419601, 0.16963708],
  34. [-1.1252419, 2.29317094, -0.1678952],
  35. [0.02980165, -0.19318073, 1.16364789]])
  36. def Simulate(self):
  37. removeGammaCorrectionLUT = createGammaLookup()
  38. if self.sim_kind == 'p':
  39. sim_mat = np.array([[(1 - self.sim_faktor), 1.05118294 * self.sim_faktor, -0.05116099 * self.sim_faktor],
  40. [0, 1, 0],
  41. [0, 0, 1]])
  42. elif self.sim_kind == 'd':
  43. sim_mat = np.array([[1, 0, 0],
  44. [0.9513092 * self.sim_faktor, (1 - self.sim_faktor), 0.04866992 * self.sim_faktor],
  45. [0, 0, 1]])
  46. else:
  47. sim_mat = np.array([[1, 0, 0],
  48. [0, 1, 0],
  49. [-0.86744736 * self.sim_faktor, 1.86727089 * self.sim_faktor, (1 - self.sim_faktor)]])
  50. # Gammakorrektur durchfuehren
  51. self.cb_image = np.copy(self.img_mat).astype('float64')
  52. self.cb_image = cv2.LUT(self.img_mat, removeGammaCorrectionLUT)
  53. rechen_Mat = np.copy(self.T_reversed.dot(sim_mat).dot(self.T))
  54. # Einzelne Pixelwertberechnung
  55. for i in range(self.rows):
  56. for j in range(self.cols):
  57. self.cb_image[i, j] = rechen_Mat.dot(self.cb_image[i, j])
  58. self.sim_image = np.copy(self.cb_image)
  59. self.sim_image = self.sim_image.astype('uint8')
  60. # Rücktransformation der Gammawerte
  61. for i in range(self.rows):
  62. for j in range(self.cols):
  63. for x in range(3):
  64. self.sim_image[i, j, x] = applyGammaCorrection(self.cb_image[i, j, x])
  65. return self.sim_image