Optimize template matching in VideoEditor by utilizing cropped regions for faster processing. This update modifies the tracking logic to first check for a defined crop rectangle, allowing for quicker template matching on smaller frames. If no crop is set, the full frame is used, maintaining the previous functionality. Debug messages remain to assist in tracking accuracy and confidence levels.
This commit is contained in:
@@ -1640,34 +1640,47 @@ 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:
|
||||||
# Use the raw display frame to avoid recursion
|
# Use only the cropped region for much faster template matching
|
||||||
raw_frame = self.current_display_frame.copy()
|
if self.crop_rect:
|
||||||
if raw_frame is not None:
|
crop_x, crop_y, crop_w, crop_h = self.crop_rect
|
||||||
# Track template in raw frame
|
# Extract only the cropped region
|
||||||
result = self.track_template(raw_frame)
|
cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w]
|
||||||
if result:
|
if cropped_frame is not None and cropped_frame.size > 0:
|
||||||
center_x, center_y, confidence = result
|
# Track template in cropped frame (much faster!)
|
||||||
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
|
result = self.track_template(cropped_frame)
|
||||||
|
if result:
|
||||||
# Map from raw frame coordinates to rotated frame coordinates
|
center_x, center_y, confidence = result
|
||||||
# We need to account for rotation and crop transformations
|
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
|
||||||
if self.rotation_angle == 90:
|
|
||||||
# 90° clockwise rotation
|
# Map from cropped frame coordinates to rotated frame coordinates
|
||||||
rot_x = self.frame_height - center_y
|
# Add crop offset back
|
||||||
rot_y = center_x
|
rot_x = center_x + crop_x
|
||||||
elif self.rotation_angle == 180:
|
rot_y = center_y + crop_y
|
||||||
# 180° rotation
|
|
||||||
rot_x = self.frame_width - center_x
|
return (rot_x, rot_y)
|
||||||
rot_y = self.frame_height - center_y
|
else:
|
||||||
elif self.rotation_angle == 270:
|
# No crop - use full frame
|
||||||
# 270° clockwise rotation
|
raw_frame = self.current_display_frame.copy()
|
||||||
rot_x = center_y
|
if raw_frame is not None:
|
||||||
rot_y = self.frame_width - center_x
|
result = self.track_template(raw_frame)
|
||||||
else:
|
if result:
|
||||||
# No rotation
|
center_x, center_y, confidence = result
|
||||||
rot_x, rot_y = center_x, center_y
|
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
|
||||||
|
|
||||||
return (rot_x, rot_y)
|
# Map from raw frame coordinates to rotated frame coordinates
|
||||||
|
if self.rotation_angle == 90:
|
||||||
|
rot_x = self.frame_height - center_y
|
||||||
|
rot_y = center_x
|
||||||
|
elif self.rotation_angle == 180:
|
||||||
|
rot_x = self.frame_width - center_x
|
||||||
|
rot_y = self.frame_height - center_y
|
||||||
|
elif self.rotation_angle == 270:
|
||||||
|
rot_x = center_y
|
||||||
|
rot_y = self.frame_width - center_x
|
||||||
|
else:
|
||||||
|
rot_x, rot_y = center_x, center_y
|
||||||
|
|
||||||
|
return (rot_x, rot_y)
|
||||||
|
|
||||||
# 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:
|
||||||
|
Reference in New Issue
Block a user