Compare commits

..

1 Commits

Author SHA1 Message Date
Adriano 0296083e3c feat: add_template_view - multi-template ensemble (Halcon-style)
Aggiunge una view extra al matcher gia addestrato. Le varianti
della nuova view vengono APPENDATE a self.variants col tag view_idx
e partecipano al pruning/matching come le altre.

NCC verify usa il template della view che ha matchato (via
_get_view_template + parametro view_idx propagato a _verify_ncc).

Halcon-equivalent: create_aniso_shape_model con fusione N viste.
Use case: pezzo che cambia aspetto (chiaro/scuro, prima/dopo
trattamento, illuminazioni diverse) → un solo matcher robusto
invece di N matcher distinti.

API:
    m.train(template_chiaro)
    m.add_template_view(template_scuro)
    m.find(scene)  # match su entrambi gli aspetti

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 22:37:13 +02:00
+86 -122
View File
@@ -125,6 +125,11 @@ class _Variant:
kw: int kw: int
cx_local: float # centro-modello dentro al bbox kernel cx_local: float # centro-modello dentro al bbox kernel
cy_local: float cy_local: float
# Indice template view (X feature - multi-template ensemble).
# 0 = template principale del train(); 1+ = view aggiunte via
# add_template_view(). Usato in _verify_ncc/_compute_recall per
# scegliere il template gray corretto per match.
view_idx: int = 0
class LineShapeMatcher: class LineShapeMatcher:
@@ -170,6 +175,11 @@ class LineShapeMatcher:
self.template_gray: np.ndarray | None = None self.template_gray: np.ndarray | None = None
# Maschera usata in training (propagata al refine per coerenza). # Maschera usata in training (propagata al refine per coerenza).
self._train_mask: np.ndarray | None = None self._train_mask: np.ndarray | None = None
# Multi-template ensemble (X feature): N view dello stesso pezzo
# (chiari/scuri, condizioni diverse). Template principale e' [0],
# view aggiunte via add_template_view() sono [1+]. Match restituisce
# la view che ha matchato meglio.
self._view_templates: list[tuple[np.ndarray, np.ndarray | None]] = []
# --- Helpers ------------------------------------------------------- # --- Helpers -------------------------------------------------------
@@ -226,120 +236,6 @@ class LineShapeMatcher:
np.array(picked_y, np.int32), np.array(picked_y, np.int32),
np.array(picked_b, np.int8)) np.array(picked_b, np.int8))
# --- Save / Load (Halcon-style write_shape_model / read_shape_model)
def save_model(self, path: str) -> None:
"""Salva matcher addestrato su disco (formato .npz).
Persiste: parametri, template_gray, mask, e tutte le varianti
pre-computate (con piramide). Halcon-equivalent write_shape_model.
Caso d'uso: training offline su workstation, deploy su macchina
di linea senza re-train (zero secondi di startup matching).
"""
if not self.variants:
raise RuntimeError("Modello non addestrato: chiamare train() prima.")
# Flatten varianti in array piatti (npz non ama dataclass nested)
n_vars = len(self.variants)
n_levels = len(self.variants[0].levels)
var_meta = np.zeros((n_vars, 6), dtype=np.float32) # ang, scale, kh, kw, cxl, cyl
all_dx, all_dy, all_bin, all_offsets = [], [], [], []
offset = 0
all_offsets_per_level = [[] for _ in range(n_levels)]
all_dx_per_level = [[] for _ in range(n_levels)]
all_dy_per_level = [[] for _ in range(n_levels)]
all_bin_per_level = [[] for _ in range(n_levels)]
for vi, var in enumerate(self.variants):
var_meta[vi] = (
var.angle_deg, var.scale, var.kh, var.kw,
var.cx_local, var.cy_local,
)
for li, lvl in enumerate(var.levels):
all_offsets_per_level[li].append(len(all_dx_per_level[li]))
all_dx_per_level[li].extend(lvl.dx.tolist())
all_dy_per_level[li].extend(lvl.dy.tolist())
all_bin_per_level[li].extend(lvl.bin.tolist())
for li in range(n_levels):
all_offsets_per_level[li].append(len(all_dx_per_level[li]))
out = {
"_format_version": np.array([1], dtype=np.int32),
"params": np.array([
self.num_features, self.weak_grad, self.strong_grad,
self.angle_range_deg[0], self.angle_range_deg[1],
self.angle_step_deg,
self.scale_range[0], self.scale_range[1], self.scale_step,
self.spread_radius, self.min_feature_spacing,
self.pyramid_levels, self.top_score_factor,
int(self.use_polarity),
], dtype=np.float64),
"template_gray": self.template_gray,
"train_mask": self._train_mask,
"var_meta": var_meta,
"n_levels": np.array([n_levels], dtype=np.int32),
}
for li in range(n_levels):
out[f"dx_l{li}"] = np.asarray(all_dx_per_level[li], dtype=np.int32)
out[f"dy_l{li}"] = np.asarray(all_dy_per_level[li], dtype=np.int32)
out[f"bin_l{li}"] = np.asarray(all_bin_per_level[li], dtype=np.int8)
out[f"offsets_l{li}"] = np.asarray(all_offsets_per_level[li], dtype=np.int32)
np.savez_compressed(path, **out)
@classmethod
def load_model(cls, path: str) -> "LineShapeMatcher":
"""Carica matcher pre-addestrato da .npz salvato con save_model.
Halcon-equivalent read_shape_model. Bypassa completamente train():
deploy production = istantaneo.
"""
data = np.load(path, allow_pickle=False)
params = data["params"]
m = cls(
num_features=int(params[0]),
weak_grad=float(params[1]),
strong_grad=float(params[2]),
angle_range_deg=(float(params[3]), float(params[4])),
angle_step_deg=float(params[5]),
scale_range=(float(params[6]), float(params[7])),
scale_step=float(params[8]),
spread_radius=int(params[9]),
min_feature_spacing=int(params[10]),
pyramid_levels=int(params[11]),
top_score_factor=float(params[12]),
use_polarity=bool(int(params[13])),
)
tpl = data["template_gray"]
if tpl.ndim > 0 and tpl.size > 0:
m.template_gray = tpl
m.template_size = (tpl.shape[1], tpl.shape[0])
mk = data["train_mask"]
m._train_mask = mk if mk.size > 0 else None
var_meta = data["var_meta"]
n_levels = int(data["n_levels"][0])
offsets_l = [data[f"offsets_l{li}"] for li in range(n_levels)]
dx_l = [data[f"dx_l{li}"] for li in range(n_levels)]
dy_l = [data[f"dy_l{li}"] for li in range(n_levels)]
bin_l = [data[f"bin_l{li}"] for li in range(n_levels)]
m.variants = []
n_vars = var_meta.shape[0]
for vi in range(n_vars):
ang, scale, kh, kw, cxl, cyl = var_meta[vi]
levels = []
for li in range(n_levels):
i0 = int(offsets_l[li][vi])
i1 = int(offsets_l[li][vi + 1])
levels.append(_LevelFeatures(
dx=dx_l[li][i0:i1].copy(),
dy=dy_l[li][i0:i1].copy(),
bin=bin_l[li][i0:i1].copy(),
n=i1 - i0,
))
m.variants.append(_Variant(
angle_deg=float(ang), scale=float(scale),
levels=levels, kh=int(kh), kw=int(kw),
cx_local=float(cxl), cy_local=float(cyl),
))
return m
def set_angle_range_around( def set_angle_range_around(
self, center_deg: float, tolerance_deg: float, self, center_deg: float, tolerance_deg: float,
) -> None: ) -> None:
@@ -428,8 +324,60 @@ class LineShapeMatcher:
self._train_mask = mask_full.copy() self._train_mask = mask_full.copy()
self.variants.clear() self.variants.clear()
# Reset view list: template principale = view 0
self._view_templates = [(gray.copy(), mask_full.copy())]
# Invalida cache feature di refine: il template e cambiato. # Invalida cache feature di refine: il template e cambiato.
self._refine_feat_cache = {} self._refine_feat_cache = {}
self._build_variants_for_view(gray, mask_full, view_idx=0)
self._dedup_variants()
return len(self.variants)
def add_template_view(
self, template_bgr: np.ndarray, mask: np.ndarray | None = None,
) -> int:
"""Aggiunge una view template extra all'ensemble (Halcon-style
create_aniso_shape_model con fusione N viste).
Genera varianti del nuovo template con stessi parametri (range
angle/scale) e le APPENDE a self.variants. NCC/recall usano
automaticamente il template della view che ha matchato.
Use case: pezzo che cambia aspetto (chiaro/scuro, prima/dopo
trattamento, illuminazioni diverse) → un solo matcher resistente.
Ritorna numero TOTALE varianti dopo l'aggiunta. Le view sono
indicizzate da 1 in poi (0 e' il template del train).
"""
if not self.variants:
raise RuntimeError(
"Chiamare train(template_principale) prima di add_template_view")
gray = self._to_gray(template_bgr)
h, w = gray.shape
if (w, h) != self.template_size:
# Resize per coerenza con bbox/poly
gray = cv2.resize(gray, self.template_size, interpolation=cv2.INTER_LINEAR)
if mask is not None:
mask = cv2.resize(mask, self.template_size, interpolation=cv2.INTER_NEAREST)
if mask is None:
mask_full = np.full(gray.shape, 255, dtype=np.uint8)
else:
mask_full = (mask > 0).astype(np.uint8) * 255
view_idx = len(self._view_templates)
self._view_templates.append((gray.copy(), mask_full.copy()))
n_before = len(self.variants)
self._build_variants_for_view(gray, mask_full, view_idx=view_idx)
self._dedup_variants()
return len(self.variants) - n_before
def _build_variants_for_view(
self, gray: np.ndarray, mask_full: np.ndarray, view_idx: int,
) -> None:
"""Estrae varianti rotate+scalate per UNA view template.
Estrazione algorithm identica al train() originale, separato per
riuso da add_template_view (multi-template ensemble).
"""
h, w = gray.shape
for s in self._scale_list(): for s in self._scale_list():
sw = max(16, int(round(w * s))) sw = max(16, int(round(w * s)))
sh = max(16, int(round(h * s))) sh = max(16, int(round(h * s)))
@@ -483,9 +431,8 @@ class LineShapeMatcher:
levels=levels, levels=levels,
kh=kh, kw=kw, kh=kh, kw=kw,
cx_local=float(cx_local), cy_local=float(cy_local), cx_local=float(cx_local), cy_local=float(cy_local),
view_idx=view_idx,
)) ))
self._dedup_variants()
return len(self.variants)
def _dedup_variants(self) -> int: def _dedup_variants(self) -> int:
"""Rimuove varianti con feature-set identico (post-quantizzazione). """Rimuove varianti con feature-set identico (post-quantizzazione).
@@ -854,9 +801,23 @@ class LineShapeMatcher:
s2, cx2, cy2 = _score_at_angle(x2) s2, cx2, cy2 = _score_at_angle(x2)
return best return best
def _get_view_template(
self, view_idx: int,
) -> tuple[np.ndarray | None, np.ndarray | None]:
"""Ritorna (template_gray, mask) per la view specificata.
view_idx 0 = template principale (train), 1+ = view extra
aggiunte via add_template_view. Usato per scegliere il template
corretto in NCC/recall verification quando il matcher e'
ensemble multi-template.
"""
if 0 <= view_idx < len(self._view_templates):
return self._view_templates[view_idx]
return self.template_gray, self._train_mask
def _verify_ncc( def _verify_ncc(
self, scene_gray: np.ndarray, cx: float, cy: float, self, scene_gray: np.ndarray, cx: float, cy: float,
angle_deg: float, scale: float, angle_deg: float, scale: float, view_idx: int = 0,
) -> float: ) -> float:
"""NCC tra template warpato alla pose e scena sottostante. """NCC tra template warpato alla pose e scena sottostante.
@@ -868,9 +829,9 @@ class LineShapeMatcher:
il matcher linemod può dare score alto su texture generiche ma il matcher linemod può dare score alto su texture generiche ma
sovrapponendo il template gray i pixel non corrispondono. sovrapponendo il template gray i pixel non corrispondono.
""" """
if self.template_gray is None: t, train_mask = self._get_view_template(view_idx)
if t is None:
return 1.0 return 1.0
t = self.template_gray
h, w = t.shape h, w = t.shape
cx_t = (w - 1) / 2.0 cx_t = (w - 1) / 2.0
cy_t = (h - 1) / 2.0 cy_t = (h - 1) / 2.0
@@ -895,8 +856,8 @@ class LineShapeMatcher:
t, M, (cw, ch), t, M, (cw, ch),
flags=cv2.INTER_LINEAR, borderValue=0, flags=cv2.INTER_LINEAR, borderValue=0,
) )
if self._train_mask is not None: if train_mask is not None:
mask_src = self._train_mask mask_src = train_mask
else: else:
mask_src = np.full_like(t, 255) mask_src = np.full_like(t, 255)
mask_w = cv2.warpAffine( mask_w = cv2.warpAffine(
@@ -1299,7 +1260,10 @@ class LineShapeMatcher:
# ranking/visualizzazione (uno score 1.0 vero richiede sia # ranking/visualizzazione (uno score 1.0 vero richiede sia
# match shape sia template gray identici). # match shape sia template gray identici).
if verify_ncc and float(score_f) < ncc_skip_above: if verify_ncc and float(score_f) < ncc_skip_above:
ncc = self._verify_ncc(gray0, cx_f, cy_f, ang_f, var.scale) ncc = self._verify_ncc(
gray0, cx_f, cy_f, ang_f, var.scale,
view_idx=getattr(var, "view_idx", 0),
)
if ncc < verify_threshold: if ncc < verify_threshold:
continue continue
score_f = (float(score_f) + max(0.0, ncc)) * 0.5 score_f = (float(score_f) + max(0.0, ncc)) * 0.5