import cv2
import numpy as np
from matplotlib import pyplot as plt
template = cv2.imread('templateGray.PNG', 0)
height, width = template.shape[::]
vidcap = cv2.VideoCapture('Vid.mp4')
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('output.avi', fourcc, 30.0, (320, 240))
while vidcap.isOpened():
ret, frame = vidcap.read()
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF)
plt.imshow(res, cmap='gray')
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = min_loc
bottom_right = (top_left[0] + width, top_left[1] + height)
cv2.rectangle(frame, top_left, bottom_right, (255, 0, 0), 2)
out.write(frame)
cv2.waitKey(0)
vidcap.release()
out.release()
cv2.destroyAllWindows()
Traceback (most recent call last): File "(program name path here)", line 16, in res = cv2.matchTemplate(img_gray, template, cv2.TM_SQDIFF) cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1175: error: (-215:Assertion failed) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function 'cv::matchTemplate'
Process finished with exit code 1
thats my code and error message. i first started with a screenshot of the video saved to a variable called img_rgb with imread, then img_gray was the grayscale version of that image. when i did it like that it worked, but it broke when i switched to a frame from a video. does anyone know what im doing wrong?