feature_hog.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # import matplotlib.pyplot as plt
  2. # from skimage.feature import hog
  3. # from skimage import exposure, io
  4. # import cv2
  5. #
  6. #
  7. # def process_image(image_path):
  8. # # 读取图像
  9. # image = io.imread(image_path)
  10. #
  11. # # 提取HOG特征
  12. # features, hog_image = hog(image, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
  13. #
  14. # # 对HOG图像进行对比度增强
  15. # hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
  16. # #
  17. # # # 可视化结果
  18. # # plt.figure(figsize=(10, 5))
  19. # # plt.subplot(121)
  20. # # plt.imshow(image, cmap='gray')
  21. # # plt.title('Original Image')
  22. #
  23. # plt.imshow(hog_image_rescaled, cmap='gray')
  24. # plt.title('HOG Image')
  25. # plt.savefig('path_to_save_image.png')
  26. #
  27. # plt.show()
  28. #
  29. #
  30. # # 在主程序中调用函数并传递图像路径
  31. # if __name__ == "__main__":
  32. # import sys
  33. #
  34. # if len(sys.argv) != 2:
  35. # sys.exit(1)
  36. #
  37. # image_path = sys.argv[1]
  38. # process_image(image_path)
  39. #
  40. import matplotlib.pyplot as plt
  41. from skimage.feature import hog
  42. from skimage import exposure, io
  43. import cv2
  44. import os
  45. import sys
  46. plt.ioff()
  47. def process_image(image_path):
  48. # 读取图像
  49. image = io.imread(image_path)
  50. # 提取HOG特征
  51. features, hog_image = hog(image, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
  52. # 对HOG图像进行对比度增强
  53. hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
  54. # 获取图像所在目录的文件夹名
  55. folder_name = os.path.basename(os.path.dirname(image_path))
  56. # 创建保存图像的文件夹
  57. save_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'hog', folder_name)
  58. os.makedirs(save_folder, exist_ok=True)
  59. # 保存图像
  60. save_path = os.path.join(save_folder, 'hog_' + os.path.basename(image_path))
  61. plt.imsave(save_path, hog_image_rescaled, cmap='gray')
  62. print(save_path)
  63. # 在主程序中调用函数并传递图像路径
  64. if __name__ == "__main__":
  65. if len(sys.argv) != 2:
  66. sys.exit(1)
  67. image_path = sys.argv[1]
  68. process_image(image_path)