MaskSegmentPercentile.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. from . import Processing
  4. from . import ColorSpaceTransform
  5. from .. import image
  6. import colour, copy, skimage, functools
  7. import numpy as np
  8. # ------------------------------------------------------------------------------------------
  9. # MIAM project 2020
  10. # ------------------------------------------------------------------------------------------
  11. # author: remi.cozot@univ-littoral.fr
  12. # ------------------------------------------------------------------------------------------
  13. class MaskSegmentPercentile(Processing.Processing):
  14. """description of class"""
  15. def __init__(self):
  16. pass
  17. def compute(self, img, **kwargs):
  18. """
  19. MaskSegmentPercentile(img, {'percent':[percentileValue], 'channel':'Y'})
  20. """
  21. # kwargs
  22. # percentile values
  23. percentileValues = [0,50,100] # default value
  24. if kwargs and ('percent' in kwargs) : percentileValues = kwargs['percent']
  25. # percentile values
  26. channel = 'Y' if img.isHDR() else 'L' # default value
  27. if kwargs and ('channel' in kwargs) : channel = kwargs['channel']
  28. res = []
  29. for pValmin,pValmax in zip(percentileValues, percentileValues[1:]):
  30. seg = copy.deepcopy(img)
  31. seg.addMask(one=False)
  32. # getChannel
  33. c = seg.getChannel(image.channel.channel.toChannel(channel))
  34. pMin, pMax = np.percentile(c, (pValmin, pValmax))
  35. seg.mask[(c>= pMin) & (c<=pMax) ] = 1
  36. res.append(seg)
  37. return res