Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f00cf9b621 |
+2
-14
@@ -246,22 +246,10 @@ def score_bitmap_rescored(
|
|||||||
return np.maximum(0.0, out).astype(np.float32)
|
return np.maximum(0.0, out).astype(np.float32)
|
||||||
|
|
||||||
|
|
||||||
_HAS_NP_BITCOUNT = hasattr(np, "bitwise_count")
|
|
||||||
|
|
||||||
|
|
||||||
def popcount_density(spread: np.ndarray) -> np.ndarray:
|
def popcount_density(spread: np.ndarray) -> np.ndarray:
|
||||||
"""Conta bit set per pixel.
|
|
||||||
|
|
||||||
Order:
|
|
||||||
1) Numba JIT parallel (preferito: piu veloce su 1080p, 0.5ms vs 1.6ms)
|
|
||||||
2) numpy.bitwise_count (NumPy 2.0+, SIMD ma single-thread)
|
|
||||||
3) Fallback numpy bit-shift puro
|
|
||||||
"""
|
|
||||||
spread_c = np.ascontiguousarray(spread, dtype=np.uint8)
|
|
||||||
if HAS_NUMBA:
|
if HAS_NUMBA:
|
||||||
return _jit_popcount_density(spread_c)
|
return _jit_popcount_density(np.ascontiguousarray(spread, dtype=np.uint8))
|
||||||
if _HAS_NP_BITCOUNT:
|
# Fallback
|
||||||
return np.bitwise_count(spread_c).astype(np.float32, copy=False)
|
|
||||||
H, W = spread.shape
|
H, W = spread.shape
|
||||||
out = np.zeros((H, W), dtype=np.float32)
|
out = np.zeros((H, W), dtype=np.float32)
|
||||||
for b in range(8):
|
for b in range(8):
|
||||||
|
|||||||
@@ -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)))
|
||||||
@@ -433,9 +435,24 @@ 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)
|
||||||
|
cached = feat_cache.get(ck)
|
||||||
|
if cached is not None:
|
||||||
|
fx, fy, fb = cached
|
||||||
|
else:
|
||||||
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
M = cv2.getRotationMatrix2D(center, ang, 1.0)
|
||||||
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
gray_r = cv2.warpAffine(gray_p, M, (diag, diag),
|
||||||
flags=cv2.INTER_LINEAR,
|
flags=cv2.INTER_LINEAR,
|
||||||
@@ -444,6 +461,10 @@ class LineShapeMatcher:
|
|||||||
flags=cv2.INTER_NEAREST, borderValue=0)
|
flags=cv2.INTER_NEAREST, borderValue=0)
|
||||||
mag, bins = self._gradient(gray_r)
|
mag, bins = self._gradient(gray_r)
|
||||||
fx, fy, fb = self._extract_features(mag, bins, mask_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