ContrastControl.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. from . import Processing
  4. from . import ColorSpaceTransform
  5. from .. import image
  6. import colour, copy
  7. import skimage, skimage.morphology, skimage.color, skimage.util, skimage.filters
  8. import numpy as np
  9. # ------------------------------------------------------------------------------------------
  10. # MIAM project 2020
  11. # ------------------------------------------------------------------------------------------
  12. # author: remi.cozot@univ-littoral.fr
  13. # ------------------------------------------------------------------------------------------
  14. class ContrastControl(Processing.Processing):
  15. """description of class"""
  16. def __init__(self):
  17. pass
  18. def compute(self, img, **kwargs):
  19. res = copy.deepcopy(img)
  20. if kwargs:
  21. if 'method' in kwargs:
  22. if kwargs['method'] == 'stretch':
  23. if 'range' in kwargs:
  24. range = kwargs['range']
  25. else:
  26. range = (2,98)
  27. p2, p98 = np.percentile(res.colorData, (2, 98))
  28. res.colorData = skimage.exposure.rescale_intensity(res.colorData, in_range=(p2, p98))
  29. elif kwargs['method'] == 'globalEqualization':
  30. res.colorData = skimage.exposure.equalize_hist(res.colorData)
  31. elif kwargs['method'] == 'adaptativeEqualization':
  32. if 'limit' in kwargs:
  33. limit = kwargs['limit']
  34. else:
  35. limit =0.03
  36. res.colorData = skimage.exposure.equalize_adapthist(res.colorData, clip_limit=limit)
  37. elif kwargs['method'] == 'localEqualization':
  38. if 'size' in kwargs:
  39. size = kwargs['size']
  40. else:
  41. size =min(img.shape[0],img.shape[1])
  42. #print("ContrastControl.compute():'method'=localEqualization, size=",size)
  43. yuv = skimage.color.rgb2yuv(res.colorData)
  44. y256 = skimage.util.img_as_ubyte(yuv[:,:,0])
  45. # equalization with skimage
  46. selem = skimage.morphology.disk(size)
  47. y256_eq = skimage.filters.rank.equalize(y256, selem=selem)
  48. y_eq = skimage.util.img_as_float(y256_eq)
  49. # transfert new y
  50. yuv_new = copy.deepcopy(yuv)
  51. y_new = yuv_new[:,:,0]
  52. u_new = yuv_new[:,:,1]
  53. v_new = yuv_new[:,:,2]
  54. yuv_old = copy.deepcopy(yuv)
  55. y_old = yuv_old[:,:,0]
  56. u_old = yuv_old[:,:,1]
  57. v_old = yuv_old[:,:,2]
  58. y_new = y_eq
  59. y_positive = y_old>0
  60. u_new[y_positive] = y_eq[y_positive]*u_old[y_positive]/y_old[y_positive]
  61. v_new[y_positive] = y_eq[y_positive]*v_old[y_positive]/y_old[y_positive]
  62. yuv_new[:,:,0] = y_new
  63. yuv_new[:,:,1] = u_new
  64. yuv_new[:,:,2] = v_new
  65. # go back to rgb
  66. newRGB = skimage.color.yuv2rgb(yuv_new)
  67. newRGB[newRGB>1] =1
  68. newRGB[newRGB<0] = 0
  69. res.colorData = newRGB
  70. pass
  71. return res