Refactor template matching logic in VideoEditor to utilize transformed frames

This commit updates the template matching process in the VideoEditor to apply tracking on the already transformed frame, which includes crop, zoom, and rotation adjustments. This change enhances tracking accuracy by ensuring that the template can locate the object in its modified position, while also improving code clarity and performance during video editing sessions.
This commit is contained in:
2025-09-26 17:49:29 +02:00
parent 92c2e62166
commit db2aa57ce5

View File

@@ -1679,15 +1679,16 @@ class VideoEditor:
template_offset = None template_offset = None
if self.template_matching_enabled and self.tracking_template is not None: if self.template_matching_enabled and self.tracking_template is not None:
if self.current_display_frame is not None: if self.current_display_frame is not None:
# Apply template matching to the ORIGINAL frame (before motion tracking offset) # Use the already transformed frame (crop, zoom, rotation applied)
# This ensures the template can find the object in its original position transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame)
result = self.track_template(self.current_display_frame) if transformed_frame is not None:
if result: result = self.track_template(transformed_frame)
center_x, center_y, confidence = result if result:
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") center_x, center_y, confidence = result
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
# Template matching returns coordinates in raw frame space
template_offset = (center_x, center_y) # Template matching returns coordinates in transformed frame space
template_offset = (center_x, center_y)
# Calculate offset from feature tracking if enabled # Calculate offset from feature tracking if enabled
feature_offset = None feature_offset = None
@@ -1781,12 +1782,13 @@ class VideoEditor:
return None return None
if self.current_display_frame is not None: if self.current_display_frame is not None:
# Apply template matching to the ORIGINAL frame (before motion tracking offset) # Use the already transformed frame (crop, zoom, rotation applied)
# This ensures the template can find the object in its original position transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame)
result = self.track_template(self.current_display_frame) if transformed_frame is not None:
if result: result = self.track_template(transformed_frame)
center_x, center_y, confidence = result if result:
return (center_x, center_y, confidence) center_x, center_y, confidence = result
return (center_x, center_y, confidence)
return None return None