Enable multi-scale template matching in VideoEditor with toggle functionality

This commit introduces a toggle for multi-scale template matching, allowing users to switch between multi-scale and single-scale approaches for improved tracking accuracy. The default setting is now multi-scale, enhancing the template matching process by evaluating templates at various sizes. Additionally, debug messages and user feedback have been updated to reflect this new feature.
This commit is contained in:
2025-09-26 14:56:06 +02:00
parent c52d9b9399
commit 2246ef9f45

View File

@@ -881,7 +881,8 @@ class VideoEditor:
self.template_matching_enabled = False
self.tracking_template = None
self.template_region = None
self.template_match_history = [] # Store recent match confidences for adaptive thresholding # (x, y, w, h) in rotated frame coordinates
self.template_match_history = [] # Store recent match confidences for adaptive thresholding
self.multi_scale_template_matching = True # Enable multi-scale by default # (x, y, w, h) in rotated frame coordinates
self.template_selection_start = None
self.template_selection_rect = None
@@ -2229,7 +2230,8 @@ class VideoEditor:
# Apply image preprocessing for better template matching
gray_frame, gray_template = self._improve_template_matching(frame, self.tracking_template)
# Multi-scale template matching for better tracking
# Multi-scale template matching for better tracking (if enabled)
if self.multi_scale_template_matching:
scales = [0.8, 0.9, 1.0, 1.1, 1.2] # Different scales to try
best_match = None
best_confidence = 0.0
@@ -2256,6 +2258,20 @@ class VideoEditor:
center_x = max_loc[0] + new_w // 2
center_y = max_loc[1] + new_h // 2
best_match = (center_x, center_y, max_val)
else:
# Single-scale template matching (faster)
result = cv2.matchTemplate(gray_frame, gray_template, cv2.TM_CCOEFF_NORMED)
_, max_val, _, max_loc = cv2.minMaxLoc(result)
if max_val > 0.6: # Higher threshold for single-scale
template_h, template_w = gray_template.shape
center_x = max_loc[0] + template_w // 2
center_y = max_loc[1] + template_h // 2
best_match = (center_x, center_y, max_val)
best_confidence = max_val
else:
best_match = None
best_confidence = 0.0
# Adaptive thresholding based on recent match history
if len(self.template_match_history) > 0:
@@ -4127,6 +4143,7 @@ class VideoEditor:
print(" H: Switch detector (SIFT/ORB)")
print(" o: Toggle optical flow tracking")
print(" m: Toggle template matching tracking")
print(" M: Toggle multi-scale template matching")
print(" Shift+Right-click+drag: Extract features from selected region")
print(" Ctrl+Right-click+drag: Delete features from selected region")
print(" Ctrl+Left-click+drag: Set template region for tracking")
@@ -4471,6 +4488,11 @@ class VideoEditor:
print(f"DEBUG: Template matching toggled to {self.template_matching_enabled}")
self.show_feedback_message(f"Template matching {'ON' if self.template_matching_enabled else 'OFF'}")
self.save_state()
elif key == ord("M"): # Shift+M - Toggle multi-scale template matching
self.multi_scale_template_matching = not self.multi_scale_template_matching
print(f"DEBUG: Multi-scale template matching toggled to {self.multi_scale_template_matching}")
self.show_feedback_message(f"Multi-scale template matching {'ON' if self.multi_scale_template_matching else 'OFF'}")
self.save_state()
elif key == ord("t"):
# Marker looping only for videos
if not self.is_image_mode: