diff --git a/ocv_template_matching.py b/ocv_template_matching.py index cebc3b8..014f96a 100644 --- a/ocv_template_matching.py +++ b/ocv_template_matching.py @@ -38,6 +38,9 @@ tracker.init(image, bb_crop) path = [] line_from = None +is_startup = True +offset_x = 0 +offset_y = 1 while True: # Read a new frame ok, image = video.read() @@ -64,7 +67,19 @@ while True: print(f"crop_scaled : {crop_scaled.shape}") print(f"template : {template.shape}") print(f"template_scaled : {template_scaled.shape}") - matcher_bbox_local = (max_loc[0]/IMG_SCALE_UP, max_loc[1]/IMG_SCALE_UP, template.shape[1], template.shape[0]) + + matcher_start_x = max_loc[0]/IMG_SCALE_UP + matcher_start_y = max_loc[0]/IMG_SCALE_UP + if is_startup: + is_startup = False + offset_x = matcher_start_x - bb_template[0] + offset_y = matcher_start_y - bb_template[1] + print(f"offset : {(offset_x, offset_y)}") + + matcher_start_x -= offset_x + matcher_start_y -= offset_y + print(f"matcher_start : {(matcher_start_x, matcher_start_y)}") + matcher_bbox_local = (round(matcher_start_x), round(matcher_start_y), template.shape[1], template.shape[0]) matcher_top_left_local, matcher_bottom_right_local = to_rect(bbox_round(matcher_bbox_local)) matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bb_crop))