Implement template matching position retrieval in VideoEditor

This commit introduces a new method to get the template matching position and confidence for a specific frame. It optimizes the tracking process by utilizing cropped regions for faster template matching while maintaining functionality for full frame matching when no crop is set. Additionally, it updates the rendering logic to visualize the template matching point on the canvas, enhancing user feedback with confidence levels displayed. This update improves the overall accuracy and efficiency of the template matching feature.
This commit is contained in:
2025-09-26 14:51:04 +02:00
parent a2dc4a2186
commit 10284dad81

View File

@@ -1760,6 +1760,38 @@ class VideoEditor:
return (x1 + t * (x2 - x1), y1 + t * (y2 - y1)) return (x1 + t * (x2 - x1), y1 + t * (y2 - y1))
return None return None
def _get_template_matching_position(self, frame_number):
"""Get template matching position and confidence for a frame"""
if not self.template_matching_enabled or self.tracking_template is None:
return None
if self.current_display_frame is not None:
# Use only the cropped region for much faster template matching
if self.crop_rect:
crop_x, crop_y, crop_w, crop_h = self.crop_rect
# Extract only the cropped region from raw frame
cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w]
if cropped_frame is not None and cropped_frame.size > 0:
# Track template in cropped frame (much faster!)
result = self.track_template(cropped_frame)
if result:
center_x, center_y, confidence = result
# Map from cropped frame coordinates to raw frame coordinates
# Add crop offset back
raw_x = center_x + crop_x
raw_y = center_y + crop_y
return (raw_x, raw_y, confidence)
else:
# No crop - use full frame
raw_frame = self.current_display_frame.copy()
if raw_frame is not None:
result = self.track_template(raw_frame)
if result:
center_x, center_y, confidence = result
return (center_x, center_y, confidence)
return None
def _get_display_params(self): def _get_display_params(self):
"""Unified display transform parameters for current frame in rotated space.""" """Unified display transform parameters for current frame in rotated space."""
eff_x, eff_y, eff_w, eff_h = self._get_effective_crop_rect_for_frame(getattr(self, 'current_frame', 0)) eff_x, eff_y, eff_w, eff_h = self._get_effective_crop_rect_for_frame(getattr(self, 'current_frame', 0))
@@ -2957,6 +2989,23 @@ class VideoEditor:
cv2.circle(canvas, (sx, sy), 4, (0, 255, 0), -1) # Green circles for features cv2.circle(canvas, (sx, sy), 4, (0, 255, 0), -1) # Green circles for features
cv2.circle(canvas, (sx, sy), 4, (255, 255, 255), 1) cv2.circle(canvas, (sx, sy), 4, (255, 255, 255), 1)
# Draw template matching point (blue circle with confidence)
if (not self.is_image_mode and
self.template_matching_enabled and
self.tracking_template is not None):
# Get template matching position for current frame
template_pos = self._get_template_matching_position(self.current_frame)
if template_pos:
tx, ty, confidence = template_pos
# Map to screen coordinates
sx, sy = self._map_rotated_to_screen(tx, ty)
# Draw blue circle for template matching
cv2.circle(canvas, (sx, sy), 8, (255, 0, 255), -1) # Magenta circle for template
cv2.circle(canvas, (sx, sy), 8, (255, 255, 255), 2)
# Draw confidence text
conf_text = f"{confidence:.2f}"
cv2.putText(canvas, conf_text, (sx + 10, sy - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
# Draw selection rectangles for feature extraction/deletion # Draw selection rectangles for feature extraction/deletion
if self.selective_feature_extraction_rect: if self.selective_feature_extraction_rect:
x, y, w, h = self.selective_feature_extraction_rect x, y, w, h = self.selective_feature_extraction_rect