import cv2 import numpy import numpy as np import torch import resnet_point_feature from old import sift_feature, polar_order backbone_device = "cpu" # device used by the augmentation-side feature path backbone = None # built lazily by get_backbone() def get_backbone(): """Lazily build the ResNet point-feature extractor. Built on first use so that merely importing this module -- which the released inference path does transitively -- does not attempt to load a backbone checkpoint at import time. """ global backbone if backbone is None: backbone = resnet_point_feature.ResNetFeatureExtractor().to(backbone_device) backbone.eval() return backbone class Config: TARGET_SIZE = 100 SIFT_EXTRACTOR = sift_feature.SIFTFeatureExtractorOptimized() SUCCESS_POINT_RATION = 0.2 def exact_features(image): features_dict = polar_order.process_single_image_polar(image) polar_order_features = features_dict["features"] # (120, 8) ellipse_feature = numpy.array([features_dict["ellipse_params"]['cx'] / Config.TARGET_SIZE, features_dict["ellipse_params"]['cy'] / Config.TARGET_SIZE, features_dict["ellipse_params"]['MA'] / Config.TARGET_SIZE, features_dict["ellipse_params"]['ma'] / Config.TARGET_SIZE, features_dict["ellipse_params"]['angle'] / 360.0, features_dict["ellipse_params"]['fit_error'] / Config.TARGET_SIZE, ]) # sift_descriptors np.ndarray 120*32, # success_mask, np.ndarray,120 sift_descriptors, success_mask = sift_feature.extract_sift_for_single_image_pure(image, features_dict[ "matched_points"], Config.SIFT_EXTRACTOR) success_point_ratio = sum(success_mask == True) / len(success_mask) if success_point_ratio < Config.SUCCESS_POINT_RATION: print(success_point_ratio) raise ValueError("边缘点过少") xy_feature = features_dict["matched_points"] / Config.TARGET_SIZE ellipse_feature_expanded = np.tile(ellipse_feature, (120, 1)) # 2. 在第一维度(列方向)上拼接所有特征 all_features = np.concatenate([ polar_order_features, # (120, 8) ellipse_feature_expanded, # (120, 6) xy_feature, # (120, 2) sift_descriptors # (120, 32) ], axis=1) return all_features def extract_features_diff(image, bgr_norm_image=None): """ 提取增强版融合特征 (包含差分特征) 修复:处理角度特征的周期性跳变问题 """ try: # ================= 1. 基础特征提取 ================= # 1.1 极坐标特征 (120, 8) # 假设格式: [t_r, t_theta, r, theta, e_r, e_theta, dis, strategy] # 其中 t_theta (idx=1) 和 theta (idx=3) 是归一化角度 (0~1) features_dict = polar_order.process_single_image_polar(image) if features_dict is None: return None polar_order_features = features_dict["features"].astype(np.float32) # (120, 8) # 1.2 椭圆全局特征 (6,) -> 归一化 ellipse_params = features_dict["ellipse_params"] ellipse_feature = np.array([ ellipse_params['cx'] / Config.TARGET_SIZE, ellipse_params['cy'] / Config.TARGET_SIZE, ellipse_params['MA'] / Config.TARGET_SIZE, ellipse_params['ma'] / Config.TARGET_SIZE, ellipse_params['angle'] / 360.0, ellipse_params['fit_error'] / Config.TARGET_SIZE, ], dtype=np.float32) if bgr_norm_image is None: sift_descriptors, success_mask = sift_feature.extract_sift_for_single_image_pure( image, features_dict["matched_points"], Config.SIFT_EXTRACTOR ) else: coords = [] mask = [] for pt in features_dict["matched_points"]: if pt is not None: coords.append([float(pt[0]), float(pt[1])]) mask.append(True) else: coords.append([0, 0]) mask.append(False) success_mask = np.array(mask, dtype=bool) coords_np = np.array(coords, dtype=np.float32) keypoints_tensor = torch.from_numpy(coords_np).float().to(backbone_device) # 再次检查范围,确保没有超出新图像边界 keypoints_tensor[..., 0] = torch.clamp(keypoints_tensor[..., 0], 0, image.shape[0] - 1) keypoints_tensor[..., 1] = torch.clamp(keypoints_tensor[..., 1], 0, image.shape[1] - 1) bgr_norm_image_tensor = torch.from_numpy(bgr_norm_image.astype(np.float32)).unsqueeze(0).permute(0, 3, 1, 2).to(backbone_device) keypoints_tensor = keypoints_tensor.unsqueeze(0) dense_point_feature = get_backbone()(bgr_norm_image_tensor, keypoints_tensor) #B *N *D # 适配下列代码,改名改格式 mask_expanded = torch.from_numpy(success_mask).to(backbone_device).unsqueeze(0).unsqueeze(-1) filtered_features = dense_point_feature * mask_expanded sift_descriptors = filtered_features.squeeze(0).detach().cpu().numpy() success_point_ratio = np.sum(success_mask) / len(success_mask) if success_point_ratio < Config.SUCCESS_POINT_RATION: # raise ValueError(f"边缘点过少:{success_point_ratio:.2f} < {Config.SUCCESS_POINT_RATION}") return None # 1.4 坐标特征 (120, 2) -> 归一化 matched_points = features_dict["matched_points"] xy_feature = matched_points / Config.TARGET_SIZE # (120, 2) # 1.5 扩展椭圆特征 (120, 6) ellipse_feature_expanded = np.tile(ellipse_feature, (120, 1)) # ================= 2. 差分特征计算 (Delta Features) ================= def compute_circular_diff(features_2d, is_angle=False): """ 计算循环差分,支持角度周期性修正 Args: features_2d: (N, D) 输入特征 is_angle: 是否为角度特征 (0~1 周期) Returns: delta: (N, D) 差分值 """ # 获取前一个时刻的特征 (循环移位) prev_features = np.roll(features_2d, shift=1, axis=0) # 原始差分 delta = features_2d - prev_features if is_angle: # 🔧 角度周期性修正 # 原理:角度差值范围应在 [-0.5, 0.5] 之间 (即 -180° ~ 180°) # 如果 diff > 0.5 (如 0.9), 说明跨过了 0°/360° 边界,实际应为 diff - 1 (如 -0.1) # 如果 diff < -0.5 (如 -0.9), 说明跨过了边界,实际应为 diff + 1 (如 0.1) delta = (delta + 0.5) % 1.0 - 0.5 return delta.astype(np.float32) # 2.1 Polar 差分 (前 6 维) # 索引说明: 0=t_r, 1=t_theta(角度), 2=r, 3=theta(角度), 4=e_r, 5=e_theta polar_geo_features = polar_order_features[:, :6] # (120, 6) # 分离角度列和非角度列 polar_non_angle = polar_geo_features[:, [0, 2, 4]] # (120, 3) -> r 相关 polar_angle = polar_geo_features[:, [1, 3, 5]] # (120, 3) -> theta 相关 # 分别计算差分 polar_non_angle_delta = compute_circular_diff(polar_non_angle, is_angle=False) polar_angle_delta = compute_circular_diff(polar_angle, is_angle=True) # 恢复原始列顺序 [0, 1, 2, 3, 4, 5] polar_delta = np.zeros_like(polar_geo_features) polar_delta[:, [0, 2, 4]] = polar_non_angle_delta polar_delta[:, [1, 3, 5]] = polar_angle_delta # 2.2 XY 坐标差分 (2 维) # 坐标是笛卡尔空间,不需要周期性修正 (除非图像是环面的,但这里不是) xy_delta = compute_circular_diff(xy_feature, is_angle=False) # (120, 2) # 2.3 SIFT 描述子差分 (32 维) # SIFT 是向量空间,不需要周期性修正 sift_delta = compute_circular_diff(sift_descriptors, is_angle=False) # (120, 32) # ================= 3. 特征拼接 ================= # 3.1 静态特征拼接 (48 维) static_features = np.concatenate([ polar_order_features, # (120, 8) ellipse_feature_expanded, # (120, 6) xy_feature, # (120, 2) sift_descriptors # (120, 32) or (120, 448) ], axis=1) # 3.2 动态差分特征拼接 (40 维) dynamic_features = np.concatenate([ polar_delta, # (120, 6) - 已修复角度跳变 xy_delta, # (120, 2) # sift_delta # (120, 32) or (120, 448) ], axis=1) # 3.3 最终融合 (88 维) all_features = np.concatenate([ static_features, dynamic_features ], axis=1) # 验证维度 # assert all_features.shape == (120, 88), f"特征维度错误:{all_features.shape}" assert all_features.shape == (120, 472), f"特征维度错误:{all_features.shape}" # 🔍 可选:调试检查角度差分是否合理 # max_angle_diff = np.max(np.abs(polar_delta[:, [1, 3, 5]])) # assert max_angle_diff <= 0.5, f"角度差分异常,最大值超过 0.5 (180 度): {max_angle_diff}" return all_features except Exception as e: print(f"特征提取失败:{str(e)}") import traceback traceback.print_exc() return None if __name__ == "__main__": image_path = "/path/to/circle_id/images/images/00059.png" img = cv2.imread(image_path) # fea = exact_features(img) # print(fea.shape) image_resized = cv2.resize(img, (100, 100), interpolation=cv2.INTER_AREA) NORM_MEAN_IMG = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 1, -1) NORM_STD_IMG = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 1, -1) # --- 处理 Image --- image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img_norm = (image / 255.0 - NORM_MEAN_IMG) / NORM_STD_IMG fea = extract_features_diff(img, img_norm.astype(np.float32))