From db2aa57ce56aea4bbdd9671675351207308026b2 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 17:49:29 +0200 Subject: [PATCH] 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. --- croppa/main.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index 4e100d2..6c4a3f3 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -1679,15 +1679,16 @@ class VideoEditor: template_offset = None if self.template_matching_enabled and self.tracking_template is not None: if self.current_display_frame is not None: - # Apply template matching to the ORIGINAL frame (before motion tracking offset) - # This ensures the template can find the object in its original position - result = self.track_template(self.current_display_frame) - if result: - 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) + # Use the already transformed frame (crop, zoom, rotation applied) + transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) + if transformed_frame is not None: + result = self.track_template(transformed_frame) + if result: + 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 transformed frame space + template_offset = (center_x, center_y) # Calculate offset from feature tracking if enabled feature_offset = None @@ -1781,12 +1782,13 @@ class VideoEditor: return None if self.current_display_frame is not None: - # Apply template matching to the ORIGINAL frame (before motion tracking offset) - # This ensures the template can find the object in its original position - result = self.track_template(self.current_display_frame) - if result: - center_x, center_y, confidence = result - return (center_x, center_y, confidence) + # Use the already transformed frame (crop, zoom, rotation applied) + transformed_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) + if transformed_frame is not None: + result = self.track_template(transformed_frame) + if result: + center_x, center_y, confidence = result + return (center_x, center_y, confidence) return None