import cv2 import numpy as np def resize_and_paste_back_with_repair_mask( image_path, mask_path, scale_width=1.0, scale_height=1.0, output_image_path="output_with_resized_obj.jpg", output_repair_mask_path="repair_mask.png" ): """ 抠出物体 → 缩放 → 贴回原图,并生成用于 AI 修复的 mask。 Returns: output_img: 合成后的图像 repair_mask: 修复用二值 mask(白色=需修复) """ # 1. 加载原图和掩码 image = cv2.imread(image_path) mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE) if image is None or mask is None: raise FileNotFoundError("Image or mask not found!") h, w = image.shape[:2] assert mask.shape == (h, w), "Mask and image size mismatch!" # 2. 找到物体边界框 coords = cv2.findNonZero(mask) if coords is None: raise ValueError("No object in mask!") x, y, obj_w, obj_h = cv2.boundingRect(coords) # 3. 计算原物体中心 center_x = x + obj_w // 2 center_y = y + obj_h // 2 # 4. 抠出 RGBA 物体 rgba = np.dstack([image, mask]) obj_rgba = rgba[y:y+obj_h, x:x+obj_w] # 5. 缩放物体 new_w = max(1, int(obj_w * scale_width)) new_h = max(1, int(obj_h * scale_height)) resized_obj = cv2.resize(obj_rgba, (new_w, new_h), interpolation=cv2.INTER_AREA) # 6. 计算新位置(保持中心对齐) new_x = center_x - new_w // 2 new_y = center_y - new_h // 2 # 7. 计算贴图的有效区域(保证 src 和 dst 尺寸一致) dst_x1 = max(0, new_x) dst_y1 = max(0, new_y) dst_x2 = min(w, new_x + new_w) dst_y2 = min(h, new_y + new_h) src_x1 = dst_x1 - new_x src_y1 = dst_y1 - new_y src_x2 = src_x1 + (dst_x2 - dst_x1) src_y2 = src_y1 + (dst_y2 - dst_y1) # Clamp to resized_obj bounds src_x1 = max(0, int(src_x1)) src_y1 = max(0, int(src_y1)) src_x2 = min(new_w, int(src_x2)) src_y2 = min(new_h, int(src_y2)) # Update final coordinates dst_x_start, dst_y_start = dst_x1, dst_y1 dst_x_end, dst_y_end = dst_x2, dst_y2 src_x_start, src_y_start = src_x1, src_y1 src_x_end, src_y_end = src_x2, src_y2 # 8. 创建修复 mask repair_mask = np.zeros((h, w), dtype=np.uint8) repair_mask[mask > 0] = 255 # 原物体区域需修复 # 9. 合成图像 & 构建新物体 mask background = image.copy() background[mask > 0] = [255, 255, 255] # 白色填充原物体区域 output_img = background.copy() new_obj_mask = np.zeros((h, w), dtype=np.uint8) if src_x_end > src_x_start and src_y_end > src_y_start: # 提取有效区域 obj_part = resized_obj[src_y_start:src_y_end, src_x_start:src_x_end] alpha = obj_part[:, :, 3].astype(np.float32) / 255.0 alpha_uint8 = (alpha * 255).astype(np.uint8) # Alpha blending bg_part = output_img[dst_y_start:dst_y_end, dst_x_start:dst_x_end] fg_part = obj_part[:, :, :3] blended = fg_part * alpha[..., None] + bg_part * (1 - alpha[..., None]) output_img[dst_y_start:dst_y_end, dst_x_start:dst_x_end] = blended.astype(np.uint8) # 记录新物体覆盖区域 new_obj_mask[dst_y_start:dst_y_end, dst_x_start:dst_x_end] = alpha_uint8 # 10. 生成 repair mask:原区域 - 新物体覆盖区域 kernel = np.ones((5, 5), np.uint8) new_obj_mask_dilated = cv2.dilate(new_obj_mask, kernel, iterations=1) repair_mask = cv2.subtract(repair_mask, new_obj_mask_dilated) repair_mask = np.clip(repair_mask, 0, 255).astype(np.uint8) repair_mask = cv2.dilate(repair_mask, np.ones((3, 3), np.uint8), iterations=1) # 11. 保存 cv2.imwrite(output_image_path, output_img) cv2.imwrite(output_repair_mask_path, repair_mask) print(f"✅ Image saved to: {output_image_path}") print(f"✅ Repair mask saved to: {output_repair_mask_path}") return output_img, repair_mask img, repair_mask = resize_and_paste_back_with_repair_mask( image_path="/mnt/prev_nas/qhy_1/datasets/flux_gen_images/allocentric_002.png", mask_path="/mnt/prev_nas/qhy_1/datasets/flux_gen_images_masks/allocentric_002.png", scale_width=1.0, scale_height=1.4, output_image_path="/mnt/prev_nas/qhy_1/GenSpace/example/imageedit/29/allocentric_002.png", output_repair_mask_path="fatter_repair_mask.png" )