template
#main.py
import numpy as np
import cv2
cap = cv2.VideoCapture("assets/Santa Barbara.mp4")
template = cv2.resize(cv2.imread('assets/boat.png', 0), (0, 0), fx=0.6, fy=0.6)
h, w = template.shape
method = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR,
cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]
while True:
ret, frame = cap.read()
width = int(cap.get(3))
height = int(cap.get(4))
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
select_method = method[1]
result = cv2.matchTemplate(gray, template, select_method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if select_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
location = min_loc
else:
location = max_loc
bottom_right = (location[0] + w, location[1] + h)
cv2.rectangle(frame, location, bottom_right, (0, 0, 255), 5)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
reference:
No comments:
Post a Comment