# import numpy as np # import cv2 # import sys # import os # from skimage.feature import graycomatrix, graycoprops, greycoprops, greycomatrix # from skimage import io, color, img_as_ubyte # import matplotlib.pyplot as plt # # 读取图像 # image = io.imread('HB03333.jpeg') # # # 将图像转换为灰度 # gray_image = io.imread('D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG') # gray_image = img_as_ubyte(gray_image) # # # 定义GLCM参数 # distances = [1, 2, 3, 4] # 邻距离 # angles = [0, np.pi/4, np.pi/2, 3*np.pi/4] # 邻角度 # # # 计算GLCM # glcm = graycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True) # # # 计算GLCM特征 # contrast = graycoprops(glcm, prop='contrast') # dissimilarity = graycoprops(glcm, prop='dissimilarity') # homogeneity = graycoprops(glcm, prop='homogeneity') # energy = graycoprops(glcm, prop='energy') # correlation = graycoprops(glcm, prop='correlation') # # # 打印GLCM特征 # print("Contrast:", contrast) # print("Dissimilarity:", dissimilarity) # print("Homogeneity:", homogeneity) # print("Energy:", energy) # print("Correlation:", correlation) # # 可视化GLCM特征 # # plt.imshow(energy, cmap='hot') # plt.title('GLCM Energy Map') # custom_xticks = np.arange(4) # custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应 # custom_yticklables = ['1', '2', '3', '4'] # plt.yticks(custom_xticks, custom_yticklables) # plt.xticks(custom_xticks, custom_xticklabels) # 设置刻度位置和标签 # plt.xlabel('Angles') # plt.ylabel('distance') # plt.colorbar() # # plt.savefig('D:\\hiddz\\SAR\\test_data\\TEST\A11') # plt.show() # # # # def process_image(image_path): # gray_image = io.imread(image_path) # gray_image = img_as_ubyte(gray_image) # # distances = [1, 2, 3, 4] # angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4] # 邻角度 # # # 计算GLCM # glcm = graycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True) # # # 计算GLCM特征 # contrast = graycoprops(glcm, prop='contrast') # dissimilarity = graycoprops(glcm, prop='dissimilarity') # homogeneity = graycoprops(glcm, prop='homogeneity') # energy = graycoprops(glcm, prop='energy') # correlation = graycoprops(glcm, prop='correlation') # # plt.imshow(energy, cmap='hot') # plt.title('GLCM Energy Map') # custom_xticks = np.arange(4) # custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应 # custom_yticklables = ['1', '2', '3', '4'] # plt.yticks(custom_xticks, custom_yticklables) # plt.xticks(custom_xticks, custom_xticklabels) # 设置刻度位置和标签 # plt.xlabel('Angles') # plt.ylabel('distance') # plt.colorbar() # plt.show() # folder_name = os.path.basename(os.path.dirname(image_path)) # # save_folder = os.path.join(os.path.dirname(os.path.abspath(__path__)), 'glcm', folder_name) # os.makedirs(save_folder, exist_ok=True) # # save_path = os.path.join(save_folder, 'glcm_', os.path.basename(image_path)) # plt.imsave(save_path, energy, cmap = 'gray') # print(save_path) # if __name__ == '__main__': # if len(sys.argv) != 2: # sys.exit(1) # # image_path = sys.argv[1] # process_image(image_path) import numpy as np from skimage import io, img_as_ubyte from skimage.feature import greycomatrix, greycoprops import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas def process_image(image_path): gray_image = io.imread(image_path) gray_image = img_as_ubyte(gray_image) distances = [1, 2, 3, 4] angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4] # 邻角度 # 计算GLCM glcm = greycomatrix(gray_image, distances=distances, angles=angles, symmetric=True, normed=True) # 计算GLCM特征 contrast = greycoprops(glcm, prop='contrast') dissimilarity = greycoprops(glcm, prop='dissimilarity') homogeneity = greycoprops(glcm, prop='homogeneity') energy = greycoprops(glcm, prop='energy') correlation = greycoprops(glcm, prop='correlation') fig, ax = plt.subplots() ax.imshow(energy, cmap='hot') ax.set_title('GLCM Energy Map') custom_xticks = np.arange(4) custom_xticklabels = ['0', 'pi/4', 'pi/2', '3pi/4'] # 自定义刻度标签,与刻度位置一一对应 custom_yticklables = ['1', '2', '3', '4'] ax.set_yticks(custom_xticks) ax.set_yticklabels(custom_yticklables) ax.set_xticks(custom_xticks) ax.set_xticklabels(custom_xticklabels) ax.set_xlabel('Angles') ax.set_ylabel('distance') plt.colorbar() # 将图形保存为图像 canvas = FigureCanvas(fig) canvas.draw() bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) width, height = bbox.width * fig.dpi, bbox.height * fig.dpi image = np.frombuffer(canvas.tostring_rgb(), dtype='unit8').reshape(int(height), int(width), 3) plt.imsave('energy_map.png', image) if __name__ == '__main__': image_path = 'D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG' process_image(image_path)