Refactor tracking point management in VideoEditor and MotionTracker to ensure accurate display coordinates

This commit modifies the handling of tracking points in the VideoEditor and MotionTracker classes by removing the storage of display coordinates. Instead, display coordinates are now calculated dynamically during rendering, ensuring accuracy regardless of crop or rotation states. The changes enhance the consistency of point transformations and improve logging for better debugging and verification of coordinate accuracy during mouse interactions.
This commit is contained in:
2025-09-16 21:34:17 +02:00
parent cd86cfc9f2
commit e162e4fe92
2 changed files with 22 additions and 19 deletions

View File

@@ -1562,13 +1562,8 @@ class VideoEditor:
crop_y <= orig_y < crop_y + crop_h) crop_y <= orig_y < crop_y + crop_h)
print(f"draw_tracking_points: point {i} is {'inside' if is_in_crop else 'outside'} crop area") print(f"draw_tracking_points: point {i} is {'inside' if is_in_crop else 'outside'} crop area")
# Get the display coordinates - either from stored value or transform now # Always transform point from original frame coordinates to display coordinates
if tracking_point.display: # This ensures the point is always drawn correctly regardless of current crop/rotation state
# Use stored display coordinates
display_point = tracking_point.display
print(f"draw_tracking_points: using stored display coordinates {display_point}")
else:
# Transform point from original frame coordinates to display coordinates
display_point = self.transform_point(tracking_point.original) display_point = self.transform_point(tracking_point.original)
print(f"draw_tracking_points: transformed to display coordinates {display_point}") print(f"draw_tracking_points: transformed to display coordinates {display_point}")
@@ -2220,17 +2215,26 @@ class VideoEditor:
if removed: if removed:
print(f"mouse_callback: removed tracking point at {original_point}") print(f"mouse_callback: removed tracking point at {original_point}")
else: else:
# Add a new tracking point with both original and display coordinates # Add a new tracking point - only store the original coordinates
# This is the key change - we store both coordinate systems # Display coordinates will be calculated fresh each time to ensure accuracy
self.motion_tracker.add_tracking_point( self.motion_tracker.add_tracking_point(
self.current_frame, self.current_frame,
original_point[0], original_point[0],
original_point[1], original_point[1]
display_coords=(display_x, display_y) # Store the display coordinates directly # No display coordinates - we'll calculate them fresh each time
) )
print(f"mouse_callback: added tracking point at {original_point} (display: {display_x}, {display_y})") print(f"mouse_callback: added tracking point at {original_point}")
# No need for verification - we're storing both coordinate systems directly # Verify the coordinates are correct by doing a round-trip transformation
verification_display = self.transform_point(original_point)
if verification_display:
expected_x = int(start_x + verification_display[0] * scale)
expected_y = int(start_y + verification_display[1] * scale)
print(f"mouse_callback: verification - expected canvas position: ({expected_x}, {expected_y}), actual: ({x}, {y})")
error_x = abs(expected_x - x)
error_y = abs(expected_y - y)
print(f"mouse_callback: verification - position error: ({error_x}, {error_y}) pixels")
# Save state when tracking points change # Save state when tracking points change
self.save_state() self.save_state()

View File

@@ -21,20 +21,19 @@ class MotionTracker:
self.base_crop_rect = None # Original crop rect when tracking started self.base_crop_rect = None # Original crop rect when tracking started
self.base_zoom_center = None # Original zoom center when tracking started self.base_zoom_center = None # Original zoom center when tracking started
def add_tracking_point(self, frame_number: int, x: float, y: float, display_coords: Optional[Tuple[float, float]] = None): def add_tracking_point(self, frame_number: int, x: float, y: float):
"""Add a tracking point at the specified frame and coordinates """Add a tracking point at the specified frame and coordinates
Args: Args:
frame_number: The frame number to add the point to frame_number: The frame number to add the point to
x: Original x coordinate x: Original x coordinate
y: Original y coordinate y: Original y coordinate
display_coords: Optional display coordinates after transformation
""" """
if frame_number not in self.tracking_points: if frame_number not in self.tracking_points:
self.tracking_points[frame_number] = [] self.tracking_points[frame_number] = []
# Store both original and display coordinates # Store only the original coordinates - display coordinates will be calculated fresh each time
point = TrackingPoint(original=(float(x), float(y)), display=display_coords) point = TrackingPoint(original=(float(x), float(y)))
print(f"Adding tracking point: {point}") print(f"Adding tracking point: {point}")
self.tracking_points[frame_number].append(point) self.tracking_points[frame_number].append(point)