Refactor template tracking in VideoEditor to use raw display frame

This commit modifies the template tracking logic in the VideoEditor to utilize the raw display frame instead of applying transformations, improving accuracy in tracking. It updates the coordinate mapping to account for various rotation angles, ensuring correct positioning of the tracked template. This enhancement streamlines the tracking process and enhances overall functionality.
This commit is contained in:
2025-09-26 14:29:50 +02:00
parent 8c1efb1b05
commit c08d5c5999

View File

@@ -1641,27 +1641,34 @@ class VideoEditor:
# First try template matching if enabled (much better than optical flow) # 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.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 transformations to get the display frame # Use the raw display frame to avoid recursion
display_frame = self.apply_crop_zoom_and_rotation(self.current_display_frame) raw_frame = self.current_display_frame.copy()
if display_frame is not None: if raw_frame is not None:
# Track template in display frame # Track template in raw frame
result = self.track_template(display_frame) result = self.track_template(raw_frame)
if result: if result:
center_x, center_y, confidence = result center_x, center_y, confidence = result
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}") print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
# Map from display frame coordinates to rotated frame coordinates # Map from raw frame coordinates to rotated frame coordinates
frame_height, frame_width = display_frame.shape[:2] # We need to account for rotation and crop transformations
available_height = self.window_height - (0 if self.is_image_mode else self.TIMELINE_HEIGHT) if self.rotation_angle == 90:
start_y = (available_height - frame_height) // 2 # 90° clockwise rotation
start_x = (self.window_width - frame_width) // 2 rot_x = self.frame_height - center_y
rot_y = center_x
elif self.rotation_angle == 180:
# 180° rotation
rot_x = self.frame_width - center_x
rot_y = self.frame_height - center_y
elif self.rotation_angle == 270:
# 270° clockwise rotation
rot_x = center_y
rot_y = self.frame_width - center_x
else:
# No rotation
rot_x, rot_y = center_x, center_y
screen_x = center_x + start_x return (rot_x, rot_y)
screen_y = center_y + start_y
# Map from screen coordinates to rotated frame coordinates
rx, ry = self._map_screen_to_rotated(screen_x, screen_y)
return (rx, ry)
# Fall back to feature tracking if enabled - but use smooth interpolation instead of averaging # Fall back to feature tracking if enabled - but use smooth interpolation instead of averaging
if self.feature_tracker.tracking_enabled: if self.feature_tracker.tracking_enabled: