test_mscn.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. from ipfml import image_processing
  2. from PIL import Image
  3. import numpy as np
  4. from ipfml import metrics
  5. from skimage import color
  6. import cv2
  7. low_bits_svd_values_norm = []
  8. low_bits_svd_values_norm_together = []
  9. low_bits_svd_values = []
  10. mscn_svd_values_norm = []
  11. mscn_svd_values_norm_together = []
  12. mscn_svd_values = []
  13. lab_svd_values_norm = []
  14. lab_svd_values_norm_together = []
  15. lab_svd_values = []
  16. def open_and_display(path):
  17. img = Image.open(path)
  18. blocks = image_processing.divide_in_blocks(img, (200, 200), False)
  19. block_used = blocks[11]
  20. img_mscn = image_processing.rgb_to_mscn(block_used)
  21. #img_mscn_norm = image_processing.normalize_2D_arr(img_mscn)
  22. #print(img_mscn)
  23. img_output = img_mscn.astype('uint8')
  24. print('-------------------------')
  25. # MSCN part computation
  26. mscn_s = metrics.get_SVD_s(img_output)
  27. mscn_svd_values.append(mscn_s)
  28. mscn_svd_values_norm.append(image_processing.normalize_arr(mscn_s))
  29. mscn_min_val = 10000000
  30. mscn_max_val = 0
  31. # check for each block of image
  32. for block in blocks:
  33. current_img_mscn = image_processing.rgb_to_mscn(block)
  34. current_img_output = img_mscn.astype('uint8')
  35. # MSCN part computation
  36. current_mscn_s = metrics.get_SVD_s(img_output)
  37. current_min = current_mscn_s.min()
  38. current_max = current_mscn_s.max()
  39. if current_min < mscn_min_val:
  40. mscn_min_val = current_min
  41. if current_max > mscn_max_val:
  42. mscn_max_val = current_max
  43. mscn_svd_values_norm_together.append(image_processing.normalize_arr_with_range(mscn_s, mscn_min_val, mscn_max_val))
  44. # LAB part computation
  45. path_block_img = '/tmp/lab_img.png'
  46. img_used_pil = Image.fromarray(block_used.astype('uint8'), 'RGB')
  47. img_used_pil.save(path_block_img)
  48. #img_used_pil.show()
  49. lab_s = image_processing.get_LAB_L_SVD_s(Image.open(path_block_img))
  50. lab_svd_values.append(lab_s)
  51. lab_svd_values_norm.append(image_processing.normalize_arr(lab_s))
  52. lab_min_val = 10000000
  53. lab_max_val = 0
  54. # check for each block of image
  55. for block in blocks:
  56. current_img_used_pil = Image.fromarray(block.astype('uint8'), 'RGB')
  57. current_img_used_pil.save(path_block_img)
  58. current_lab_s = image_processing.get_LAB_L_SVD_s(Image.open(path_block_img))
  59. current_min = current_lab_s.min()
  60. current_max = current_lab_s.max()
  61. if current_min < lab_min_val:
  62. lab_min_val = current_min
  63. if current_max > lab_max_val:
  64. lab_max_val = current_max
  65. lab_svd_values_norm_together.append(image_processing.normalize_arr_with_range(lab_s, lab_min_val, lab_max_val))
  66. # computation of low bits parts
  67. low_bits_block = image_processing.rgb_to_grey_low_bits(block_used)
  68. low_bits_svd = metrics.get_SVD_s(low_bits_block)
  69. low_bits_svd_values.append(low_bits_svd)
  70. low_bits_svd_values_norm.append(image_processing.normalize_arr(low_bits_svd))
  71. low_bits_min_val = 10000000
  72. low_bits_max_val = 0
  73. # check for each block of image
  74. for block in blocks:
  75. current_grey_block = np.array(color.rgb2gray(block)*255, 'uint8')
  76. current_low_bit_block = current_grey_block & 15
  77. current_low_bits_svd = metrics.get_SVD_s(current_low_bit_block)
  78. current_min = current_low_bits_svd.min()
  79. current_max = current_low_bits_svd.max()
  80. if current_min < low_bits_min_val:
  81. low_bits_min_val = current_min
  82. if current_max > low_bits_max_val:
  83. low_bits_max_val = current_max
  84. low_bits_svd_values_norm_together.append(image_processing.normalize_arr_with_range(low_bits_svd, low_bits_min_val, low_bits_max_val))
  85. # Other MSCN
  86. img_grey = np.array(color.rgb2gray(np.asarray(block_used))*255, 'uint8')
  87. img_mscn_in_grey = np.array(image_processing.normalize_2D_arr(image_processing.calculate_mscn_coefficients(img_grey, 7))*255, 'uint8')
  88. svd_s_values = metrics.get_SVD_s(img_mscn_in_grey)
  89. #print(svd_s_values[0:10])
  90. img_mscn_pil = Image.fromarray(img_mscn_in_grey.astype('uint8'), 'L')
  91. #img_mscn_pil.show()
  92. #path_noisy = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Appart1opt02/appartAopt_00020.png'
  93. #path_threshold = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Appart1opt02/appartAopt_00300.png'
  94. #path_ref = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Appart1opt02/appartAopt_00900.png'
  95. path_noisy = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Cuisine01/cuisine01_00050.png'
  96. path_threshold = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Cuisine01/cuisine01_00400.png'
  97. path_ref = '/home/jbuisine/Documents/Thesis/Development/NoiseDetection_In_SynthesisImages/fichiersSVD_light/Cuisine01/cuisine01_01200.png'
  98. path_list = [path_noisy, path_threshold, path_ref]
  99. for p in path_list:
  100. open_and_display(p)
  101. import matplotlib.pyplot as plt
  102. # SVD
  103. fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
  104. # make a little extra space between the subplots
  105. fig.subplots_adjust(hspace=0.5)
  106. ax1.plot(lab_svd_values[0], label='Noisy')
  107. ax1.plot(lab_svd_values[1], label='Threshold')
  108. ax1.plot(lab_svd_values[2], label='Reference')
  109. ax1.set_ylabel('LAB SVD comparisons')
  110. ax1.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  111. ax2.plot(mscn_svd_values[0], label='Noisy')
  112. ax2.plot(mscn_svd_values[1], label='Threshold')
  113. ax2.plot(mscn_svd_values[2], label='Reference')
  114. ax2.set_ylabel('MSCN SVD comparisons')
  115. ax2.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  116. ax3.plot(low_bits_svd_values[0], label='Noisy')
  117. ax3.plot(low_bits_svd_values[1], label='Threshold')
  118. ax3.plot(low_bits_svd_values[2], label='Reference')
  119. ax3.set_ylabel('Low bits SVD comparisons')
  120. ax3.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  121. plt.show()
  122. # SVDN
  123. fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
  124. # make a little extra space between the subplots
  125. fig.subplots_adjust(hspace=0.5)
  126. ax1.plot(lab_svd_values_norm[0], label='Noisy')
  127. ax1.plot(lab_svd_values_norm[1], label='Threshold')
  128. ax1.plot(lab_svd_values_norm[2], label='Reference')
  129. ax1.set_ylabel('LAB SVDN comparisons')
  130. ax1.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  131. ax2.plot(mscn_svd_values_norm[0], label='Noisy')
  132. ax2.plot(mscn_svd_values_norm[1], label='Threshold')
  133. ax2.plot(mscn_svd_values_norm[2], label='Reference')
  134. ax2.set_ylabel('MSCN SVDN comparisons')
  135. ax2.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  136. ax3.plot(low_bits_svd_values_norm[0], label='Noisy')
  137. ax3.plot(low_bits_svd_values_norm[1], label='Threshold')
  138. ax3.plot(low_bits_svd_values_norm[2], label='Reference')
  139. ax3.set_ylabel('Low bits SVD comparisons')
  140. ax3.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  141. plt.show()
  142. # SVDNE
  143. fig, (ax1, ax2, ax3) = plt.subplots(3, 1)
  144. # make a little extra space between the subplots
  145. fig.subplots_adjust(hspace=0.5)
  146. ax1.plot(lab_svd_values_norm_together[0], label='Noisy')
  147. ax1.plot(lab_svd_values_norm_together[1], label='Threshold')
  148. ax1.plot(lab_svd_values_norm_together[2], label='Reference')
  149. ax1.set_ylabel('LAB SVDNE comparisons')
  150. ax1.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  151. ax2.plot(mscn_svd_values_norm_together[0], label='Noisy')
  152. ax2.plot(mscn_svd_values_norm_together[1], label='Threshold')
  153. ax2.plot(mscn_svd_values_norm_together[2], label='Reference')
  154. ax2.set_ylabel('MSCN SVDNE comparisons')
  155. ax2.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  156. ax3.plot(low_bits_svd_values_norm_together[0], label='Noisy')
  157. ax3.plot(low_bits_svd_values_norm_together[1], label='Threshold')
  158. ax3.plot(low_bits_svd_values_norm_together[2], label='Reference')
  159. ax3.set_ylabel('Low bits SVD comparisons')
  160. ax3.legend(bbox_to_anchor=(0.7, 1), loc=2, borderaxespad=0.2)
  161. plt.show()
  162. #print(mscn_svd_values[0][0:3])
  163. #print(mscn_svd_values[1][0:3])
  164. #print(mscn_svd_values[2][0:3])