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.1KB

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