I am performing template matching and everything seems to be working fine in cv2.imshow()
.
I want to write a video with all frames resulting from the template matching process.
Part of my code includes resizing the template image and the video frame dimensions, I don't know if the video writing problem comes from this step or not.
The errors are coming mostly from the out.write(frame)
part.
I get the following error after using the code below:
cv2.error: OpenCV(3.4.2) C:\Miniconda3\conda-bld\opencv-suite_1534379934306\work\modules\videoio\src\cap_ffmpeg.cpp:296: error: (-215:Assertion failed) image.depth() == 0 in function 'cv::`anonymous-namespace'::CvVideoWriter_FFMPEG_proxy::write'
Any ideas?
import cv2
import numpy as np
cap = cv2.VideoCapture(input_video_file)
fps = int(round(cap.get(5)))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # saving output video as .mp4
out = cv2.VideoWriter(output_video_file, fourcc, fps, (frame_width, frame_height))
# while loop where the real work happens
while cap.isOpened():
ret, frame = cap.read()
if ret:
if cv2.waitKey(28) & 0xFF == ord('q'):
break
# Read template image from path
template = cv2.imread('D:\KU_Leuven\Computer_Vision\Assignment_1\Videos\Raspberry_Template.jpg',0)
h2,w2 = template.shape[:2]
#resize video frame dimensions
scale_percent =60 # percentage of original video frame size
width = int(frame.shape[1] * scale_percent / 100)
height = int(frame.shape[0] * scale_percent / 100)
dim = (width, height)
frame = cv2.resize(frame , dsize = dim)
#resize template dimensions
scale_percent = 16# percentage of original template image size
width = int(template.shape[1] * scale_percent / 100)
height = int(template.shape[0] * scale_percent / 100)
dim = (width, height)
template = cv2.resize(template , dsize = dim)
method = eval('cv2.TM_CCOEFF_NORMED')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# template match
res = cv2.matchTemplate(gray,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# Draw box around the Max likelihood location of the template
top_left = max_loc
h,w =template.shape[:2]
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(gray,top_left, bottom_right, (255,0,0), 2)
res = cv2.putText(res,'Likelihood Map from Template Matching', (50,50),cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255,255,255), 2, cv2.LINE_AA)
cv2.rectangle(frame,top_left, bottom_right, (51,255,51), 2)
cv2.imshow("Color Video Feed - Box",frame)
cv2.imshow("Grayscale Video Feed - Box",gray)
cv2.imshow("Likelihood Map",res) # produces the correct result
res = cv2.resize(res,(frame_width,frame_height))
out.write(res)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and writing object
cap.release()
out.release()
# Closes all the frames
cv2.destroyAllWindows()