我正在尝试使用 OpenCV 和Python从笔记本电脑中保存2个视频,这些视频具有相同的帧大小和像素值。 我正在更改第二个视频中帧中某些像素的RGB值。 我想保存视频而没有之间的任何区别,除了像素变化以外,但是当我使用 cv2.VideoWriter 时,有一个fourcc编解码器可以压缩视频并我的项目失败了,因为我想 稍后在像素中检索更改的信息。视频的大小和像素值不同。 例如,使用以下代码,我录制了一秒钟的视频并将其保存两次(第二个视频的像素已更改)。
import cv2
from numpy import *
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Get the default resolution.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc(*"XVID"), 25, (frame_width,frame_height))
out2 = cv2.VideoWriter('outpy2.avi',cv2.VideoWriter_fourcc(*"XVID"), 25, (frame_width,frame_height))
while(True):
ret, frame = cap.read()
if ret == True:
# changing the BGR values
out.write(frame)
frame[1,1,0] = 255
frame[1,1,1] = 255
frame[1,1,2] = 255
out2.write(frame)
# Display the resulting frame
cv2.imshow('frame',frame)
# Press Q on keyboard to stop recording
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release the video capture and video write objects
cap.release()
out.release()
out2.release()
# Closes all the frames
cv2.destroyAllWindows()
第一个视频的大小为 314 120字节,第二个视频的大小为 314′452字节,视频中的帧像素值也不同。
因此,我尝试使用“ Lagarith无损视频编解码器”来避免像out=cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc(*"LAGS"), 25, (frame_width,frame_height))
这样的压缩,但是却收到错误消息:找不到编解码器ID 146的编码器:找不到编码器。
之后,我尝试通过第一个链接从https://lags.leetcode.net/codec.html下载LAGS编解码器,安装了编解码器,但遇到了同样的错误。我不知道问题出在哪里。
如果我没看错,我的问题是由于Fourcc编解码器的压缩。 LAGS编解码器是解决方案吗?如果不是,请提出解决方案。