POGclusterLightness.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import os, sys
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import scipy.stats
  7. import multiprocessing as mp
  8. import easygui
  9. import imageio
  10. # miam import
  11. import miam.image.Image as MIMG
  12. import miam.image.imageType as MTYPE
  13. import miam.image.channel as MCHN
  14. import miam.processing.ColorSpaceTransform as MCST
  15. import miam.processing.TMO_Lightness as TMO_L
  16. import miam.processing.ContrastControl as PCC
  17. import miam.histogram.Histogram as MHIST
  18. import miam.aesthetics.LightnessAesthetics as MLAC
  19. import miam.aesthetics.Palette as MPAL
  20. import miam.imageDB.ImageDB
  21. import miam.imageDB.POGChecker
  22. import miam.imageDB.HwHDRBuilder
  23. import miam.html.generator
  24. import miam.utils
  25. import miam.pointcloud.PointCloud2D
  26. # ------------------------------------------------------------------------------------------
  27. # MIAM project 2020
  28. # ------------------------------------------------------------------------------------------
  29. # author: remi.cozot@univ-littoral.fr
  30. # ------------------------------------------------------------------------------------------
  31. def pCompLightnessMoments(filename):
  32. # cut filename
  33. path, name, ext = miam.utils.splitFileName(filename)
  34. # read image
  35. img = MIMG.Image.read(filename,exif=False)
  36. # compute L lightness
  37. L = img.getChannelVector(MCHN.channel.L)
  38. # get bins
  39. L_mean = np.mean(L)
  40. L_std = np.std(L)
  41. L_skew = scipy.stats.skew(L)
  42. #print("<",name,">",end='')
  43. print("█",end='')
  44. sys.stdout.flush()
  45. return np.asarray([L_mean,L_std,L_skew])
  46. def pCompHistogram(filename):
  47. # cut filename
  48. path, name, ext = miam.utils.splitFileName(filename)
  49. # read image
  50. img = MIMG.Image.read(filename,exif=False)
  51. # compute histogram
  52. histo = MHIST.Histogram.build(img,MIMG.channel.channel.L,50).normalise(norm='dot')
  53. # get bins
  54. bins = histo.histValue
  55. #print("<",name,">",end='')
  56. print("█",end='')
  57. sys.stdout.flush()
  58. return bins
  59. if __name__ == '__main__':
  60. print("MIAM[POG Cluster Ligthness]")
  61. # ------------------------------------------------------------------------------------------
  62. # what to do !
  63. # ------------------------------------------------------------------------------------------
  64. # config: configfile
  65. jsonfile = "../DB/config_POG_DB.json"
  66. # all lightness moments file
  67. allLightness_Moments = "../DB/POG_DB_L_Moments.npy"
  68. # all lightness histogram file
  69. allLightness_50bins = "../DB/POG_DB_L_Hists_50bins.npy"
  70. allLightness_100bins = "../DB/POG_DB_L_Hists_100bins.npy"
  71. # scatter plot: sdt(mean)
  72. display_StdMean = True
  73. # ------------------------------------------------------------------------------------------
  74. # pog
  75. pogDB = miam.imageDB.ImageDB.ImageDB(jsonConfigFile ="../DB/config_POG_DB.json")
  76. pogDB.check(miam.imageDB.POGChecker.POGChecker(local=True))
  77. print("MIAM[",pogDB.name,":(csvFileName:: ",pogDB.csvFileName,", imagePATH::",pogDB.imagePATH,")]")
  78. # POG: compute Lightness Moments
  79. # ------------------------------------------------------------------------------------------
  80. allMoments = None
  81. if not os.path.isfile(allLightness_Moments):
  82. print("MIAM[",pogDB.name," lightness moments not found !]")
  83. nbCpu = mp.cpu_count()
  84. print("MIAM[", nbCpu, "cpu cores]")
  85. print("MIAM[",pogDB.name," number of images:",len(pogDB.db),"]")
  86. print("MIAM[",pogDB.name," launch parallel computation of moments !]")
  87. _pool = mp.Pool()
  88. result = _pool.map(pCompLightnessMoments, pogDB.db)
  89. allMoments = np.asarray(result)
  90. print("")
  91. print("[ --------------------------------------------------------]")
  92. print("MIAM[",pogDB.name," parallel computation of moments done !]")
  93. print("MIAM[",pogDB.name," all moments:",allMoments.shape," ]")
  94. np.save("../DB/"+pogDB.name+'_L_Moments',allMoments)
  95. print("MIAM[",pogDB.name," all moments saved !]")
  96. else:
  97. print("")
  98. print("[ --------------------------------------------------------]")
  99. print("MIAM[",pogDB.name," lightness moments found !]")
  100. print("MIAM[",pogDB.name," all moments loaded !]")
  101. allMoments = np.load(allLightness_Moments)
  102. print("MIAM[",pogDB.name," all moments:",allMoments.shape," ]")
  103. # POG: display Lightness Moments
  104. # ------------------------------------------------------------------------------------------
  105. if display_StdMean:
  106. plt.figure("Lightness: standard deviations/mean")
  107. plt.plot(allMoments[:,0],allMoments[:,1],'bo',markersize=2)
  108. # k,r = miam.pointcloud.PointCloud2D.PointCloud2D(allMoments[:,0],allMoments[:,1]).removeIsolatedPoint(2.0,1)
  109. #k,r = miam.pointcloud.PointCloud2D.PointCloud2D(allMoments[:,0],allMoments[:,1]).removeIsolatedPoint(10.0,500)
  110. #plt.plot(k.X,k.Y,'bo',markersize=2)
  111. #plt.plot(r.X,r.Y,'ro',markersize=2)
  112. plt.plot(allMoments[:,0],allMoments[:,1],'bo',markersize=2)
  113. plt.show(block=True)
  114. # POG: compute Lightness Histograms
  115. # ------------------------------------------------------------------------------------------
  116. if not os.path.isfile(allLightness_50bins):
  117. print("MIAM[",pogDB.name," lightness histogram not found !]")
  118. nbCpu = mp.cpu_count()
  119. print("MIAM[", nbCpu, "cpu cores]")
  120. print("MIAM[",pogDB.name," number of images:",len(pogDB.db),"]")
  121. print("MIAM[",pogDB.name," launch parallel computation of histograms !]")
  122. _pool = mp.Pool()
  123. result = _pool.map(pCompHistogram, pogDB.db)
  124. allHist = np.asarray(result)
  125. print("")
  126. print("[ ---------------------------------------------------------- ]")
  127. print("MIAM[",pogDB.name," parallel computation of histograms done !]")
  128. print("MIAM[",pogDB.name," all histograms:",allHist.shape," ]")
  129. np.save("../DB/"+pogDB.name+'_L_Hists_50bins',allHist)
  130. print("MIAM[",pogDB.name," all histograms saved !]")
  131. else:
  132. print("")
  133. print("[ --------------------------------------------------------]")
  134. print("MIAM[",pogDB.name," lightness histogram found !]")