import cv2 import numpy as np def normalize_background(img, ksize=251): """减去大核背景, 消除整体色调/亮度偏移""" blur = cv2.GaussianBlur(img, (ksize, ksize), 0) return img.astype(np.float32) - blur.astype(np.float32) def detect_cast(qz, mesh7, diff_thresh=12, min_area=300, feather=25): """背景归一化后差分, 检出局部色偏/脏斑""" g = cv2.cvtColor(mesh7, cv2.COLOR_BGR2GRAY) valid = ((g > 8) & (g < 248)).astype(np.uint8) valid = cv2.morphologyEx(valid, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8)) # 关键: 先减背景 qz_res = normalize_background(qz) m_res = normalize_background(mesh7) lab_qz = cv2.cvtColor(np.clip(qz_res + 128, 0, 255).astype(np.uint8), cv2.COLOR_BGR2Lab).astype(np.float32) lab_m = cv2.cvtColor(np.clip(m_res + 128, 0, 255).astype(np.uint8), cv2.COLOR_BGR2Lab).astype(np.float32) diff = (np.abs(lab_qz[:, :, 0] - lab_m[:, :, 0]) + 0.5 * np.abs(lab_qz[:, :, 1] - lab_m[:, :, 1]) + 0.5 * np.abs(lab_qz[:, :, 2] - lab_m[:, :, 2])) fg = ((diff > diff_thresh) & (valid > 0)).astype(np.uint8) num, labels, stats, _ = cv2.connectedComponentsWithStats(fg, connectivity=8) clean = np.zeros_like(fg) kept = 0 for i in range(1, num): if stats[i, cv2.CC_STAT_AREA] >= min_area: clean[labels == i] = 1 kept += 1 print(f" 检出 {kept} 个脏斑块") clean = cv2.morphologyEx(clean, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8)) clean = cv2.dilate(clean, np.ones((7, 7), np.uint8), iterations=2) mask = cv2.GaussianBlur(clean.astype(np.float32), (feather, feather), 0) mask = mask * valid.astype(np.float32) return np.clip(mask, 0, 1), diff def correct_from_qz(mesh7, qz, mask, k=1.0): """保留 mesh7 清晰度, 用 qz 颜色偏移修正(不替换像素)""" f_m = mesh7.astype(np.float32) f_q = qz.astype(np.float32) delta = np.clip(f_q - f_m, -50, 50) m = mask[:, :, np.newaxis] return np.clip(f_m + delta * m * k, 0, 255).astype(np.uint8) def main(): qz_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.qz.jpg" mesh7_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.mesh7.png" out_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.jpg" mask_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/stain_mask_debug.png" diff_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/diff_map.png" TARGET = 4096 print("读取图片...") qz = cv2.imread(qz_path) mesh7 = cv2.imread(mesh7_path) if qz is None or mesh7 is None: print("读取失败"); return if qz.shape[0] != TARGET: qz = cv2.resize(qz, (TARGET, TARGET), interpolation=cv2.INTER_AREA) if mesh7.shape[0] != TARGET: mesh7 = cv2.resize(mesh7, (TARGET, TARGET), interpolation=cv2.INTER_AREA) print(f"统一到 {TARGET}x{TARGET}") # 背景归一化后扫阈值 print("背景归一化 + 扫描阈值...") qz_res = normalize_background(qz) m_res = normalize_background(mesh7) lab_qz = cv2.cvtColor(np.clip(qz_res + 128, 0, 255).astype(np.uint8), cv2.COLOR_BGR2Lab).astype(np.float32) lab_m = cv2.cvtColor(np.clip(m_res + 128, 0, 255).astype(np.uint8), cv2.COLOR_BGR2Lab).astype(np.float32) diff_l = np.abs(lab_qz[:, :, 0] - lab_m[:, :, 0]) g = cv2.cvtColor(mesh7, cv2.COLOR_BGR2GRAY) valid = ((g > 8) & (g < 248)).astype(np.uint8) valid = cv2.morphologyEx(valid, cv2.MORPH_CLOSE, np.ones((5, 5), np.uint8)) results = [] for dt in range(3, 40): fg = ((diff_l > dt) & (valid > 0)).astype(np.uint8) num, _, stats, _ = cv2.connectedComponentsWithStats(fg, connectivity=8) big = sum(1 for i in range(1, num) if stats[i, cv2.CC_STAT_AREA] >= 300) cov = fg.mean() * 100 results.append((dt, big, cov)) # 选: 有检出 且 覆盖率在 0.1%~10% candidates = [(dt, big, cov) for dt, big, cov in results if big >= 1 and 0.1 <= cov <= 10] if candidates: dt = min(candidates, key=lambda x: abs(x[2] - 2.0))[0] print(f" 选用 diff_thresh={dt}") else: any_big = [(dt, big, cov) for dt, big, cov in results if big >= 1] dt = min(any_big, key=lambda x: x[2])[0] if any_big else 12 print(f" 兜底 diff_thresh={dt}") mask, diff = detect_cast(qz, mesh7, diff_thresh=dt) cv2.imwrite(mask_path, (mask * 255).astype(np.uint8)) dmax = diff.max() if dmax > 0: cv2.imwrite(diff_path, (diff / dmax * 255).astype(np.uint8)) cov = np.mean(mask > 0.3) * 100 print(f"掩码覆盖率: {cov:.2f}%") print("颜色修正(保留 mesh7 清晰度)...") res = correct_from_qz(mesh7, qz, mask, k=1.0) cv2.imwrite(out_path, res) print(f"完成 -> {out_path}") print(f"掩码 -> {mask_path}") if __name__ == "__main__": main()