| 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(白色=需修复) |
| """ |
| |
| 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!" |
|
|
| |
| coords = cv2.findNonZero(mask) |
| if coords is None: |
| raise ValueError("No object in mask!") |
| x, y, obj_w, obj_h = cv2.boundingRect(coords) |
|
|
| |
| center_x = x + obj_w // 2 |
| center_y = y + obj_h // 2 |
|
|
| |
| rgba = np.dstack([image, mask]) |
| obj_rgba = rgba[y:y+obj_h, x:x+obj_w] |
|
|
| |
| 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) |
|
|
| |
| new_x = center_x - new_w // 2 |
| new_y = center_y - new_h // 2 |
|
|
| |
| 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) |
|
|
| |
| 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)) |
|
|
| |
| 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 |
|
|
| |
| repair_mask = np.zeros((h, w), dtype=np.uint8) |
| repair_mask[mask > 0] = 255 |
|
|
| |
| 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) |
|
|
| |
| 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 |
|
|
| |
| 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) |
|
|
| |
| 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" |
| ) |