myapp.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. # import
  2. # ------------------------------------------------------------------------------------------
  3. import os, time
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import multiprocessing as mp
  7. import easygui
  8. import imageio
  9. # miam import
  10. import miam.image.Image as MIMG
  11. import miam.image.imageType as MTYPE
  12. import miam.image.channel as MCH
  13. import miam.processing.ColorSpaceTransform as MCST
  14. import miam.processing.TMO_Lightness as TMO_L
  15. import miam.processing.ContrastControl as PCC
  16. import miam.histogram.Histogram as MHIST
  17. import miam.aesthetics.LightnessAesthetics as MLAC
  18. import miam.aesthetics.Palette as MPAL
  19. import miam.aesthetics.Composition as MCMP
  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 as MPC2D
  26. import miam.classification.kmeans
  27. import miam.math.Distance
  28. import miam.math.Normalize
  29. # ------------------------------------------------------------------------------------------
  30. # MIAM project 2020
  31. # ------------------------------------------------------------------------------------------
  32. # author: remi.cozot@univ-littoral.fr
  33. # ------------------------------------------------------------------------------------------
  34. # --------------------------------------------> basic app
  35. def guiImage(filename=None):
  36. if not filename:
  37. filename = easygui.fileopenbox(msg="select image.")
  38. print("MIAM[selected image:", filename,"]")
  39. # paramters
  40. nbColorInPalette =5
  41. nbPointInCompo =10
  42. # reading image
  43. start = time.time()
  44. image = MIMG.Image.readImage(filename, readExif=False)
  45. end = time.time()
  46. print("[read image: ........ ",int((end-start)*1000),"(ms)]")
  47. # compute histogram
  48. start = time.time()
  49. histogram = MHIST.Histogram.build(image,MIMG.channel.channel.L,50).normalise(norm='dot')
  50. end = time.time()
  51. print("[compute histogram: . ",int((end-start)*1000),"(ms)]")
  52. # compute palette
  53. start = time.time()
  54. palette = MPAL.Palette.build(image,nbColorInPalette,method='kmean-Lab', removeBlack=True)
  55. imageOfPalette = palette.createImageOfPalette()
  56. end = time.time()
  57. print("[compute palette: ... ",int((end-start)*1000),"(ms)]")
  58. # compute composition
  59. start = time.time()
  60. composition = MCMP.Composition.build(image,nbPoint =nbPointInCompo, nbZone=9)
  61. end = time.time()
  62. print("[compute composition: ",int((end-start)*1000),"(ms)]")
  63. # display all elements
  64. fig = plt.figure()
  65. axHisto = plt.subplot(233)
  66. axPalette = plt.subplot(236)
  67. axImage = plt.subplot(131)
  68. axCompo = plt.subplot(132)
  69. image.plot(axImage,title=False)
  70. histogram.plot(axHisto,title=False)
  71. imageOfPalette.plot(axPalette, title=False)
  72. image.plot(axCompo,title=False)
  73. composition.plot(axCompo,title=False, keepRawPoints = True)
  74. axHisto.set_title("Histogram (Lightness)")
  75. axPalette.set_title("Palette ("+str(nbColorInPalette)+" colors)")
  76. axImage.set_title("Image")
  77. axCompo.set_title("Composition ("+str(nbPointInCompo)+" points)")
  78. fig.suptitle(image.name)
  79. plt.show(block=True)
  80. def guiHDR():
  81. pathImage = easygui.fileopenbox(msg="select HDR image.")
  82. print("selected image:", pathImage)
  83. # load image and remove zeros
  84. img = MIMG.Image.readImage(pathImage).removeZeros().removeZeros(0.5)
  85. ((minR,maxR),(minG,maxG),(minB,maxB)) = img.getMinMaxPerChannel()
  86. # print min max per channel
  87. print("(min/max): R:(",minR,"/",maxR,") - G:(",minG,"/",maxG,") -B:(",minB,"/",maxB,")")
  88. # to XYZ
  89. imgXYZ = MCST.ColorSpaceTransform().compute(img,dest='XYZ')
  90. # print min max per channel
  91. ((minX,maxX),(minY,maxY),(minZ,maxZ)) = imgXYZ.getMinMaxPerChannel()
  92. print("(min/max): X:(",minX,"/",maxX,") - Y:(",minY,"/",maxY,") - Z:(",minZ,"/",maxZ,")")
  93. # dynamic range
  94. print("dynamic range (0.5%):",img.getDynamicRange( percentile = 0.25, mode="f-stops"),"f-stops/",
  95. img.getDynamicRange( percentile = 0.25, mode="maxmin"),"(Ymax,Ymin)")
  96. print("dynamic range (0):",img.getDynamicRange( percentile = 0, mode="f-stops"),"f-stops/",
  97. img.getDynamicRange( percentile = 0, mode="maxmin"),"(Ymax,Ymin)")
  98. # histogram
  99. hRGB = MHIST.Histogram.build(img,MIMG.channel.channel.sG,nbBins=50)
  100. hXYZ = MHIST.Histogram.build(imgXYZ,MIMG.channel.channel.Y,nbBins=50)
  101. # plot
  102. fig, ax = plt.subplots(1,3)
  103. img.plot(ax[0])
  104. hRGB.plot(ax[1])
  105. hXYZ.plot(ax[2])
  106. plt.show(block=True)
  107. # --------------------------------------------> Lightness HDR dataset
  108. def testDB_Huawei():
  109. hDB = miam.imageDB.ImageDB_HDD.ImageDB_HDD(name= "HDR DataBase" ,csvFile="../DB/hdrHuawei_DB.csv", jsonConfigFile ="../DB/config_HDR_DB.json")
  110. hDB.build(miam.imageDB.HwHDRBuilder.HwHDRBuilder(),src='HDR')
  111. #hDB.build(miam.imageDB.HwHDRBuilder.HwHDRBuilder(),src='HTM')
  112. #hDB.check()
  113. hDB.buildHTMLPage()
  114. # --------------------------------------------> Lightness TMO
  115. def guiLightnessTMO():
  116. pathImage = easygui.fileopenbox(msg="select HDR image.")
  117. print("selected image:", pathImage)
  118. # load image and remove zeros
  119. img = MIMG.Image.readImage(pathImage).removeZeros().removeZeros(0.5) #.resize((1920,None))
  120. # tonemap
  121. res = img.process(TMO_L.TMO_Lightness())
  122. # contrast control
  123. imgLE = res.process(PCC.ContrastControl(),method='localEqualization', size=1500)
  124. imgLE.name += '_local equaliz.'
  125. alpha = 0.75
  126. fusion = alpha*res+(1-alpha)*imgLE
  127. fusion.name = 'fusion'
  128. # histograms
  129. #hHDR = MHIST.Histogram.build(img,MIMG.channel.channel.Y,nbBins=50) # hdr image histogram
  130. #hTMoL = MHIST.Histogram.build(res,MIMG.channel.channel.L,nbBins=50) # TMo lightness histogram
  131. #hLE = MHIST.Histogram.build(imgLE,MIMG.channel.channel.L,nbBins=50) # TMo Lightness + local equalization histogram
  132. # plot
  133. fig, ax = plt.subplots(2,2)
  134. img.plot(ax[0,0]) # hdr
  135. res.plot(ax[0,1]) # lightness tone mapping
  136. fusion.plot(ax[1,0])
  137. imgLE.plot(ax[1,1])
  138. plt.show(block=True)
  139. imageio.imsave("testLightness_loalEQ.jpg",fusion.colorData)
  140. #imageio.imsave("testLightness+stretch.jpg",imgCS.colorData)
  141. #imageio.imsave("testLightness+equalization.jpg",imgGE.colorData)
  142. pass
  143. def testLightnessTMO():
  144. srcPath = "../../../photos/HDR-DB/HDR/"
  145. listFiles = os.listdir(srcPath)
  146. for file in listFiles:
  147. print("fileName:", file , end='::')
  148. (name, ext) = miam.utils.splitFileName(file)
  149. print("loading", end='::')
  150. # load image and remove zeros
  151. img = MIMG.Image.readImage(srcPath+file).removeZeros().removeZeros(0.5)
  152. print("ok", end='::')
  153. print("tone mapping", end='::')
  154. # tonemap
  155. res = img.process(TMO_L.TMO_Lightness())
  156. print("ok", end='::')
  157. print("post-processing", end='::')
  158. # contrast control
  159. imgLE = res.process(PCC.ContrastControl(),method='localEqualization', size=1500)
  160. print("ok", end='::')
  161. print("fusion", end='::')
  162. # fusion
  163. alpha = 0.75
  164. fusion = alpha*res+(1-alpha)*imgLE
  165. print("ok", end='::')
  166. print("save", end='::')
  167. # save
  168. imageio.imsave(name+"_Lightness_localEQ.jpg",fusion.colorData)
  169. print("ok", end='\n')
  170. # --------------------------------------------> Segmentation
  171. def testSegment(filename=None):
  172. if not filename:
  173. filename = easygui.fileopenbox(msg="select HDR image.")
  174. print("selected image:", filename)
  175. # reading image
  176. i0 = MIMG.Image.readImage(filename)
  177. # compute histogram
  178. h0 = MHIST.Histogram.build(i0,MIMG.channel.channel.L,100).normalise(norm='probability')
  179. print(h0)
  180. res = h0.segmentPics(nbSegs=5)
  181. def testMask(filename=None):
  182. if not filename:
  183. filename = easygui.fileopenbox(msg="select HDR image.")
  184. print("selected image:", filename)
  185. # reading image
  186. i0 = MIMG.Image.readImage(filename)
  187. mask = i0.buildMaskByValue(MIMG.channel.channel.L, 0,50)
  188. fig, ax = plt.subplots(nrows=1, ncols=2)
  189. # +-----------------------------+-----------------------------+
  190. # | [0]= i0 | [1]= mask |
  191. # +-----------------------------+-----------------------------+
  192. i0.plot(ax[0])
  193. mask.plot(ax[1])
  194. plt.show(block=True)
  195. def testSegment2(filename=None):
  196. if not filename:
  197. filename = easygui.fileopenbox(msg="select an image.")
  198. print("selected image:", filename)
  199. None
  200. # reading image and compute histogram
  201. i0 = MIMG.Image.readImage(filename)
  202. if i0.type == MTYPE.imageType.HDR:
  203. LorY = MIMG.channel.channel.Y
  204. i0 = i0.removeZeros(0.5)
  205. else:
  206. LorY = MIMG.channel.channel.L
  207. h0 = MHIST.Histogram.build(i0,LorY,100).normalise(norm='probability')
  208. segmentBoundariesValue = h0.segmentPics(nbSegs=5)
  209. # debug
  210. print("segment boundaries value:", segmentBoundariesValue)
  211. mask_i = []
  212. for i in range(len(segmentBoundariesValue)-1):
  213. mask = i0.buildMaskByValue(LorY,segmentBoundariesValue[i],segmentBoundariesValue[i+1])
  214. mask_i.append(mask)
  215. # plot
  216. fig, ax = plt.subplots(nrows=1, ncols=1)
  217. # +-----------------------------+
  218. # | [0]= i0 |
  219. # +-----------------------------+
  220. i0.plot(ax)
  221. plt.show(block=False)
  222. # plot
  223. fig, ax = plt.subplots(nrows=len(segmentBoundariesValue)-1, ncols=1)
  224. # +-----------------------------+
  225. # | [i]=mask[i] |
  226. # +-----------------------------+
  227. for i in range(len(segmentBoundariesValue)-1):
  228. mask_i[i].plot(ax[i])
  229. plt.show(block=True)
  230. # --------------------------------------------> Palette
  231. def testAddPalette(filename0=None, filename1=None):
  232. # select images
  233. if not filename0:
  234. filename0 = easygui.fileopenbox(msg="select first image.")
  235. print("selected image:", filename0)
  236. if not filename1:
  237. filename1 = easygui.fileopenbox(msg="select first image.")
  238. print("selected image:", filename1)
  239. # reading image
  240. i0 = MIMG.Image.readImage(filename0)
  241. i1 = MIMG.Image.readImage(filename1)
  242. # compute palettes
  243. p0 = MPAL.Palette.build(i0,5,method='kmean-Lab', removeBlack=True)
  244. imageOfPalette0 = p0.createImageOfPalette()
  245. p1 = MPAL.Palette.build(i1,5,method='kmean-Lab', removeBlack=True)
  246. imageOfPalette1 = p1.createImageOfPalette()
  247. # average palette
  248. p01 = 0.5*(p0+p1)
  249. fig, ax = plt.subplots(nrows=2, ncols=3)
  250. # +-----------------------------+-----------------------------+-----------------------------+
  251. # | [0,0]= i0 | [0,1]= i1 | [0,2]= p01 |
  252. # +-----------------------------+-----------------------------+-----------------------------+
  253. # | [1,0]= p0 | [1,1]= p1 | [1,2]= |
  254. # +-----------------------------+-----------------------------+-----------------------------+
  255. i0.plot(ax[0,0])
  256. imageOfPalette0.plot(ax[1,0])
  257. i1.plot(ax[0,1])
  258. imageOfPalette1.plot(ax[1,1])
  259. p01.createImageOfPalette().plot(ax[0,2])
  260. plt.show(block=True)
  261. # --------------------------------------------> Compistion
  262. def estimateComposition(filename=None):
  263. if not filename:
  264. filename = easygui.fileopenbox(msg="select image.")
  265. print("selected image:", filename)
  266. # reading image
  267. img = MIMG.Image.readImage(filename)
  268. comp = MCMP.Composition.build(img,nbPoint =3, nbZone=9)
  269. fig, ax = plt.subplots(nrows=1, ncols=2)
  270. # +-----------------------------+-----------------------------+-----------------------------+
  271. # | [0,0]= image | [0,1]= ssl map | [0,2]= |
  272. # +-----------------------------+-----------------------------+-----------------------------+
  273. # | [1,0]= | [1,1]= | [1,2]= |
  274. # +-----------------------------+-----------------------------+-----------------------------+
  275. img.plot(ax[0])
  276. comp.plot(ax[0],title=False)
  277. ax[0].axis(False)
  278. comp.plot(ax[1])
  279. plt.show(block=True)
  280. # load aesthetics classes
  281. #centroidsAES = MLAC.LightnessAesthetics.readLightnessClasses('../../aestheticsClass/both/statsLY/'+'km_centroids_nbCluster5_nbBin50.npy')
  282. #dists = centroidsAES.computeImageClass(i0)
  283. #h0AES0 = centroidsAES.projectImage(i0,nbClass=range(0,1))
  284. #h0AES01 = centroidsAES.projectImage(i0,nbClass=range(0,2))