From 65b80034cb3420f5e3329aeed106bc64db2cdeee Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 14:35:51 +0200 Subject: [PATCH] Enhance template matching in VideoEditor by utilizing cropped regions for improved speed and accuracy. This update modifies the tracking logic to prioritize cropped areas for faster processing, while maintaining functionality for full frame matching when no crop is set. Debug messages have been refined to provide clearer insights into tracking results and confidence levels. --- croppa/main.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index 7cbdfea..2c73c25 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -1640,17 +1640,35 @@ class VideoEditor: # First try template matching if enabled (much better than optical flow) if self.template_matching_enabled and self.tracking_template is not None: if self.current_display_frame is not None: - # Use the raw frame for template matching (template_region is in raw frame coordinates) - 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 - print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") - - # Template matching returns coordinates in raw frame space - # No additional transformation needed since template_region is in raw frame coordinates - return (center_x, center_y) + # 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 + print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") + + # 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) + 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 + print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") + + # Template matching returns coordinates in raw frame space + return (center_x, center_y) # Fall back to feature tracking if enabled - but use smooth interpolation instead of averaging if self.feature_tracker.tracking_enabled: