gpt4 book ai didi

opencv - 使用来自 cv2.VideoWriter 的 write() 后的错误/空视频

转载 作者:行者123 更新时间:2023-12-05 05:44:38 27 4
gpt4 key购买 nike

我正在执行模板匹配,在 cv2.imshow() 中似乎一切正常。我想编写一个视频,其中包含模板匹配过程产生的所有帧。我的部分代码包括调整模板图像大小和视频帧尺寸,我不知道视频编写问题是否来自这一步。错误主要来自 out.write(frame) 部分。

使用下面的代码后出现以下错误:

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) 图片.depth() == 0 in function 'cv::`anonymous-namespace'::CvVideoWriter_FFMPEG_proxy::write'

有什么想法吗?

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()

最佳答案

以下内容可能有助于使用 opencv VideoWriter 将 H264 编码的视频保存到 mp4 文件中:

import cv2
#print(cv2.__version__)

# Uncommenting the following would allow checking if your opencv build has FFMPEG and GSTREAMER support
#print(cv2.getBuildInformation())

# Video source
cap = cv2.VideoCapture('videotestsrc ! videoconvert ! queue ! video/x-raw, format=BGR ! appsink drop=1', cv2.CAP_GSTREAMER)

# fps is a float value, width and height are integer values
fps = float(cap.get(cv2.CAP_PROP_FPS))
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Using FFMPEG backend: 4CC specifies codec, container is found from filename extension...here using mp4
output_video_file='test_H264.mp4'
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter(output_video_file, cv2.CAP_FFMPEG, fourcc, fps, (frame_width, frame_height))

# Alternatively, using GSTREAMER backend:
#out = cv2.VideoWriter('appsrc ! queue ! video/x-raw,format=BGR ! videoconvert ! x264enc ! h264parse ! qtmux ! filesink location=test_H264.mp4', cv2.CAP_GSTREAMER, 0, fps, (frame_width, frame_height))
# With NVIDIA, you may use HW encoder:
#out = cv2.VideoWriter('appsrc ! queue ! video/x-raw,format=BGR ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! video/x-raw(memory:NVMM),format=NV12 ! nvv4l2h264enc ! h264parse ! qtmux ! filesink location=test_H264.mp4', cv2.CAP_GSTREAMER, 0, fps, (frame_width, frame_height))


if not cap.isOpened():
print("Failed to open capture")
exit()

if not out.isOpened():
print("Failed to open writer")
exit()

while cap.isOpened():
ret, frame = cap.read()
out.write(frame)
cv2.imshow('Test', frame)
cv2.waitKey(1)

关于opencv - 使用来自 cv2.VideoWriter 的 write() 后的错误/空视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71563084/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com