Composition.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import copy
  4. import numpy as np
  5. # local
  6. import miam.utils
  7. import miam.image.Image as MIMG
  8. import miam.processing.SumSquaredLaplace as MSSL
  9. import miam.pointcloud.PointCloud2D as MPC2D
  10. # ------------------------------------------------------------------------------------------
  11. # MIAM project 2020
  12. # ------------------------------------------------------------------------------------------
  13. # author: remi.cozot@univ-littoral.fr
  14. # ------------------------------------------------------------------------------------------
  15. class Composition(object):
  16. """
  17. class Composition:
  18. attribute(s):
  19. name: object name
  20. nbPoints: number of point in composition
  21. points: list points[(x,y)]*nbPoints
  22. """
  23. # constructor
  24. def __init__(self, name, points,imgShape):
  25. self.name: name # name (str)
  26. self.points = points # list of tunple [(x,y)] * npPoints in [0..1] range
  27. self.nbPoints = len(self.points) # number of Points
  28. self.shape = imgShape # shape of image from which it is
  29. # build
  30. def build(image, nbPoint=10, nbZone=9, **kwargs):
  31. # taking into account additional parameters
  32. if not kwargs: kwargs = {'preGaussian': True, 'postScaling': True} # default value
  33. preGaussian, postScaling = kwargs['preGaussian'], kwargs['postScaling']
  34. # compute Sum of Squared Laplace map
  35. sslMap = image.process(MSSL.SumSquaredLaplace(),
  36. nbZone= nbZone,
  37. preGaussian = preGaussian,
  38. postScaling= postScaling)
  39. # extract maximum focus (ie max sum squared Laplacien)
  40. points = []
  41. sslArray = np.argsort(sslMap.colorData.flatten())
  42. for i in range(-1,-(nbPoint+1),-1):
  43. p = np.divmod(sslArray[i],nbZone)
  44. x,y = (p[1]+0.5)/nbZone, (p[0]+0.5)/nbZone
  45. points.append((x,y))
  46. (path,name, zxt) = miam.utils.splitFileName(image.name)
  47. return Composition(name+"(cpmposition)", points, image.shape[0:2])
  48. # plot
  49. def plot(self,ax, marker = None,shortName=True, title=True, keepRawPoints = False):
  50. marker = 'go-' if not marker else marker
  51. # to image space
  52. W, H =self.shape[1], self.shape[0]
  53. pImageSpace = []
  54. for p in self.points:
  55. xIS, yIS = p
  56. xIS = xIS*W
  57. yIS = yIS*H
  58. pImageSpace.append((xIS,yIS))
  59. # to X, Y array
  60. X, Y, XRAW, YRAW = [],[], [], []
  61. for p in pImageSpace:
  62. x,y = p
  63. X.append(x)
  64. Y.append(y)
  65. XRAW = copy.deepcopy(X)
  66. YRAW = copy.deepcopy(Y)
  67. if self.nbPoints == 3: # display triangle
  68. X.append(X[0])
  69. Y.append(Y[0])
  70. keepRawPoints = False
  71. else: # display convex Hull
  72. pc = MPC2D.PointCloud2D(X,Y)
  73. X,Y = MPC2D.PointCloud2D.toXYarray(pc.convexHull())
  74. if keepRawPoints : ax.plot(XRAW,YRAW,'ro')
  75. ax.plot(X,Y,marker)
  76. ax.set_xlim(0,W)
  77. ax.set_ylim(H,0)
  78. name = self.name if not shortName else "(comp.)"
  79. if title: ax.set_title(name)
  80. ax.axis("on")