gpt4 book ai didi

python-3.x - 无法使用Python OpenCV保存修改的框架

转载 作者:行者123 更新时间:2023-12-02 16:19:08 25 4
gpt4 key购买 nike

我编写了这段代码,以从本地存储中获取视频文件作为输入,然后显示它并以灰度重新编码帧,然后将其保存到输出文件中。这是我的代码:

import cv2 as cv

fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 25.0, (640, 480))
cap = cv.VideoCapture(input())
if not cap.isOpened():
print("Error opening video file")
exit()
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print("Can't receive frame. Exiting ...")
break

frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) #This line is causing the problem
out.write(frame)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break

cap.release()
out.release()
cv.destroyAllWindows()

使用此代码,我可以看到正在播放的视频文件,但是输出文件显示为空白。如果我将原始 frame对象写入输出文件,则可以使用。但是,每当我使用 frame = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)将帧转换为灰度时,它都无法正常工作,并且我得到一个空白的输出文件。我在做什么错?

最佳答案

从CV文档here中,VideoWriter构造函数的签名为

VideoWriter (const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)

即,如果要编写灰度图像,则需要将isColor参数设置为false,因为默认情况下它为true。用此替换行,
out = cv.VideoWriter('output.avi', fourcc, 25.0, (640,  480), 0)

关于python-3.x - 无法使用Python OpenCV保存修改的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62127432/

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