Refactor template matching logic in VideoEditor to use original frame for tracking
This commit updates the template matching process in the VideoEditor to apply tracking directly on the original frame instead of a cropped region. This change ensures that the template can accurately locate the object in its original position, improving tracking reliability. The code has been simplified by removing the previous cropping logic, enhancing overall clarity and performance during video editing sessions.
This commit is contained in:
@@ -1679,35 +1679,15 @@ class VideoEditor:
|
|||||||
template_offset = None
|
template_offset = None
|
||||||
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 only the cropped region for much faster template matching
|
# Apply template matching to the ORIGINAL frame (before motion tracking offset)
|
||||||
if self.crop_rect:
|
# This ensures the template can find the object in its original position
|
||||||
crop_x, crop_y, crop_w, crop_h = self.crop_rect
|
result = self.track_template(self.current_display_frame)
|
||||||
# Extract only the cropped region from raw frame
|
if result:
|
||||||
cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w]
|
center_x, center_y, confidence = result
|
||||||
if cropped_frame is not None and cropped_frame.size > 0:
|
print(f"DEBUG: Template match found at ({center_x}, {center_y}) with confidence {confidence:.2f}")
|
||||||
# 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
|
# Template matching returns coordinates in raw frame space
|
||||||
# Add crop offset back
|
template_offset = (center_x, center_y)
|
||||||
raw_x = center_x + crop_x
|
|
||||||
raw_y = center_y + crop_y
|
|
||||||
|
|
||||||
template_offset = (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
|
|
||||||
template_offset = (center_x, center_y)
|
|
||||||
|
|
||||||
# Calculate offset from feature tracking if enabled
|
# Calculate offset from feature tracking if enabled
|
||||||
feature_offset = None
|
feature_offset = None
|
||||||
@@ -1801,29 +1781,12 @@ class VideoEditor:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
if self.current_display_frame is not None:
|
if self.current_display_frame is not None:
|
||||||
# Use only the cropped region for much faster template matching
|
# Apply template matching to the ORIGINAL frame (before motion tracking offset)
|
||||||
if self.crop_rect:
|
# This ensures the template can find the object in its original position
|
||||||
crop_x, crop_y, crop_w, crop_h = self.crop_rect
|
result = self.track_template(self.current_display_frame)
|
||||||
# Extract only the cropped region from raw frame
|
if result:
|
||||||
cropped_frame = self.current_display_frame[crop_y:crop_y+crop_h, crop_x:crop_x+crop_w]
|
center_x, center_y, confidence = result
|
||||||
if cropped_frame is not None and cropped_frame.size > 0:
|
return (center_x, center_y, confidence)
|
||||||
# Track template in cropped frame (much faster!)
|
|
||||||
result = self.track_template(cropped_frame)
|
|
||||||
if result:
|
|
||||||
center_x, center_y, confidence = result
|
|
||||||
# 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, confidence)
|
|
||||||
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
|
|
||||||
return (center_x, center_y, confidence)
|
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user