123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- import cv2
- import numpy as np
- import sys
- import matplotlib.pyplot as plt
- from skimage import feature
- import os
- # 定义LBP算子的计算函数
- def calculate_lbp_pixel(center, pixels):
- lbp_code = 0
- for i, pixel in enumerate(pixels):
- lbp_code |= (pixel >= center) << i
- return lbp_code
- # 定义LBP特征提取函数
- def extract_lbp_features(image, radius, num_points):
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- height, width = gray.shape
- lbp_image = np.zeros((height, width), dtype=np.uint8)
- for y in range(radius, height - radius):
- for x in range(radius, width - radius):
- center = gray[y, x]
- pixels = [
- gray[y - radius, x - radius],
- gray[y - radius, x],
- gray[y - radius, x + radius],
- gray[y, x + radius],
- gray[y + radius, x + radius],
- gray[y + radius, x],
- gray[y + radius, x - radius],
- gray[y, x - radius]
- ]
- lbp_code = calculate_lbp_pixel(center, pixels)
- lbp_image[y, x] = lbp_code
- hist = np.histogram(lbp_image, bins=np.arange(0, num_points + 2), range=(0, num_points + 1))[0]
- hist = hist.astype("float")
- hist /= (hist.sum() + 1e-6) # 归一化
- return hist
- # 加载图像
- # image = cv2.imread('D:\\hiddz\\SAR\\test_data\\TEST\\2S1\\HB14937.JPG')
- # # 设置LBP算子的半径和邻域点数
- # radius = 1
- # num_points = 8 * radius
- #
- # # 提取LBP特征
- # lbp_features = extract_lbp_features(image, radius, num_points)
- #
- # # 打印特征向量
- # print("LBP特征向量:")
- # print(lbp_features)
- #
- # image_path = 'D:\\hiddz\\SAR\\test_data\\TEST\\A11\\HB14931.JPG'
- #
- # # 读取图像并转换为灰度图
- # image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
- # # 计算LBP特征
- # radius = 1
- # n_points = 8 * radius
- # lbp_image = feature.local_binary_pattern(image, n_points, radius, method='uniform')
- # cv2.imwrite('image_features/image_with_lbp_keypoints.jpg', lbp_image)
- # # 显示原始图像和LBP图像
- #
- # plt.imshow(lbp_image, cmap='gray')
- # plt.title('LBP Image')
- # plt.show()
- def process_image(image_path):
- image = cv2.imread(image_path)
- # 设置LBP算子的半径和邻域点数
- radius = 1
- num_points = 8 * radius
- # 提取LBP特征
- lbp_features = extract_lbp_features(image, radius, num_points)
- # 读取图像并转换为灰度图
- image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
- # 计算LBP特征
- radius = 1
- n_points = 8 * radius
- lbp_image = feature.local_binary_pattern(image, n_points, radius, method='uniform')
- cv2.imwrite('image_features/image_with_lbp_keypoints.jpg', lbp_image)
- # LBP图像
- plt.imshow(lbp_image, cmap='gray')
- plt.title('LBP Image')
- folder_name = os.path.basename(os.path.dirname(image_path))
- # 创建保存图像的文件夹
- save_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lbp', folder_name)
- os.makedirs(save_folder, exist_ok=True)
- # 保存图像
- save_path = os.path.join(save_folder, 'lbp_' + os.path.basename(image_path))
- plt.imsave(save_path, lbp_image, 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)
|