draw path

This commit is contained in:
2024-07-01 21:18:33 +02:00
parent dcb4215334
commit f4d8c80ede
2 changed files with 22 additions and 3 deletions
+15 -3
View File
@@ -2,13 +2,13 @@ import cv2
import sys import sys
import imutils as im 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 = (0, 255, 0)
COLOR_TRACKER_EXT = (0, 255, 255) COLOR_TRACKER_EXT = (0, 255, 255)
COLOR_MATCHER = (0, 0, 255) COLOR_MATCHER = (0, 0, 255)
COLOR_TEMPLATE = (255, 0, 0) COLOR_TEMPLATE = (255, 0, 0)
IMG_SCALE_UP = 2 IMG_SCALE_UP = 1
TEMPLATE_MATCH_OVERLAP = 0 TEMPLATE_MATCH_OVERLAP = 0
video = cv2.VideoCapture("./data/IMG_1730.MP4") 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) bb_crop = bbox_extend(cv2.selectROI("Image", image, False), TEMPLATE_MATCH_OVERLAP)
crop = image_crop(image.copy(), bb_crop) 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) template = image_crop(crop.copy(), bb_template)
# Initialize tracker with first frame and bounding box # Initialize tracker with first frame and bounding box
tracker = cv2.TrackerKCF().create() tracker = cv2.TrackerKCF().create()
tracker.init(image, bb_crop) tracker.init(image, bb_crop)
path = []
line_from = None
while True: while True:
# Read a new frame # Read a new frame
ok, image = video.read() ok, image = video.read()
@@ -77,6 +80,15 @@ while True:
cv2.rectangle(image_anno, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 1) cv2.rectangle(image_anno, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 1)
matcher_crop = image_crop(image, matcher_bbox) 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("Crop", crop)
cv2.imshow("Image", image_anno) cv2.imshow("Image", image_anno)
cv2.imshow("Matcher res", res) cv2.imshow("Matcher res", res)
+7
View File
@@ -28,6 +28,13 @@ def bbox_round(src):
return x, y, w, h 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): def bbox_extend(bbox: cv2.typing.Rect, factor: float = 0):
we2 = bbox[2]*factor/2 we2 = bbox[2]*factor/2
he2 = bbox[3]*factor/2 he2 = bbox[3]*factor/2