import cv2 import os # 配置路径 original_images_parent_dir = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/classdata3/val" # 含 class0, class1 等 roi_coords_file = "./roi_coordinates/1_rois.txt" # 你手动标注的唯一一个 ROI 文件 output_parent_dir = "/media/hx/04e879fa-d697-4b02-ac7e-a4148876ebb0/dataset/classdata3/classdata3_cropped" target_size = 640 os.makedirs(output_parent_dir, exist_ok=True) def load_global_rois(txt_path): """加载全局 ROI 坐标""" rois = [] if not os.path.exists(txt_path): print(f"❌ ROI 文件不存在: {txt_path}") return rois with open(txt_path, 'r') as f: for line in f: line = line.strip() if line: try: x, y, w, h = map(int, line.split(',')) rois.append((x, y, w, h)) print(f"📌 加载 ROI: (x={x}, y={y}, w={w}, h={h})") except Exception as e: print(f"⚠️ 无法解析 ROI 行: {line}, 错误: {e}") return rois # 加载全局 ROI rois = load_global_rois(roi_coords_file) if len(rois) == 0: print("❌ 没有加载到任何有效的 ROI 坐标,程序退出。") exit(1) # 遍历所有子目录和图像 for class_name in os.listdir(original_images_parent_dir): class_dir = os.path.join(original_images_parent_dir, class_name) if not os.path.isdir(class_dir): continue # 创建输出类别目录 output_class_dir = os.path.join(output_parent_dir, class_name) os.makedirs(output_class_dir, exist_ok=True) print(f"🔄 处理类别: {class_name}") for img_file in os.listdir(class_dir): if not img_file.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')): continue img_path = os.path.join(class_dir, img_file) base_name, ext = os.path.splitext(img_file) # 读取图像 img = cv2.imread(img_path) if img is None: print(f"❌ 无法读取图像: {img_path}") continue # 对每一个 ROI 进行裁剪 for i, (x, y, w, h) in enumerate(rois): # 检查坐标是否越界 h_img, w_img = img.shape[:2] if x < 0 or y < 0 or x + w > w_img or y + h > h_img: print(f"⚠️ ROI 越界,跳过: {x},{y},{w},{h} in {img_path}") continue roi_img = img[y:y+h, x:x+w] if roi_img.size == 0: print(f"⚠️ 空 ROI 区域: {x},{y},{w},{h} in {img_path}") continue # resize roi_resized = cv2.resize(roi_img, (target_size, target_size), interpolation=cv2.INTER_AREA) # 保存文件名:原名 + _roi0, _roi1... suffix = f"_roi{i}" if len(rois) > 1 else "" save_filename = f"{base_name}{suffix}{ext}" save_path = os.path.join(output_class_dir, save_filename) cv2.imwrite(save_path, roi_resized) print(f"✅ 保存: {save_path}") print("🎉 所有图像已根据全局 ROI 裁剪并保存完成!")