【问题标题】:OpenCV do not want to stop video recording with cv2.COLOR_BGR2GRAYOpenCV 不想用 cv2.COLOR_BGR2GRAY 停止视频录制
【发布时间】:2021-05-23 08:41:29
【问题描述】:

当我运行这段代码时:

from cv2 import *
image = cv2.VideoCapture(0, cv2.CAP_DSHOW)
fourcc_cod = cv2.VideoWriter_fourcc(*"XVID")
name = input()
video = cv2.VideoWriter(f"{name}.AVI",fourcc_cod,60,(640,480))
while (True):
    check,frame = image.read()
    frame1 = cvtColor(frame, cv2.COLOR_BGR2GRAY)
    video.write(frame1)
    cv2.imshow('myimage',frame1)
    if waitKey(1) ord('q'):
        cv2.destroyAllWindows()
video.release()
image.release()

即使我按“q”也不会停止录制视频

【问题讨论】:

    标签: python-3.x windows opencv


    【解决方案1】:

    试试这个 sn-p。

    import cv2
    cap = cv2.VideoCapture(0)
    name = input()
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter(f'{name}.avi', fourcc, 20.0, (640, 480))
    while True:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 0)
        # uncomment belove line for grayscale
        # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    # Release everything if job is finished
    cap.release()
    out.release() # video file writer
    cv2.destroyAllWindows()
    

    【讨论】:

    • 这并不能解决问题,因为我想要黑白视频
    • 你可以简单地添加这个:frame = cvtColor(frame, cv2.COLOR_BGR2GRAY)
    • 但是当我将此代码添加到程序中时,它不会制作视频。
    • @mohmmedelgamal,当您运行代码时,它要求为视频文件写入name,然后它将开始录制。
    • 是的,我知道,但它制作了一个 AVI 文件,但文件不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多