HueEqualiser.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import cv2
  2. import numpy
  3. import Tool
  4. class HueEqualiser(Tool.Tool):
  5. def on_init(self):
  6. self.id = "hueequaliser"
  7. self.name = "Hue Equaliser"
  8. self.icon_path = "ui/PF2_Icons/HueEqualiser.png"
  9. self.properties = [
  10. Tool.Property("header", "Hue Equaliser", "Header", None, has_toggle=False, has_button=False),
  11. Tool.Property("bleed", "Hue Bleed", "Slider", 0.5, max=2.0, min=0.01),
  12. # Red
  13. Tool.Property("header_red", "Red", "Header", None, has_toggle=False, has_button=False),
  14. Tool.Property("red_value", "Value", "Slider", 0, max=50, min=-50),
  15. Tool.Property("red_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  16. # Yellow
  17. Tool.Property("header_yellow", "Yellow", "Header", None, has_toggle=False, has_button=False),
  18. Tool.Property("yellow_value", "Value", "Slider", 0, max=50, min=-50),
  19. Tool.Property("yellow_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  20. # Green
  21. Tool.Property("header_green", "Green", "Header", None, has_toggle=False, has_button=False),
  22. Tool.Property("green_value", "Value", "Slider", 0, max=50, min=-50),
  23. Tool.Property("green_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  24. # Cyan
  25. Tool.Property("header_cyan", "Cyan", "Header", None, has_toggle=False, has_button=False),
  26. Tool.Property("cyan_value", "Value", "Slider", 0, max=50, min=-50),
  27. Tool.Property("cyan_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  28. # Blue
  29. Tool.Property("header_blue", "Blue", "Header", None, has_toggle=False, has_button=False),
  30. Tool.Property("blue_value", "Value", "Slider", 0, max=50, min=-50),
  31. Tool.Property("blue_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  32. # Violet
  33. Tool.Property("header_violet", "Violet", "Header", None, has_toggle=False, has_button=False),
  34. Tool.Property("violet_value", "Value", "Slider", 0, max=50, min=-50),
  35. Tool.Property("violet_saturation", "Saturation", "Slider", 0, max=50, min=-50),
  36. ]
  37. def on_update(self, image):
  38. hues = {
  39. "red": 0,
  40. "yellow": 60,
  41. "green": 120,
  42. "cyan": 180,
  43. "blue": 240,
  44. "violet": 300,
  45. "_red": 360,
  46. }
  47. out = image
  48. if(not self.is_default()):
  49. bleed = self.props["bleed"].get_value()
  50. out = out.astype(numpy.float32)
  51. # Convert to HSV colorspace
  52. out = cv2.cvtColor(out, cv2.COLOR_BGR2HSV)
  53. # Bits per pixel
  54. bpp = float(str(image.dtype).replace("uint", "").replace("float", ""))
  55. # Pixel value range
  56. np = float(2 ** bpp - 1)
  57. imhue = out[0:, 0:, 0]
  58. imsat = out[0:, 0:, 1]
  59. imval = out[0:, 0:, 2]
  60. for hue in hues:
  61. hsat = self.props["%s_saturation" % hue.replace('_', '')].get_value()
  62. hval = self.props["%s_value" % hue.replace('_', '')].get_value()
  63. isHue = self._is_hue(imhue, hues[hue], (3.5/bleed))
  64. imsat = imsat + ((hsat / 10000) * 255) * isHue
  65. imval = imval + ((hval / 1000) * np) * isHue
  66. # Clip any values out of bounds
  67. imval[imval < 0.0] = 0.0
  68. imval[imval > np] = np
  69. imsat[imsat < 0.0] = 0.0
  70. imsat[imsat > 1.0] = 1.0
  71. out[0:, 0:, 1] = imsat
  72. out[0:, 0:, 2] = imval
  73. # Convert back to BGR colorspace
  74. out = cv2.cvtColor(out, cv2.COLOR_HSV2BGR)
  75. out = out.astype(image.dtype)
  76. return out
  77. def _is_hue(self, image, hue_value, bleed_value = 3.5):
  78. mif = hue_value - 30
  79. mir = hue_value + 30
  80. if (mir > 360):
  81. mir = 360
  82. if (mif < 0):
  83. mif = 0
  84. bleed = float(360 / bleed_value)
  85. icopy = image.copy()
  86. print(bleed, mif, mir)
  87. if(mif != 0):
  88. icopy[icopy < mif - bleed] = 0.0
  89. icopy[icopy > mir + bleed] = 0.0
  90. icopy[(icopy < mif) * (icopy != 0.0)] = (((mif - (icopy[(icopy < mif) * (icopy != 0.0)]))/360.0) / (bleed/360.0)) * -1 + 1
  91. icopy[(icopy > mir) * (icopy != 0.0)] = ((((icopy[(icopy > mir) * (icopy != 0.0)]) - mir)/360.0) / (bleed/360.0)) * -1 + 1
  92. icopy[(icopy >= mif) * (icopy <= mir)] = 1.0
  93. if(mif == 0):
  94. icopy[icopy > mir + bleed] = 0.0
  95. icopy[(icopy > mir) * (icopy != 0.0)] = ((((icopy[(icopy > mir) * (icopy != 0.0)]) - mir) / 360.0) / (bleed/360.0)) * -1 + 1
  96. return icopy