gpt4 book ai didi

python - 将视频中检测到的边缘帧保存为视频(opencv)

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

我想将边缘检测到的帧保存为视频。我可以预览视频的帧,但输出视频不播放。

cap = cv2.VideoCapture("video.mp4")

fourcc = cv2.VideoWriter_fourcc(*'MP4V')

out = cv2.VideoWriter('output.mp4', fourcc, 23.976, (1280,720))

while(cap.isOpened()):
ret, frame = cap.read()
edges = cv.Canny(frame,50,50)
out.write(edges)
cv2.imshow('frame', edges)
c = cv2.waitKey(1)
if c & 0xFF == ord('q'):
break

cap.release()
out.release()

最佳答案

我想解决三个问题。


  • 您正在申请Canny到每个视频帧。输出大小是多少:

  • print(edges.shape)
    (720, 1280)
  • 维度720, 1280表示该帧是一个灰度值。如果框架是彩色图像,则尺寸将为 (720, 1080, 3)对于每个 channel R、G、B。
  • 因此你需要初始化你的VideoWriter对象作为 grey-scale图片。

  • out = cv2.VideoWriter('output.mp4', fourcc, 23.976, (1280,720), isColor=False)



  • 确保您正在捕获下一帧:

  • ret, frame = cap.read()
    if ret:
    edges = cv.Canny(frame,50,50)
    .
    .



  • 确保您的框架与 VideoWriter 的尺寸相同。对象

  • while cap.isOpened():
    ret, frame = cap.read()
    if ret:
    edges = cv2.Canny(frame, 50, 50)
    edges = cv2.resize(edges, (1280, 720))
    out.write(edges)
    .
    .


  • 代码:

    import cv2

    cap = cv2.VideoCapture("video.mp4")

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")

    out = cv2.VideoWriter('output.mp4', fourcc, 23.976, (1280, 720), isColor=False)

    while cap.isOpened():
    ret, frame = cap.read()
    if ret:
    edges = cv2.Canny(frame, 50, 50)
    edges = cv2.resize(edges, (1280, 720))
    out.write(edges)
    cv2.imshow('frame', edges)
    c = cv2.waitKey(1)
    if c & 0xFF == ord('q'):
    break
    else:
    break

    cap.release()
    out.release()

    关于python - 将视频中检测到的边缘帧保存为视频(opencv),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64537408/

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