Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 746d1668c6 |
+14
-30
@@ -239,8 +239,6 @@ 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)))
|
||||||
@@ -435,36 +433,17 @@ 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
|
||||||
ck = (round(ang * 20), cache_scale_key)
|
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
||||||
cached = feat_cache.get(ck)
|
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
||||||
if cached is not None:
|
flags=cv2.INTER_LINEAR,
|
||||||
fx, fy, fb = cached
|
borderMode=cv2.BORDER_REPLICATE)
|
||||||
else:
|
mask_r = cv2.warpAffine(mask_p, M, (diag, diag),
|
||||||
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
flags=cv2.INTER_NEAREST, borderValue=0)
|
||||||
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
mag, bins = self._gradient(gray_r)
|
||||||
flags=cv2.INTER_LINEAR,
|
fx, fy, fb = self._extract_features(mag, bins, mask_r)
|
||||||
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)
|
||||||
@@ -593,6 +572,7 @@ class LineShapeMatcher:
|
|||||||
subpixel: bool = True,
|
subpixel: bool = True,
|
||||||
verify_ncc: bool = True,
|
verify_ncc: bool = True,
|
||||||
verify_threshold: float = 0.4,
|
verify_threshold: float = 0.4,
|
||||||
|
ncc_skip_above: float = 0.85,
|
||||||
coarse_angle_factor: int = 2,
|
coarse_angle_factor: int = 2,
|
||||||
scale_penalty: float = 0.0,
|
scale_penalty: float = 0.0,
|
||||||
) -> list[Match]:
|
) -> list[Match]:
|
||||||
@@ -826,7 +806,11 @@ class LineShapeMatcher:
|
|||||||
search_radius=self.angle_step_deg / 2.0,
|
search_radius=self.angle_step_deg / 2.0,
|
||||||
original_score=score,
|
original_score=score,
|
||||||
)
|
)
|
||||||
if verify_ncc:
|
# NCC verify lazy (Halcon-style): skip se shape-score gia molto
|
||||||
|
# alto (probabilita falso positivo trascurabile). NCC e l'op
|
||||||
|
# piu costosa per match (warp + corr), quindi vale la pena
|
||||||
|
# saltarlo quando il gradiente shape e gia conclusivo.
|
||||||
|
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)
|
||||||
if ncc < verify_threshold:
|
if ncc < verify_threshold:
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user