Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6db2086ead |
+2
-5
@@ -220,11 +220,8 @@ def auto_tune(template_bgr: np.ndarray, mask: np.ndarray | None = None) -> dict:
|
|||||||
else:
|
else:
|
||||||
min_score = 0.45
|
min_score = 0.45
|
||||||
|
|
||||||
# angle step adattivo (Halcon-style): atan(2/max_side) deg, clampato.
|
# angle step: 5° default; se simmetria, mantengo step ma range ridotto
|
||||||
# Template grande → step fine (rotazione minima visibile su perimetro).
|
angle_step = 5.0
|
||||||
# Template piccolo → step grosso (over-sampling = sprecato).
|
|
||||||
max_side = max(h, w)
|
|
||||||
angle_step = float(np.clip(np.degrees(np.arctan2(2.0, max_side)), 1.0, 8.0))
|
|
||||||
|
|
||||||
result = {
|
result = {
|
||||||
"backend": "line",
|
"backend": "line",
|
||||||
|
|||||||
+69
-31
@@ -197,31 +197,12 @@ class LineShapeMatcher:
|
|||||||
n = int(np.floor((s1 - s0) / self.scale_step)) + 1
|
n = int(np.floor((s1 - s0) / self.scale_step)) + 1
|
||||||
return [float(s0 + i * self.scale_step) for i in range(n)]
|
return [float(s0 + i * self.scale_step) for i in range(n)]
|
||||||
|
|
||||||
def _auto_angle_step(self) -> float:
|
|
||||||
"""Step angolare derivato da dimensione template (Halcon-style).
|
|
||||||
|
|
||||||
Formula: step ≈ atan(2 / max_side) gradi. Garantisce che la
|
|
||||||
rotazione minima produca uno spostamento di ≥2 px sul perimetro
|
|
||||||
del template (sotto sample il matching coarse perde candidati).
|
|
||||||
Clampato in [0.5°, 10°].
|
|
||||||
"""
|
|
||||||
max_side = max(self.template_size) if self.template_size != (0, 0) else 64
|
|
||||||
step = math.degrees(math.atan2(2.0, float(max_side)))
|
|
||||||
return float(np.clip(step, 0.5, 10.0))
|
|
||||||
|
|
||||||
def _effective_angle_step(self) -> float:
|
|
||||||
"""Risolve angle_step_deg gestendo modalità auto (<=0)."""
|
|
||||||
if self.angle_step_deg <= 0:
|
|
||||||
return self._auto_angle_step()
|
|
||||||
return self.angle_step_deg
|
|
||||||
|
|
||||||
def _angle_list(self) -> list[float]:
|
def _angle_list(self) -> list[float]:
|
||||||
a0, a1 = self.angle_range_deg
|
a0, a1 = self.angle_range_deg
|
||||||
step = self._effective_angle_step()
|
if self.angle_step_deg <= 0 or a0 >= a1:
|
||||||
if step <= 0 or a0 >= a1:
|
|
||||||
return [float(a0)]
|
return [float(a0)]
|
||||||
n = int(np.floor((a1 - a0) / step))
|
n = int(np.floor((a1 - a0) / self.angle_step_deg))
|
||||||
return [float(a0 + i * step) for i in range(n)]
|
return [float(a0 + i * self.angle_step_deg) for i in range(n)]
|
||||||
|
|
||||||
# --- Training ------------------------------------------------------
|
# --- Training ------------------------------------------------------
|
||||||
|
|
||||||
@@ -434,7 +415,7 @@ class LineShapeMatcher:
|
|||||||
if original_score is not None and original_score >= 0.99:
|
if original_score is not None and original_score >= 0.99:
|
||||||
return (angle_deg, original_score, cx, cy)
|
return (angle_deg, original_score, cx, cy)
|
||||||
if search_radius is None:
|
if search_radius is None:
|
||||||
search_radius = self._effective_angle_step() / 2.0
|
search_radius = self.angle_step_deg / 2.0
|
||||||
|
|
||||||
h, w = template_gray.shape
|
h, w = template_gray.shape
|
||||||
sw = max(16, int(round(w * scale)))
|
sw = max(16, int(round(w * scale)))
|
||||||
@@ -593,6 +574,8 @@ class LineShapeMatcher:
|
|||||||
verify_threshold: float = 0.4,
|
verify_threshold: float = 0.4,
|
||||||
coarse_angle_factor: int = 2,
|
coarse_angle_factor: int = 2,
|
||||||
scale_penalty: float = 0.0,
|
scale_penalty: float = 0.0,
|
||||||
|
pyramid_propagate: bool = True,
|
||||||
|
propagate_topk: int = 8,
|
||||||
) -> list[Match]:
|
) -> list[Match]:
|
||||||
"""
|
"""
|
||||||
scale_penalty: se > 0, riduce lo score per match a scala diversa da 1.0:
|
scale_penalty: se > 0, riduce lo score per match a scala diversa da 1.0:
|
||||||
@@ -664,7 +647,12 @@ class LineShapeMatcher:
|
|||||||
end = min(n, i + half + 1)
|
end = min(n, i + half + 1)
|
||||||
neighbor_map[vi_c] = vi_sorted[start:end]
|
neighbor_map[vi_c] = vi_sorted[start:end]
|
||||||
|
|
||||||
# Pruning varianti via top-level (parallelizzato) - solo coarse
|
# Pruning varianti via top-level (parallelizzato).
|
||||||
|
# Quando pyramid_propagate=True ritorna anche le top-K posizioni
|
||||||
|
# del picco (in coord top-level) per restringere la fase full-res
|
||||||
|
# a piccoli crop attorno ai candidati (vs intera scena).
|
||||||
|
peaks_by_vi: dict[int, list[tuple[int, int, float]]] = {}
|
||||||
|
|
||||||
def _top_score(vi: int) -> tuple[int, float]:
|
def _top_score(vi: int) -> tuple[int, float]:
|
||||||
var = self.variants[vi]
|
var = self.variants[vi]
|
||||||
lvl = var.levels[min(top, len(var.levels) - 1)]
|
lvl = var.levels[min(top, len(var.levels) - 1)]
|
||||||
@@ -672,7 +660,23 @@ class LineShapeMatcher:
|
|||||||
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
spread_top, lvl.dx, lvl.dy, lvl.bin, bit_active_top,
|
||||||
bg_cache_top[var.scale],
|
bg_cache_top[var.scale],
|
||||||
)
|
)
|
||||||
return vi, float(score.max()) if score.size else -1.0
|
if score.size == 0:
|
||||||
|
return vi, -1.0
|
||||||
|
best = float(score.max())
|
||||||
|
if pyramid_propagate and best > 0:
|
||||||
|
# Top-K posizioni > top_thresh (max propagate_topk)
|
||||||
|
flat = score.ravel()
|
||||||
|
k = min(propagate_topk, flat.size)
|
||||||
|
idx = np.argpartition(-flat, k - 1)[:k]
|
||||||
|
peaks: list[tuple[int, int, float]] = []
|
||||||
|
for i in idx:
|
||||||
|
s = float(flat[i])
|
||||||
|
if s < top_thresh * 0.7:
|
||||||
|
continue
|
||||||
|
yt, xt = int(i // score.shape[1]), int(i % score.shape[1])
|
||||||
|
peaks.append((xt, yt, s))
|
||||||
|
peaks_by_vi[vi] = peaks
|
||||||
|
return vi, best
|
||||||
|
|
||||||
kept_coarse: list[tuple[int, float]] = []
|
kept_coarse: list[tuple[int, float]] = []
|
||||||
all_top_scores: list[tuple[int, float]] = []
|
all_top_scores: list[tuple[int, float]] = []
|
||||||
@@ -732,14 +736,48 @@ class LineShapeMatcher:
|
|||||||
for sc in unique_scales:
|
for sc in unique_scales:
|
||||||
bg_cache_full[sc] = _bg_for_scale(density_full, sc, 1)
|
bg_cache_full[sc] = _bg_for_scale(density_full, sc, 1)
|
||||||
|
|
||||||
|
# Margine in full-res attorno ad ogni peak top: copre incertezza
|
||||||
|
# downsampling (sf_top px) + spread_radius + slack per NMS.
|
||||||
|
propagate_margin = sf_top + self.spread_radius + max(8, nms_radius // 2)
|
||||||
|
H_full, W_full = spread0.shape
|
||||||
|
|
||||||
def _full_score(vi: int) -> tuple[int, np.ndarray]:
|
def _full_score(vi: int) -> tuple[int, np.ndarray]:
|
||||||
var = self.variants[vi]
|
var = self.variants[vi]
|
||||||
lvl0 = var.levels[0]
|
lvl0 = var.levels[0]
|
||||||
score = _jit_score_bitmap_rescored(
|
if not pyramid_propagate or vi not in peaks_by_vi or not peaks_by_vi[vi]:
|
||||||
spread0, lvl0.dx, lvl0.dy, lvl0.bin, bit_active_full,
|
# Path legacy: scansiona intera scena
|
||||||
bg_cache_full[var.scale],
|
return vi, _jit_score_bitmap_rescored(
|
||||||
)
|
spread0, lvl0.dx, lvl0.dy, lvl0.bin, bit_active_full,
|
||||||
return vi, score
|
bg_cache_full[var.scale],
|
||||||
|
)
|
||||||
|
# Path piramide propagata: valuta solo crop locali attorno
|
||||||
|
# alle posizioni dei picchi top-level (riproiettati a full-res).
|
||||||
|
score_full = np.zeros((H_full, W_full), dtype=np.float32)
|
||||||
|
mark = np.zeros((H_full, W_full), dtype=bool)
|
||||||
|
bg = bg_cache_full[var.scale]
|
||||||
|
for xt, yt, _s in peaks_by_vi[vi]:
|
||||||
|
cx0 = xt * sf_top
|
||||||
|
cy0 = yt * sf_top
|
||||||
|
x_lo = max(0, cx0 - propagate_margin)
|
||||||
|
x_hi = min(W_full, cx0 + propagate_margin + 1)
|
||||||
|
y_lo = max(0, cy0 - propagate_margin)
|
||||||
|
y_hi = min(H_full, cy0 + propagate_margin + 1)
|
||||||
|
if x_hi <= x_lo or y_hi <= y_lo:
|
||||||
|
continue
|
||||||
|
if mark[y_lo:y_hi, x_lo:x_hi].all():
|
||||||
|
continue
|
||||||
|
# Crop spread + bg, valuta kernel sul crop
|
||||||
|
spread_crop = np.ascontiguousarray(spread0[y_lo:y_hi, x_lo:x_hi])
|
||||||
|
bg_crop = np.ascontiguousarray(bg[y_lo:y_hi, x_lo:x_hi])
|
||||||
|
score_crop = _jit_score_bitmap_rescored(
|
||||||
|
spread_crop, lvl0.dx, lvl0.dy, lvl0.bin,
|
||||||
|
bit_active_full, bg_crop,
|
||||||
|
)
|
||||||
|
score_full[y_lo:y_hi, x_lo:x_hi] = np.maximum(
|
||||||
|
score_full[y_lo:y_hi, x_lo:x_hi], score_crop,
|
||||||
|
)
|
||||||
|
mark[y_lo:y_hi, x_lo:x_hi] = True
|
||||||
|
return vi, score_full
|
||||||
|
|
||||||
candidates_per_var: list[tuple[int, np.ndarray]] = []
|
candidates_per_var: list[tuple[int, np.ndarray]] = []
|
||||||
raw: list[tuple[float, int, int, int]] = []
|
raw: list[tuple[float, int, int, int]] = []
|
||||||
@@ -821,7 +859,7 @@ class LineShapeMatcher:
|
|||||||
ang_f, score_f, cx_f, cy_f = self._refine_angle(
|
ang_f, score_f, cx_f, cy_f = self._refine_angle(
|
||||||
spread0, bit_active_full, self.template_gray, cx_f, cy_f,
|
spread0, bit_active_full, self.template_gray, cx_f, cy_f,
|
||||||
var.angle_deg, var.scale, mask_full,
|
var.angle_deg, var.scale, mask_full,
|
||||||
search_radius=self._effective_angle_step() / 2.0,
|
search_radius=self.angle_step_deg / 2.0,
|
||||||
original_score=score,
|
original_score=score,
|
||||||
)
|
)
|
||||||
if verify_ncc:
|
if verify_ncc:
|
||||||
|
|||||||
Reference in New Issue
Block a user