feature_glcm.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # import numpy as np
  2. # import cv2
  3. # import sys
  4. # import os
  5. # from skimage.feature import graycomatrix, graycoprops, greycoprops, greycomatrix
  6. # from skimage import io, color, img_as_ubyte
  7. # import matplotlib.pyplot as plt
  8. # # 读取图像
  9. # image = io.imread('HB03333.jpeg')
  10. #
  11. # # 将图像转换为灰度
  12. # gray_image = io.imread('D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG')
  13. # gray_image = img_as_ubyte(gray_image)
  14. #
  15. # # 定义GLCM参数
  16. # distances = [1, 2, 3, 4] # 邻距离
  17. # angles = [0, np.pi/4, np.pi/2, 3*np.pi/4] # 邻角度
  18. #
  19. # # 计算GLCM
  20. # glcm = graycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True)
  21. #
  22. # # 计算GLCM特征
  23. # contrast = graycoprops(glcm, prop='contrast')
  24. # dissimilarity = graycoprops(glcm, prop='dissimilarity')
  25. # homogeneity = graycoprops(glcm, prop='homogeneity')
  26. # energy = graycoprops(glcm, prop='energy')
  27. # correlation = graycoprops(glcm, prop='correlation')
  28. #
  29. # # 打印GLCM特征
  30. # print("Contrast:", contrast)
  31. # print("Dissimilarity:", dissimilarity)
  32. # print("Homogeneity:", homogeneity)
  33. # print("Energy:", energy)
  34. # print("Correlation:", correlation)
  35. # # 可视化GLCM特征
  36. #
  37. # plt.imshow(energy, cmap='hot')
  38. # plt.title('GLCM Energy Map')
  39. # custom_xticks = np.arange(4)
  40. # custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应
  41. # custom_yticklables = ['1', '2', '3', '4']
  42. # plt.yticks(custom_xticks, custom_yticklables)
  43. # plt.xticks(custom_xticks, custom_xticklabels) # 设置刻度位置和标签
  44. # plt.xlabel('Angles')
  45. # plt.ylabel('distance')
  46. # plt.colorbar()
  47. #
  48. # plt.savefig('D:\\hiddz\\SAR\\test_data\\TEST\A11')
  49. # plt.show()
  50. # #
  51. #
  52. # def process_image(image_path):
  53. # gray_image = io.imread(image_path)
  54. # gray_image = img_as_ubyte(gray_image)
  55. #
  56. # distances = [1, 2, 3, 4]
  57. # angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4] # 邻角度
  58. #
  59. # # 计算GLCM
  60. # glcm = graycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True)
  61. #
  62. # # 计算GLCM特征
  63. # contrast = graycoprops(glcm, prop='contrast')
  64. # dissimilarity = graycoprops(glcm, prop='dissimilarity')
  65. # homogeneity = graycoprops(glcm, prop='homogeneity')
  66. # energy = graycoprops(glcm, prop='energy')
  67. # correlation = graycoprops(glcm, prop='correlation')
  68. #
  69. # plt.imshow(energy, cmap='hot')
  70. # plt.title('GLCM Energy Map')
  71. # custom_xticks = np.arange(4)
  72. # custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应
  73. # custom_yticklables = ['1', '2', '3', '4']
  74. # plt.yticks(custom_xticks, custom_yticklables)
  75. # plt.xticks(custom_xticks, custom_xticklabels) # 设置刻度位置和标签
  76. # plt.xlabel('Angles')
  77. # plt.ylabel('distance')
  78. # plt.colorbar()
  79. # plt.show()
  80. # folder_name = os.path.basename(os.path.dirname(image_path))
  81. #
  82. # save_folder = os.path.join(os.path.dirname(os.path.abspath(__path__)), 'glcm', folder_name)
  83. # os.makedirs(save_folder, exist_ok=True)
  84. #
  85. # save_path = os.path.join(save_folder, 'glcm_', os.path.basename(image_path))
  86. # plt.imsave(save_path, energy, cmap = 'gray')
  87. # print(save_path)
  88. # if __name__ == '__main__':
  89. # if len(sys.argv) != 2:
  90. # sys.exit(1)
  91. #
  92. # image_path = sys.argv[1]
  93. # process_image(image_path)
  94. import numpy as np
  95. from skimage import io, img_as_ubyte
  96. from skimage.feature import greycomatrix, greycoprops
  97. import matplotlib.pyplot as plt
  98. from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
  99. def process_image(image_path):
  100. gray_image = io.imread(image_path)
  101. gray_image = img_as_ubyte(gray_image)
  102. distances = [1, 2, 3, 4]
  103. angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4] # 邻角度
  104. # 计算GLCM
  105. glcm = greycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True)
  106. # 计算GLCM特征
  107. contrast = greycoprops(glcm, prop='contrast')
  108. dissimilarity = greycoprops(glcm, prop='dissimilarity')
  109. homogeneity = greycoprops(glcm, prop='homogeneity')
  110. energy = greycoprops(glcm, prop='energy')
  111. correlation = greycoprops(glcm, prop='correlation')
  112. fig, ax = plt.subplots()
  113. ax.imshow(energy, cmap='hot')
  114. ax.set_title('GLCM Energy Map')
  115. custom_xticks = np.arange(4)
  116. custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应
  117. custom_yticklables = ['1', '2', '3', '4']
  118. ax.set_yticks(custom_xticks)
  119. ax.set_yticklabels(custom_yticklables)
  120. ax.set_xticks(custom_xticks)
  121. ax.set_xticklabels(custom_xticklabels)
  122. ax.set_xlabel('Angles')
  123. ax.set_ylabel('distance')
  124. plt.colorbar()
  125. # 将图形保存为图像
  126. canvas = FigureCanvas(fig)
  127. canvas.draw()
  128. bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
  129. width, height = bbox.width * fig.dpi, bbox.height * fig.dpi
  130. image = np.frombuffer(canvas.tostring_rgb(), dtype='unit8').reshape(int(height), int(width), 3)
  131. plt.imsave('energy_map.png', image)
  132. if __name__ == '__main__':
  133. image_path = 'D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG'
  134. process_image(image_path)