| import numpy as np | |
| import cv2 | |
| def overlay_mask(rgb_image: np.ndarray, mask: np.ndarray, alpha=0.45): | |
| # mask: [H,W] 0/1; resize mask to match image | |
| h, w = rgb_image.shape[:2] | |
| mask_rs = cv2.resize(mask.astype(np.uint8), (w, h), interpolation=cv2.INTER_NEAREST) | |
| overlay = rgb_image.copy() | |
| # red overlay where crack | |
| red = np.zeros_like(rgb_image) | |
| red[..., 0] = 255 | |
| overlay = np.where(mask_rs[..., None] == 1, | |
| (alpha * red + (1 - alpha) * overlay).astype(np.uint8), | |
| overlay) | |
| return overlay |