Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5002515b41 | |||
| 8029a1e12b | |||
| d37833076e |
+29
-29
@@ -143,17 +143,6 @@ def _draw_matches(scene: np.ndarray, matches: list[Match],
|
||||
nell'anteprima modello.
|
||||
"""
|
||||
out = scene.copy()
|
||||
# Baricentro UCS in coord template (calcolato una volta dal matcher
|
||||
# se disponibile): mean delle feature di una variante a 0°. Questo e'
|
||||
# lo stesso baricentro mostrato nell'anteprima modello.
|
||||
bary_dx = bary_dy = 0.0
|
||||
if matcher is not None and matcher.variants:
|
||||
# Trova variante con angle_deg piu vicino a 0
|
||||
v0 = min(matcher.variants, key=lambda v: abs(v.angle_deg))
|
||||
if len(v0.levels[0].dx) > 0:
|
||||
bary_dx = float(np.mean(v0.levels[0].dx))
|
||||
bary_dy = float(np.mean(v0.levels[0].dy))
|
||||
|
||||
# Lunghezza assi UCS: stessa formula dell'anteprima modello
|
||||
# (0.15 * max lato template) scalata per m.scale → coerenza dimensionale.
|
||||
if matcher is not None and matcher.template_size != (0, 0):
|
||||
@@ -161,19 +150,21 @@ def _draw_matches(scene: np.ndarray, matches: list[Match],
|
||||
else:
|
||||
L_base = 30
|
||||
H_scene, W_scene = scene.shape[:2]
|
||||
|
||||
for i, m in enumerate(matches):
|
||||
# Proietta baricentro template alla pose del match.
|
||||
# cv2.getRotationMatrix2D con angle positivo applica:
|
||||
# new_x = cos*x + sin*y new_y = -sin*x + cos*y
|
||||
# Visivamente in image y-down e' rotazione anti-clockwise.
|
||||
# UCS posizionato esattamente sul CENTRO POSE del match (m.cx, m.cy):
|
||||
# equivale al centro template traslato alla scena, ruotato con
|
||||
# m.angle_deg. Coerente con UCS dell'anteprima modello che ora
|
||||
# e' anche sul centro ROI (vedi preview_edges).
|
||||
ax = np.deg2rad(m.angle_deg)
|
||||
ca, sa = np.cos(ax), np.sin(ax)
|
||||
bx_scene = m.cx + (bary_dx * ca + bary_dy * sa) * m.scale
|
||||
by_scene = m.cy + (-bary_dx * sa + bary_dy * ca) * m.scale
|
||||
cx, cy = int(round(bx_scene)), int(round(by_scene))
|
||||
cx, cy = int(round(m.cx)), int(round(m.cy))
|
||||
# Overlay edge del modello orientato (richiesta utente):
|
||||
# warpa template alla pose, applica hysteresis identica al matcher,
|
||||
# disegna pixel edge come overlay verde tenue.
|
||||
# disegna pixel edge come overlay verde tenue. Maschera col
|
||||
# _train_mask warpato + erode per rimuovere edge sui BORDI del
|
||||
# rettangolo template (transizione bordo nero → scena = falso edge
|
||||
# che appariva come "ROI" attorno a ogni match).
|
||||
if template_gray is not None and matcher is not None:
|
||||
t = template_gray
|
||||
th, tw = t.shape
|
||||
@@ -184,11 +175,23 @@ def _draw_matches(scene: np.ndarray, matches: list[Match],
|
||||
warped_gray = cv2.warpAffine(
|
||||
t, M, (W_scene, H_scene),
|
||||
flags=cv2.INTER_LINEAR, borderValue=0)
|
||||
# Maschera: train_mask se disponibile, altrimenti rettangolo pieno
|
||||
mask_src = (matcher._train_mask if matcher._train_mask is not None
|
||||
else np.full((th, tw), 255, dtype=np.uint8))
|
||||
warped_mask = cv2.warpAffine(
|
||||
mask_src, M, (W_scene, H_scene),
|
||||
flags=cv2.INTER_NEAREST, borderValue=0)
|
||||
# Erode di spread_radius per scartare la fascia di transizione
|
||||
# bordo che produce gradient spurio
|
||||
er_k = max(3, 2 * matcher.spread_radius + 1)
|
||||
kernel_er = np.ones((er_k, er_k), np.uint8)
|
||||
warped_mask = cv2.erode(warped_mask, kernel_er)
|
||||
mag, _ = matcher._gradient(warped_gray)
|
||||
if matcher.weak_grad < matcher.strong_grad:
|
||||
edge_mask = matcher._hysteresis_mask(mag)
|
||||
else:
|
||||
edge_mask = mag >= matcher.strong_grad
|
||||
edge_mask = edge_mask & (warped_mask > 0)
|
||||
if edge_mask.any():
|
||||
edge_overlay = np.zeros_like(out)
|
||||
edge_overlay[edge_mask] = (0, 220, 0) # verde brillante
|
||||
@@ -763,26 +766,23 @@ def preview_edges(p: EdgePreviewParams):
|
||||
b = int(fb[i])
|
||||
col = bin_colors[b % len(bin_colors)]
|
||||
cv2.circle(out, (int(fx[i]), int(fy[i])), 2, col, -1, cv2.LINE_AA)
|
||||
# UCS sul baricentro feature (richiesta utente): assi X rosso, Y verde
|
||||
bary_cx = bary_cy = None
|
||||
if len(fx) > 0:
|
||||
bary_cx = float(np.mean(fx))
|
||||
bary_cy = float(np.mean(fy))
|
||||
bx, by = int(round(bary_cx)), int(round(bary_cy))
|
||||
axis_len = max(20, int(0.15 * max(out.shape[:2])))
|
||||
# X axis (rosso, verso destra)
|
||||
# UCS sul CENTRO ROI (coerente con _draw_matches che usa centro pose).
|
||||
# In questo modo l'UCS visualizzato nel modello = UCS del match (modulo
|
||||
# rotazione/traslazione data dalla pose del pezzo trovato).
|
||||
rh, rw = roi_img.shape[:2]
|
||||
bx, by = (rw - 1) // 2, (rh - 1) // 2
|
||||
axis_len = max(20, int(0.15 * max(rw, rh)))
|
||||
cv2.arrowedLine(out, (bx, by), (bx + axis_len, by),
|
||||
(0, 0, 255), 2, cv2.LINE_AA, tipLength=0.2)
|
||||
cv2.putText(out, "X", (bx + axis_len + 4, by + 5),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
|
||||
# Y axis (verde, verso il basso = convenzione image y-down)
|
||||
cv2.arrowedLine(out, (bx, by), (bx, by + axis_len),
|
||||
(0, 255, 0), 2, cv2.LINE_AA, tipLength=0.2)
|
||||
cv2.putText(out, "Y", (bx + 4, by + axis_len + 12),
|
||||
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
|
||||
# Origine: cerchio bianco con bordo nero
|
||||
cv2.circle(out, (bx, by), 4, (0, 0, 0), -1, cv2.LINE_AA)
|
||||
cv2.circle(out, (bx, by), 3, (255, 255, 255), -1, cv2.LINE_AA)
|
||||
bary_cx, bary_cy = float(bx), float(by)
|
||||
img_id = _store_image(out)
|
||||
n_edge_strong = int((mag >= m.strong_grad).sum())
|
||||
n_edge_total = int(edge_mask.sum() / 255)
|
||||
|
||||
Reference in New Issue
Block a user