display_bits_shifted_scene.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Sep 14 21:02:42 2018
  5. @author: jbuisine
  6. """
  7. from __future__ import print_function
  8. import sys, os, getopt
  9. import numpy as np
  10. import random
  11. import time
  12. import json
  13. from PIL import Image
  14. from ipfml import image_processing
  15. from ipfml import metrics
  16. from skimage import color
  17. import matplotlib.pyplot as plt
  18. config_filename = "config"
  19. zone_folder = "zone"
  20. min_max_filename = "_min_max_values"
  21. # define all scenes values
  22. scenes_list = ['Appart1opt02', 'Bureau1', 'Cendrier', 'Cuisine01', 'EchecsBas', 'PNDVuePlongeante', 'SdbCentre', 'SdbDroite', 'Selles']
  23. scenes_indexes = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
  24. choices = ['svd', 'svdn', 'svdne']
  25. path = '../fichiersSVD_light'
  26. zones = np.arange(16)
  27. seuil_expe_filename = 'seuilExpe'
  28. metric_choices = ['lab', 'mscn', 'mscn_revisited', 'low_bits_2', 'low_bits_3', 'low_bits_4']
  29. max_nb_bits = 8
  30. def display_data_scenes(nb_bits, p_scene):
  31. """
  32. @brief Method which generates all .csv files from scenes photos
  33. @param path - path of scenes folder information
  34. @return nothing
  35. """
  36. scenes = os.listdir(path)
  37. # remove min max file from scenes folder
  38. scenes = [s for s in scenes if min_max_filename not in s]
  39. # go ahead each scenes
  40. for id_scene, folder_scene in enumerate(scenes):
  41. if p_scene == folder_scene:
  42. print(folder_scene)
  43. scene_path = os.path.join(path, folder_scene)
  44. config_file_path = os.path.join(scene_path, config_filename)
  45. with open(config_file_path, "r") as config_file:
  46. last_image_name = config_file.readline().strip()
  47. prefix_image_name = config_file.readline().strip()
  48. start_index_image = config_file.readline().strip()
  49. end_index_image = config_file.readline().strip()
  50. step_counter = int(config_file.readline().strip())
  51. # construct each zones folder name
  52. zones_folder = []
  53. # get zones list info
  54. for index in zones:
  55. index_str = str(index)
  56. if len(index_str) < 2:
  57. index_str = "0" + index_str
  58. current_zone = "zone"+index_str
  59. zones_folder.append(current_zone)
  60. zones_images_data = []
  61. threshold_info = []
  62. for id_zone, zone_folder in enumerate(zones_folder):
  63. zone_path = os.path.join(scene_path, zone_folder)
  64. current_counter_index = int(start_index_image)
  65. end_counter_index = int(end_index_image)
  66. # get threshold information
  67. path_seuil = os.path.join(zone_path, seuil_expe_filename)
  68. # open treshold path and get this information
  69. with open(path_seuil, "r") as seuil_file:
  70. seuil_learned = int(seuil_file.readline().strip())
  71. threshold_info.append(seuil_learned)
  72. # compute mean threshold values
  73. mean_threshold = sum(threshold_info) / float(len(threshold_info))
  74. print(mean_threshold, "mean threshold found")
  75. threshold_image_found = False
  76. # find appropriate mean threshold picture
  77. while(current_counter_index <= end_counter_index and not threshold_image_found):
  78. if mean_threshold < int(current_counter_index):
  79. current_counter_index_str = str(current_counter_index)
  80. while len(start_index_image) > len(current_counter_index_str):
  81. current_counter_index_str = "0" + current_counter_index_str
  82. threshold_image_found = True
  83. threshold_image_zone = current_counter_index_str
  84. current_counter_index += step_counter
  85. # all indexes of picture to plot
  86. images_indexes = [start_index_image, threshold_image_zone, end_index_image]
  87. images_data = []
  88. print(images_indexes)
  89. low_bits_svd_values = []
  90. for i in range(0, max_nb_bits - nb_bits + 1):
  91. low_bits_svd_values.append([])
  92. for index in images_indexes:
  93. img_path = os.path.join(scene_path, prefix_image_name + index + ".png")
  94. current_img = Image.open(img_path)
  95. block_used = np.array(current_img)
  96. low_bits_block = image_processing.rgb_to_LAB_L_bits(block_used, (i + 1, i + nb_bits + 1))
  97. low_bits_svd = metrics.get_SVD_s(low_bits_block)
  98. low_bits_svd = [b / low_bits_svd[0] for b in low_bits_svd]
  99. low_bits_svd_values[i].append(low_bits_svd)
  100. fig=plt.figure(figsize=(8, 8))
  101. fig.suptitle("Lab SVD " + str(nb_bits) + " bits values shifted for " + p_scene + " scene", fontsize=20)
  102. for id, data in enumerate(low_bits_svd_values):
  103. fig.add_subplot(3, 3, (id + 1))
  104. plt.plot(data[0], label='Noisy_' + start_index_image)
  105. plt.plot(data[1], label='Threshold_' + threshold_image_zone)
  106. plt.plot(data[2], label='Reference_' + end_index_image)
  107. plt.ylabel('Lab SVD ' + str(nb_bits) + ' bits values shifted by ' + str(id), fontsize=14)
  108. plt.xlabel('Vector features', fontsize=16)
  109. plt.legend(bbox_to_anchor=(0.5, 1), loc=2, borderaxespad=0.2, fontsize=14)
  110. plt.ylim(0, 0.1)
  111. plt.show()
  112. def main():
  113. if len(sys.argv) <= 1:
  114. print('Run with default parameters...')
  115. print('python generate_all_data.py --bits 3 --scene A')
  116. sys.exit(2)
  117. try:
  118. opts, args = getopt.getopt(sys.argv[1:], "hb:s", ["help=", "bits=", "scene="])
  119. except getopt.GetoptError:
  120. # print help information and exit:
  121. print('python generate_all_data.py --bits 4 --scene A')
  122. sys.exit(2)
  123. for o, a in opts:
  124. if o == "-h":
  125. print('python generate_all_data.py --bits 4 --scene A')
  126. sys.exit()
  127. elif o in ("-b", "--bits"):
  128. p_bits = int(a)
  129. elif o in ("-s", "--scene"):
  130. p_scene = a
  131. if p_scene not in scenes_indexes:
  132. assert False, "Invalid metric choice"
  133. else:
  134. p_scene = scenes_list[scenes_indexes.index(p_scene)]
  135. else:
  136. assert False, "unhandled option"
  137. display_data_scenes(p_bits, p_scene)
  138. if __name__== "__main__":
  139. main()