12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- # import matplotlib.pyplot as plt
- # from skimage.feature import hog
- # from skimage import exposure, io
- # import cv2
- #
- #
- # def process_image(image_path):
- # # 读取图像
- # image = io.imread(image_path)
- #
- # # 提取HOG特征
- # features, hog_image = hog(image, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
- #
- # # 对HOG图像进行对比度增强
- # hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
- # #
- # # # 可视化结果
- # # plt.figure(figsize=(10, 5))
- # # plt.subplot(121)
- # # plt.imshow(image, cmap='gray')
- # # plt.title('Original Image')
- #
- # plt.imshow(hog_image_rescaled, cmap='gray')
- # plt.title('HOG Image')
- # plt.savefig('path_to_save_image.png')
- #
- # plt.show()
- #
- #
- # # 在主程序中调用函数并传递图像路径
- # if __name__ == "__main__":
- # import sys
- #
- # if len(sys.argv) != 2:
- # sys.exit(1)
- #
- # image_path = sys.argv[1]
- # process_image(image_path)
- #
- import matplotlib.pyplot as plt
- from skimage.feature import hog
- from skimage import exposure, io
- import cv2
- import os
- import sys
- plt.ioff()
- def process_image(image_path):
- # 读取图像
- image = io.imread(image_path)
- # 提取HOG特征
- features, hog_image = hog(image, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=True)
- # 对HOG图像进行对比度增强
- hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
- # 获取图像所在目录的文件夹名
- folder_name = os.path.basename(os.path.dirname(image_path))
- # 创建保存图像的文件夹
- save_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'hog', folder_name)
- os.makedirs(save_folder, exist_ok=True)
- # 保存图像
- save_path = os.path.join(save_folder, 'hog_' + os.path.basename(image_path))
- plt.imsave(save_path, hog_image_rescaled, 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)
|