SumSquaredLaplace.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. from . import Processing, LaplaceFilter, GaussianFilter
  4. from .. import image
  5. import colour, copy
  6. from scipy import ndimage
  7. import numpy as np
  8. # ------------------------------------------------------------------------------------------
  9. # MIAM project 2020
  10. # ------------------------------------------------------------------------------------------
  11. # author: remi.cozot@univ-littoral.fr
  12. # ------------------------------------------------------------------------------------------
  13. class SumSquaredLaplace(Processing.Processing):
  14. """description of class"""
  15. def __init__(self):
  16. pass
  17. def compute(self, img, **kwargs):
  18. """
  19. compute method:
  20. @params:
  21. self - Required: (MIAM.processing.LaplaceFilter)
  22. img: - Required: image on which Laplace is computed (MIAM.image.IMAGE)
  23. kwargs - Optionnal: optionnal parameter (dict)
  24. TO DO
  25. """
  26. # taking into account additional parameters
  27. if not kwargs: kwargs = {'nbZone': 9, 'preGaussian': True, 'postScaling': True} # default value
  28. nbZone, preGaussian, postScaling = kwargs['nbZone'], kwargs['nbZone'], kwargs['postScaling']
  29. # pre-processing: gaussian filter
  30. if preGaussian:
  31. sigmaValue = max(img.colorData.shape[0],img.colorData.shape[1])/nbZone**2
  32. img = img.process(GaussianFilter.GaussianFilter(),sigma=sigmaValue)
  33. # laplace filter
  34. img = img.process(LaplaceFilter.LaplaceFilter())
  35. # squared laplace
  36. img = img**2
  37. # sum of channels WARNING: miam.image.Image to numpy
  38. sumLaplace2 = np.zeros(img.colorData.shape[0:2])
  39. sumLaplace2 = img.colorData[:,:,0]+img.colorData[:,:,1]+img.colorData[:,:,2]
  40. sumLaplace2 = sumLaplace2 / np.max(sumLaplace2)
  41. # zone and sum
  42. af = np.zeros((nbZone,nbZone)) # autofocus map
  43. stepW = sumLaplace2.shape[1]/nbZone
  44. stepH = sumLaplace2.shape[0]/nbZone
  45. for i in range(nbZone):
  46. for j in range(nbZone):
  47. af[j,i] = np.sum(sumLaplace2[(int(j*stepH)):(int((j+1)*stepH)-1),(int(i*stepW)):(int((i+1)*stepW)-1)])
  48. # post processing: scaling to One
  49. if postScaling:
  50. af = af/np.max(af)
  51. else: #average per pixel
  52. nbPixelPerZone = img.colorData.shape[0]*img.colorData.shape[1]
  53. af = af/nbPixelPerZone
  54. return image.Image.Image(af,
  55. "SumSquaredLaplace",
  56. type = image.imageType.imageType.SDR, # dot the best idea !
  57. linear = True, # dot the best idea !
  58. colorspace = image.ColorSpace.ColorSpace.buildsRGB(), # dot the best idea !
  59. scalingFactor = 1)