- defined COLORs added legend

- ocv_template_mnatching uses video
This commit is contained in:
2024-07-01 12:28:47 +02:00
parent 50bb0433f1
commit 0189e02bbc
3 changed files with 93 additions and 46 deletions
+29 -21
View File
@@ -6,16 +6,16 @@ from template_matcher import template_match_rotation
from util import bbox_extend, bbox_round, process_image_bbox, image_crop, bbox_add_position, to_rect
IMG_SCALE_UP = 1
TEMPLATE_MATCH_OVERLAP = 0.5
TEMPLATE_MATCH_OVERLAP = 1
def match(source, template, angle, center):
# coarse
angle_c, loc = template_match_rotation(source, template, angle, center)
angle_c, loc, res = template_match_rotation(source, template, angle, center)
# fine
angle, loc = template_match_rotation(source, template, angle_c, center, rot_min=-0.25, rot_max=+0.25, n_steps=20, scale=IMG_SCALE_UP)
angle, loc, res = template_match_rotation(source, template, angle_c, center, rot_min=-0.25, rot_max=+0.25, n_steps=20, scale=IMG_SCALE_UP)
return angle, loc
return angle, loc, res
class Reference:
@@ -85,6 +85,9 @@ print(f"ref_center = {ref_center}")
tracker.create()
tracker.init(frame_init, bbox_template)
COLOR_TRACKER = (0, 255, 0)
COLOR_TRACKER_EXT = (0, 255, 255)
COLOR_MATCHER = (0, 0, 255)
current_angle = 0
path = []
line_from = None
@@ -100,39 +103,40 @@ while True:
# Update tracker
ok, bbox_tracker = tracker.update(frame)
print(f"Bbox Tracker: {bbox_tracker}")
image_template_rotated = image_template
bbox_tracker_ext = bbox_extend(bbox_tracker, TEMPLATE_MATCH_OVERLAP)
if ok:
image_bbox = process_image_bbox(image_crop(frame, bbox_tracker_ext))
current_angle, max_loc = match(image_bbox, image_template, current_angle, ref_center)
crop = image_crop(frame.copy(), bbox_tracker_ext)
current_angle, max_loc, matcher_res = match(process_image_bbox(crop), image_template, current_angle, ref_center)
# determine the starting and ending (x, y)-coordinates of the
# bounding box
(startX, startY) = max_loc
print(f"current_angle: {current_angle:.2f}, startX: {startX}, startY: {startY}")
image_template_rotated = im.rotate(image_template, current_angle, center=ref_center)
# Matcher
matcher_bbox = (max_loc[0], max_loc[1], image_template.shape[1], image_template.shape[0])
matcher_bbox_global = bbox_round(bbox_add_position(matcher_bbox, bbox_tracker_ext))
matcher_top_left, matcher_bottom_right = to_rect(matcher_bbox_global)
cv2.rectangle(frame, matcher_top_left, matcher_bottom_right, (255, 0, 0), 2)
matcher_bbox_local = (max_loc[0], max_loc[1], image_template.shape[1], image_template.shape[0])
matcher_rect_local = to_rect(bbox_round(matcher_bbox_local))
matcher_bbox = bbox_round(bbox_add_position(matcher_bbox_local, bbox_tracker))
matcher_rect = to_rect(matcher_bbox)
# Tracker
tracker_top_left, tracker_bottom_right = to_rect(bbox_round(bbox_tracker))
cv2.rectangle(frame, tracker_top_left, tracker_bottom_right, (0, 0, 255), 2)
tracker_rect = to_rect(bbox_round(bbox_tracker))
# Tracker ext
tracker_top_left, tracker_bottom_right = to_rect(bbox_round(bbox_tracker_ext))
cv2.rectangle(frame, tracker_top_left, tracker_bottom_right, (255, 0, 255), 2)
tracker_ext_rect = to_rect(bbox_round(bbox_tracker_ext))
cv2.rectangle(frame, tracker_ext_rect[0], tracker_ext_rect[1], COLOR_TRACKER_EXT, 2)
cv2.rectangle(frame, tracker_rect[0], tracker_rect[1], COLOR_TRACKER, 2)
cv2.rectangle(frame, matcher_rect[0], matcher_rect[1], COLOR_MATCHER, 1)
# Calculate Frames per second (FPS)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
# Draw path
line_to = (round(startX + bbox_tracker[0]), round(startY + bbox_tracker[1]))
line_to = matcher_rect[0]
if line_from is not None:
path.append({'from': line_from, 'to': line_to})
line_from = line_to
@@ -145,16 +149,20 @@ while True:
cv2.putText(frame, "Tracking failure detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
# Display tracker type on frame
cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
cv2.putText(frame, tracker_type + " Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, COLOR_TRACKER, 2)
cv2.putText(frame, tracker_type + " Tracker Ext", (100, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.75, COLOR_TRACKER_EXT, 2)
cv2.putText(frame, tracker_type + " Matcher", (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.75, COLOR_MATCHER, 2)
# Display FPS on frame
cv2.putText(frame, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
cv2.putText(frame, "FPS : " + str(int(fps)), (100, 140), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
# Display result
cv2.imshow("Frame", frame)
cv2.imshow("Matcher", matcher_res)
cv2.imshow("Crop", image_crop(frame.copy(), matcher_bbox))
# Exit if ESC pressed
k = cv2.waitKey(1) & 0xff
k = cv2.waitKey(100) & 0xff
if k == 27:
break