- added ocv_template_matching.py for evaluationof template matcher - fixed template matching from tracker bounding box
50 lines
1.2 KiB
Python
50 lines
1.2 KiB
Python
import cv2
|
|
import sys
|
|
import imutils as im
|
|
|
|
from template_matcher import template_match_rotation
|
|
from util import bbox_extend, bbox_round, process_image_bbox, image_crop
|
|
|
|
|
|
video = cv2.VideoCapture("./data/IMG_1730.MP4")
|
|
|
|
# Exit if video not open
|
|
# d.
|
|
if not video.isOpened():
|
|
print("Could not open video")
|
|
sys.exit()
|
|
|
|
# Read first frame.
|
|
ok, image = video.read()
|
|
if not ok:
|
|
print('Cannot read video file')
|
|
sys.exit()
|
|
|
|
bbox_template = cv2.selectROI("Image", image, False)
|
|
template = image_crop(image, bbox_extend(bbox_template, 0.5))
|
|
cv2.imshow("Template", template)
|
|
|
|
# Perform template match operations
|
|
res = cv2.matchTemplate(image, template, cv2.TM_CCOEFF_NORMED)
|
|
(min_val, max_val, min_loc, max_loc) = cv2.minMaxLoc(res)
|
|
|
|
h, w = (template.shape[0], template.shape[1])
|
|
|
|
print(f"(w, h): {(w, h)}, max_loc: {max_loc}")
|
|
|
|
top_left = max_loc
|
|
bottom_right = (top_left[0] + w, top_left[1] + h)
|
|
|
|
cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
|
|
cv2.rectangle(image, (bbox_template[0], bbox_template[1]), (bbox_template[0] + bbox_template[2], bbox_template[1] + bbox_template[3]), (0, 0, 255), 2)
|
|
|
|
|
|
cv2.imshow("Image", image)
|
|
cv2.imshow("Matcher", res)
|
|
|
|
while True:
|
|
# Exit if ESC pressed
|
|
k = cv2.waitKey(1) & 0xff
|
|
if k == 27:
|
|
break
|