Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f00cf9b621 |
+29
-42
@@ -239,6 +239,8 @@ class LineShapeMatcher:
|
|||||||
self._train_mask = mask_full.copy()
|
self._train_mask = mask_full.copy()
|
||||||
|
|
||||||
self.variants.clear()
|
self.variants.clear()
|
||||||
|
# Invalida cache feature di refine: il template e cambiato.
|
||||||
|
self._refine_feat_cache = {}
|
||||||
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)))
|
||||||
@@ -293,42 +295,8 @@ class LineShapeMatcher:
|
|||||||
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),
|
||||||
))
|
))
|
||||||
self._dedup_variants()
|
|
||||||
return len(self.variants)
|
return len(self.variants)
|
||||||
|
|
||||||
def _dedup_variants(self) -> int:
|
|
||||||
"""Rimuove varianti con feature-set identico (post-quantizzazione).
|
|
||||||
|
|
||||||
Halcon-style: con angle range = (0, 360) e simmetrie del template,
|
|
||||||
molte rotazioni producono lo stesso set quantizzato di feature.
|
|
||||||
Es: quadrato a 0/90/180/270 deg → stesse features (modulo permutazione).
|
|
||||||
Hash su feature ordinate (livello 0, full-res) elimina i duplicati.
|
|
||||||
|
|
||||||
Vantaggio: meno varianti = meno chiamate kernel JIT al top-level
|
|
||||||
senza perdere copertura angolare effettiva. Per template asimmetrici
|
|
||||||
non rimuove nulla.
|
|
||||||
"""
|
|
||||||
seen: dict[bytes, int] = {}
|
|
||||||
kept: list[_Variant] = []
|
|
||||||
removed = 0
|
|
||||||
for var in self.variants:
|
|
||||||
lvl0 = var.levels[0]
|
|
||||||
order = np.lexsort((lvl0.bin, lvl0.dy, lvl0.dx))
|
|
||||||
key = (
|
|
||||||
lvl0.dx[order].tobytes()
|
|
||||||
+ b"|" + lvl0.dy[order].tobytes()
|
|
||||||
+ b"|" + lvl0.bin[order].tobytes()
|
|
||||||
+ b"|" + str(round(var.scale, 4)).encode()
|
|
||||||
)
|
|
||||||
h = key # diretto, senza hash crypto (collision ok solo se identici)
|
|
||||||
if h in seen:
|
|
||||||
removed += 1
|
|
||||||
continue
|
|
||||||
seen[h] = len(kept)
|
|
||||||
kept.append(var)
|
|
||||||
self.variants = kept
|
|
||||||
return removed
|
|
||||||
|
|
||||||
# --- Matching ------------------------------------------------------
|
# --- Matching ------------------------------------------------------
|
||||||
|
|
||||||
def _response_map(self, gray: np.ndarray) -> np.ndarray:
|
def _response_map(self, gray: np.ndarray) -> np.ndarray:
|
||||||
@@ -467,17 +435,36 @@ class LineShapeMatcher:
|
|||||||
H, W = spread0.shape
|
H, W = spread0.shape
|
||||||
margin = 3
|
margin = 3
|
||||||
|
|
||||||
|
# Cache template features per angolo (chiave: int(round(ang*20)) =
|
||||||
|
# bucket di 0.05°). Golden-search ricontratto puo richiedere lo
|
||||||
|
# stesso bucket piu volte; evita re-warp+gradient+extract (costoso).
|
||||||
|
# Cache a livello matcher per riusare tra chiamate find() su scene
|
||||||
|
# diverse: la rotazione del template non dipende dalla scena.
|
||||||
|
if not hasattr(self, '_refine_feat_cache'):
|
||||||
|
self._refine_feat_cache = {}
|
||||||
|
feat_cache = self._refine_feat_cache
|
||||||
|
cache_scale_key = round(scale * 1000)
|
||||||
|
|
||||||
def _score_at_angle(off: float) -> tuple[float, float, float]:
|
def _score_at_angle(off: float) -> tuple[float, float, float]:
|
||||||
"""Ritorna (score, best_cx, best_cy) per angolo = angle_deg + off."""
|
"""Ritorna (score, best_cx, best_cy) per angolo = angle_deg + off."""
|
||||||
ang = angle_deg + off
|
ang = angle_deg + off
|
||||||
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
ck = (round(ang * 20), cache_scale_key)
|
||||||
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
cached = feat_cache.get(ck)
|
||||||
flags=cv2.INTER_LINEAR,
|
if cached is not None:
|
||||||
borderMode=cv2.BORDER_REPLICATE)
|
fx, fy, fb = cached
|
||||||
mask_r = cv2.warpAffine(mask_p, M, (diag, diag),
|
else:
|
||||||
flags=cv2.INTER_NEAREST, borderValue=0)
|
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
||||||
mag, bins = self._gradient(gray_r)
|
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
||||||
fx, fy, fb = self._extract_features(mag, bins, mask_r)
|
flags=cv2.INTER_LINEAR,
|
||||||
|
borderMode=cv2.BORDER_REPLICATE)
|
||||||
|
mask_r = cv2.warpAffine(mask_p, M, (diag, diag),
|
||||||
|
flags=cv2.INTER_NEAREST, borderValue=0)
|
||||||
|
mag, bins = self._gradient(gray_r)
|
||||||
|
fx, fy, fb = self._extract_features(mag, bins, mask_r)
|
||||||
|
# LRU semplice: limita cache a ~256 angoli (8 angoli * 32 candidati)
|
||||||
|
if len(feat_cache) > 256:
|
||||||
|
feat_cache.pop(next(iter(feat_cache)))
|
||||||
|
feat_cache[ck] = (fx, fy, fb)
|
||||||
if len(fx) < 8:
|
if len(fx) < 8:
|
||||||
return (0.0, cx, cy)
|
return (0.0, cx, cy)
|
||||||
dx = (fx - center[0]).astype(np.int32)
|
dx = (fx - center[0]).astype(np.int32)
|
||||||
|
|||||||
Reference in New Issue
Block a user