feature_lbp.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import cv2
  2. import numpy as np
  3. import sys
  4. import matplotlib.pyplot as plt
  5. from skimage import feature
  6. import os
  7. # 定义LBP算子的计算函数
  8. def calculate_lbp_pixel(center, pixels):
  9. lbp_code = 0
  10. for i, pixel in enumerate(pixels):
  11. lbp_code |= (pixel >= center) << i
  12. return lbp_code
  13. # 定义LBP特征提取函数
  14. def extract_lbp_features(image, radius, num_points):
  15. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  16. height, width = gray.shape
  17. lbp_image = np.zeros((height, width), dtype=np.uint8)
  18. for y in range(radius, height - radius):
  19. for x in range(radius, width - radius):
  20. center = gray[y, x]
  21. pixels = [
  22. gray[y - radius, x - radius],
  23. gray[y - radius, x],
  24. gray[y - radius, x + radius],
  25. gray[y, x + radius],
  26. gray[y + radius, x + radius],
  27. gray[y + radius, x],
  28. gray[y + radius, x - radius],
  29. gray[y, x - radius]
  30. ]
  31. lbp_code = calculate_lbp_pixel(center, pixels)
  32. lbp_image[y, x] = lbp_code
  33. hist = np.histogram(lbp_image, bins=np.arange(0, num_points + 2), range=(0, num_points + 1))[0]
  34. hist = hist.astype("float")
  35. hist /= (hist.sum() + 1e-6) # 归一化
  36. return hist
  37. # 加载图像
  38. # image = cv2.imread('D:\\hiddz\\SAR\\test_data\\TEST\\2S1\\HB14937.JPG')
  39. # # 设置LBP算子的半径和邻域点数
  40. # radius = 1
  41. # num_points = 8 * radius
  42. #
  43. # # 提取LBP特征
  44. # lbp_features = extract_lbp_features(image, radius, num_points)
  45. #
  46. # # 打印特征向量
  47. # print("LBP特征向量:")
  48. # print(lbp_features)
  49. #
  50. # image_path = 'D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG'
  51. #
  52. # # 读取图像并转换为灰度图
  53. # image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  54. # # 计算LBP特征
  55. # radius = 1
  56. # n_points = 8 * radius
  57. # lbp_image = feature.local_binary_pattern(image, n_points, radius, method='uniform')
  58. # cv2.imwrite('image_features/image_with_lbp_keypoints.jpg', lbp_image)
  59. # # 显示原始图像和LBP图像
  60. #
  61. # plt.imshow(lbp_image, cmap='gray')
  62. # plt.title('LBP Image')
  63. # plt.show()
  64. def process_image(image_path):
  65. image = cv2.imread(image_path)
  66. # 设置LBP算子的半径和邻域点数
  67. radius = 1
  68. num_points = 8 * radius
  69. # 提取LBP特征
  70. lbp_features = extract_lbp_features(image, radius, num_points)
  71. # 读取图像并转换为灰度图
  72. image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  73. # 计算LBP特征
  74. radius = 1
  75. n_points = 8 * radius
  76. lbp_image = feature.local_binary_pattern(image, n_points, radius, method='uniform')
  77. cv2.imwrite('image_features/image_with_lbp_keypoints.jpg', lbp_image)
  78. # LBP图像
  79. plt.imshow(lbp_image, cmap='gray')
  80. plt.title('LBP Image')
  81. folder_name = os.path.basename(os.path.dirname(image_path))
  82. # 创建保存图像的文件夹
  83. save_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lbp', folder_name)
  84. os.makedirs(save_folder, exist_ok=True)
  85. # 保存图像
  86. save_path = os.path.join(save_folder, 'lbp_' + os.path.basename(image_path))
  87. plt.imsave(save_path, lbp_image, cmap='gray')
  88. print(save_path)
  89. if __name__ == "__main__":
  90. if len(sys.argv) != 2:
  91. sys.exit(1)
  92. image_path = sys.argv[1]
  93. process_image(image_path)