# Test 2: LOKALNY deficyt glebi — komora = region maski znacznie glebiej niz # jego otoczenie (obrecz siedziska), nie niz globalna mediana. # deficit(px) = max glebi w promieniu R - glebi(px); komora ma duzy deficit, # bo jej sasiedztwo siega jasnego obrysu; oparcie/dalekie siedzisko - maly. import cv2 import numpy as np import torch from PIL import Image from transformers import AutoImageProcessor, AutoModelForDepthEstimation MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf" IMG = r"C:\xampp\htdocs\vilmax\vilmal\tmp\recolor-svc-storage-granat-v4.png" MATTE = r"C:\xampp\htdocs\vilmax\vilmal\tmp\birefnet-matte-6413.png" proc = AutoImageProcessor.from_pretrained(MODEL_ID) model = AutoModelForDepthEstimation.from_pretrained(MODEL_ID).to("cuda").eval() img = Image.open(IMG).convert("RGB") W, H = img.size inputs = proc(images=img, return_tensors="pt").to("cuda") with torch.no_grad(): d = model(**inputs).predicted_depth[0].cpu().numpy() d = cv2.resize(d, (W, H), interpolation=cv2.INTER_LINEAR) d = (d - d.min()) / max(1e-6, d.max() - d.min()) matte = np.asarray(Image.open(MATTE).convert("L").resize((W, H), Image.BILINEAR), np.float32) / 255.0 mask = (matte > 0.5).astype(np.uint8) # lokalny maks glebi TYLKO w obrebie maski: tlo=0 przed dylatacja, # wynik ograniczony do maski — komora "wyciaga" jasna obrecz siedziska R = 260 # ~8% szerokosci — musi przekroczyc szerokosc obreczy komory k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * R + 1, 2 * R + 1)) d_in = d * mask local_max = cv2.dilate(d_in, k) deficit = np.where(mask > 0, local_max - d, 0).astype(np.float32) for th in (0.15, 0.2, 0.25): rec = (deficit > th).astype(np.uint8) n, lab, stats, _ = cv2.connectedComponentsWithStats(rec, 8) big = [(int(s[cv2.CC_STAT_AREA]), (int(s[cv2.CC_STAT_LEFT]), int(s[cv2.CC_STAT_TOP]), int(s[cv2.CC_STAT_WIDTH]), int(s[cv2.CC_STAT_HEIGHT]))) for s in stats[1:] if s[cv2.CC_STAT_AREA] > 5000] big.sort(reverse=True) print(f"th={th}: px={int(rec.sum())} ({rec.sum()/max(1,mask.sum()):.1%} maski) komponenty>5k: {big[:6]}") th = 0.2 rec = (deficit > th).astype(np.uint8) out = np.asarray(img, np.uint8).copy() out[rec > 0] = (out[rec > 0] * 0.35 + np.array([255, 60, 60]) * 0.65).astype(np.uint8) Image.fromarray(out).resize((W // 3, H // 3)).save(r"C:\xampp\htdocs\vilmax\vilmal\tmp\depth-cavity-local.png") Image.fromarray((np.clip(deficit, 0, 1) * 255).astype(np.uint8)).resize((W // 3, H // 3)).save( r"C:\xampp\htdocs\vilmax\vilmal\tmp\depth-deficit-map.png" ) print("overlay zapisany")