# Skan progow deficytu + czy kotwica 'plate' trafia w komponent komory. 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) for R in (200, 300, 400): k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * R + 1, 2 * R + 1)) local_max = cv2.dilate(d * mask, k) deficit = np.where(mask > 0, local_max - d, 0).astype(np.float32) for th in (0.15, 0.2, 0.25, 0.3, 0.35): rec = (deficit > th).astype(np.uint8) n, lab, stats, _ = cv2.connectedComponentsWithStats(rec, 8) big = sorted( (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] > 8000) big.sort(reverse=True) print(f"R={R} th={th}: comps>8k: {big[:6]}")