From f4d8c80edec028dcc5e9ff6c54d7307af2ce514c Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 1 Jul 2024 21:18:33 +0200 Subject: [PATCH] draw path --- ocv_template_matching.py | 18 +++++++++++++++--- util.py | 7 +++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ocv_template_matching.py b/ocv_template_matching.py index e5145bc..cebc3b8 100644 --- a/ocv_template_matching.py +++ b/ocv_template_matching.py @@ -2,13 +2,13 @@ import cv2 import sys import imutils as im -from util import bbox_extend, bbox_round, image_crop, to_rect, bbox_add_position +from util import bbox_extend, bbox_round, image_crop, to_rect, bbox_add_position, bbox_center COLOR_TRACKER = (0, 255, 0) COLOR_TRACKER_EXT = (0, 255, 255) COLOR_MATCHER = (0, 0, 255) COLOR_TEMPLATE = (255, 0, 0) -IMG_SCALE_UP = 2 +IMG_SCALE_UP = 1 TEMPLATE_MATCH_OVERLAP = 0 video = cv2.VideoCapture("./data/IMG_1730.MP4") @@ -28,13 +28,16 @@ if not ok: bb_crop = bbox_extend(cv2.selectROI("Image", image, False), TEMPLATE_MATCH_OVERLAP) crop = image_crop(image.copy(), bb_crop) -bb_template = cv2.selectROI("Image", crop, False) +bb_template = cv2.selectROI("Image", crop, fromCenter=True, showCrosshair=True) template = image_crop(crop.copy(), bb_template) # Initialize tracker with first frame and bounding box tracker = cv2.TrackerKCF().create() tracker.init(image, bb_crop) +path = [] +line_from = None + while True: # Read a new frame ok, image = video.read() @@ -77,6 +80,15 @@ while True: cv2.rectangle(image_anno, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 1) matcher_crop = image_crop(image, matcher_bbox) + # Draw path + line_to = bbox_center(matcher_bbox) + if line_from is not None: + path.append({'from': line_from, 'to': line_to}) + line_from = line_to + + for p in path: + cv2.line(image_anno, p['from'], p['to'], (0, 0, 255), 2) + cv2.imshow("Crop", crop) cv2.imshow("Image", image_anno) cv2.imshow("Matcher res", res) diff --git a/util.py b/util.py index 9542319..dcbca45 100644 --- a/util.py +++ b/util.py @@ -28,6 +28,13 @@ def bbox_round(src): return x, y, w, h +def bbox_center(bbox): + x = round(bbox[0] + bbox[2]/2) + y = round(bbox[1] + bbox[3]/2) + + return x, y + + def bbox_extend(bbox: cv2.typing.Rect, factor: float = 0): we2 = bbox[2]*factor/2 he2 = bbox[3]*factor/2