定位印染区域

This commit is contained in:
hesuicong committed 2026-11-16 14:39:29 +08:00
1 parent 28e12c4de5
commit 5dd3472e68
2 files changed
+109 -158

No files matched your search

+106 -158
View File
@@ -1,171 +1,119 @@
import cv2
import numpy as np
def detect_stain_mask(img2, img1, diff_thresh=0.12, grad_thresh=0.03, var_thresh=0.005):
"""
检测图2中的印染区域(相对于图1的差异 + 高梯度 + 高局部方差)
Args:
img2: 图2(清晰但有印染),uint8 RGB
img1: 图1(模糊但纯净),uint8 RGB
Returns:
mask: 浮点掩码 [0,1],1表示印染区域
"""
# 转浮点
f2 = img2.astype(np.float32) / 255.0
f1 = img1.astype(np.float32) / 255.0
# 1. 差异图
diff = np.abs(f2 - f1)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)
# 2. 图2的拉普拉斯梯度(印染边缘高梯度)
gray2 = cv2.cvtColor(f2, cv2.COLOR_RGB2GRAY)
laplacian2 = cv2.Laplacian(gray2, cv2.CV_32F, ksize=3)
grad_mag = np.abs(laplacian2)
# 3. 图2的局部方差(印染区域局部对比度高)
mean2 = cv2.GaussianBlur(f2, (0, 0), 5)
sqr_mean2 = cv2.GaussianBlur(f2 * f2, (0, 0), 5)
local_var = np.mean(sqr_mean2 - mean2 * mean2, axis=2)
# 4. 组合条件
mask_cond = (
(gray_diff > diff_thresh) &
(grad_mag > grad_thresh) &
(local_var > var_thresh)
).astype(np.float32)
# 5. 形态学操作:膨胀连接区域 + 高斯模糊羽化
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
mask = cv2.dilate(mask_cond, kernel, iterations=2)
mask = cv2.GaussianBlur(mask, (15, 15), 0)
# 归一化到 [0, 1]
mask = np.clip(mask, 0, 1)
return mask
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))
def fuse_textures(img2, img1, mask, sharpen_img1=True):
"""
融合两张纹理图
Args:
img2: 底图(清晰但有印染)
img1: 补丁图(模糊但纯净)
mask: 印染掩码 [0,1]
sharpen_img1: 是否对图1做轻微锐化以减少"糊"感
Returns:
fused: 融合结果
"""
f2 = img2.astype(np.float32) / 255.0
f1 = img1.astype(np.float32) / 255.0
# 可选:对图1做轻微锐化
if sharpen_img1:
# 非锐化掩模
blurred = cv2.GaussianBlur(f1, (0, 0), 1.5)
sharpened = cv2.addWeighted(f1, 1.5, blurred, -0.5, 0)
f1_sharp = np.clip(sharpened, 0, 1)
else:
f1_sharp = f1
# 融合:mask区域用图1,其余保留图2
mask_3ch = np.stack([mask, mask, mask], axis=2)
fused = f2 * (1.0 - mask_3ch) + f1_sharp * mask_3ch
# 转回 uint8
fused = np.clip(fused * 255.0, 0, 255).astype(np.uint8)
return fused
# 关键: 先减背景
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]))
def post_process_clahe(img, clip_limit=1.5, grid_size=(8, 8), mix_ratio=0.3):
"""
轻微 CLAHE 后处理(仅 V 通道),与原图混合
"""
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV).astype(np.float32)
h, s, v = cv2.split(hsv)
# CLAHE 需要 uint8
v_u8 = (v / 255.0 * 255).astype(np.uint8)
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=grid_size)
v_adjusted = clahe.apply(v_u8).astype(np.float32)
# 混合
v_mixed = v * (1 - mix_ratio) + v_adjusted * mix_ratio
v_mixed = np.clip(v_mixed, 0, 255)
hsv_out = cv2.merge([h, s, v_mixed]).astype(np.uint8)
return cv2.cvtColor(hsv_out, cv2.COLOR_HSV2RGB)
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():
# === 参数配置 ===
img1_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.qz.jpg" # 图1:模糊但纯净
img2_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.mesh7.png" # 图2:清晰但有印染
output_path = "/home/algo/Documents/openMVS/openMVS/cartoon_texture/data/642713/642713.jpg"
# 印染检测参数(可按实际效果微调)
DIFF_THRESH = 0.05 # 差异阈值
GRAD_THRESH = 0.03 # 梯度阈值
VAR_THRESH = 0.005 # 局部方差阈值
# 后处理参数
APPLY_CLAHE = True # 是否做轻微CLAHE
SHARPEN_IMG1 = True # 是否锐化图1
# === 加载图片 ===
img2 = cv2.imread(img2_path)
img1 = cv2.imread(img1_path)
if img2 is None or img1 is None:
print("Error: Cannot load images")
return
# 确保尺寸一致(以图2为准)
if img1.shape != img2.shape:
print(f"Resizing img1 from {img1.shape} to {img2.shape}")
img1 = cv2.resize(img1, (img2.shape[1], img2.shape[0]))
# 转 RGB(OpenCV 默认 BGR)
img2_rgb = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img1_rgb = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
print("Detecting stain mask...")
mask = detect_stain_mask(img2_rgb, img1_rgb,
diff_thresh=DIFF_THRESH,
grad_thresh=GRAD_THRESH,
var_thresh=VAR_THRESH)
print(f"Mask coverage: {np.mean(mask > 0.5) * 100:.1f}%")
print("Fusing textures...")
fused = fuse_textures(img2_rgb, img1_rgb, mask, sharpen_img1=SHARPEN_IMG1)
if APPLY_CLAHE:
print("Applying CLAHE post-processing...")
fused = post_process_clahe(fused)
# 保存结果
fused_bgr = cv2.cvtColor(fused, cv2.COLOR_RGB2BGR)
cv2.imwrite(output_path, fused_bgr)
# 保存掩码可视化(调试用)
mask_viz = (mask * 255).astype(np.uint8)
cv2.imwrite("stain_mask_debug.png", mask_viz)
print(f"Done! Result saved to {output_path}")
print(f"Debug mask saved to stain_mask_debug.png")
# # 显示结果
# cv2.imshow("Original (mesh7.1)", img2)
# cv2.imshow("Reference (n1)", img1)
# cv2.imshow("Stain Mask", mask_viz)
# cv2.imshow("Fused Result", fused_bgr)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
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()