gpt4 book ai didi

python - 如何使用OpenCV更改视频中的单个帧?

转载 作者:行者123 更新时间:2023-12-02 17:35:58 25 4
gpt4 key购买 nike

我有一个使用OpenCV VideoWriter构建的视频。

我想用一个不同的框架来改变一个特定的框架。
有没有一种方法可以在不重建整个视频的情况下进行操作?

最佳答案

我通过将所有像素都设为零来更改第三帧。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

frame_number = -1

while(True):
frame_number += 1
ret, frame = cap.read()
if frame_number == 3: # if frame is the third frame than replace it with blank drame
change_frame_with = np.zeros_like(frame)
frame = change_frame_with
out.write(frame)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
out.release()
cv2.destroyAllWindows()

如果您不想再次浏览所有帧:
import numpy as np
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

frame_number = -1

while(True):
frame_number += 1
ret, frame = cap.read()
if frame_number == 3: # if frame is the third frame than replace it with blank drame
change_frame_with = np.zeros_like(frame)
frame = change_frame_with
out.write(frame)
break # add a break here
else:
out.write(frame)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break

cap.release()
out.release()
cv2.destroyAllWindows()
  • 但是上述解决方案不会将3之后的所有其他帧写入整个新视频
  • 关于python - 如何使用OpenCV更改视频中的单个帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50894377/

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