ExposureControl.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. from . import Processing, Ymap
  4. from . import ColorSpaceTransform
  5. from .. import image
  6. import colour, copy, skimage, math
  7. import numpy as np
  8. # ------------------------------------------------------------------------------------------
  9. # MIAM project 2020
  10. # ------------------------------------------------------------------------------------------
  11. # author: remi.cozot@univ-littoral.fr
  12. # ------------------------------------------------------------------------------------------
  13. class ExposureControl(Processing.Processing):
  14. """description of class"""
  15. def __init__(self):
  16. pass
  17. def compute(self, img, **kwargs):
  18. """
  19. Linear tone mapping operator
  20. @params:
  21. img - Required : hdr image (miam.image.Image)
  22. kwargs - Optionnal : optionnal parameter (dict)
  23. 'min' : minimum value under which output is zero (float)
  24. 'max' : maximum value above which output is one (float)
  25. """
  26. minValue = 0.0
  27. maxValue = 1.0
  28. if not kwargs: kwargs = {'auto': True, 'target': 0.5, 'EV': 0} # default value
  29. # auto
  30. if 'auto' in kwargs:
  31. # auto or manual mode
  32. auto = kwargs['auto']
  33. else:
  34. # if not auto in kwargs -> manual mode
  35. auto = False
  36. # target or EV value
  37. if auto:
  38. # auto: target value
  39. if 'target' in kwargs:
  40. target = kwargs['target']
  41. else:
  42. target = 0.5
  43. # EV value
  44. if 'EV' in kwargs:
  45. EV = kwargs['EV']
  46. else:
  47. EV = 0
  48. res = copy.deepcopy(img)
  49. rgbIn = res.colorData
  50. if auto:
  51. # compute mean Y (Luminance)
  52. y = res.getChannel(image.channel.channel.Y)
  53. if res.hasMask():
  54. if res.binaryMask():
  55. # DEBUG
  56. print("ExposureControl.compute(): BINARY mask taken into account")
  57. ymean = np.mean(y[res.mask==1])
  58. else:
  59. # DEBUG
  60. print("ExposureControl.compute(): NON BINARY mask taken into account")
  61. ymean = np.mean(y*res.mask[...,np.newaxis])
  62. else: # no mask
  63. ymean = np.mean(y)
  64. rgbOut = rgbIn*(target/ymean)*math.pow(2,EV)
  65. else:
  66. rgbOut = rgbIn*math.pow(2,EV)
  67. # clip if SDR
  68. if (img.type == image.imageType.imageType.SDR):
  69. rgbOut[rgbOut>=1.0] = 1.0
  70. rgbOut[rgbOut<=0.0] = 0.0
  71. # update attributes
  72. res.colorData = rgbOut
  73. return res