With the help of OpenCV, multiple frames of images are combined into a video and OpenCV records and saves the video


Author:qyan.li

Date:2022.6.19

Topic: With the help of OpenCV, multiple frames of images are combined into a video and OpenCV records and saves the video

1. Write in front

         ~~~~~~~~         Recently, I have been searching on the Internet how to combine multiple frames of images into a video with the help of OpenCV. I learned that it can be realized with the help of the VideoWrite() function. By the way, I will learn how to use the VideoWriter function, and use this function to record and save the camera video. The frame pictures are synthesized into a video.

2. Video recording and storage

​ As usual, first post the code for your reference:

def VideoWrite_Function():
    cap = cv2.VideoCapture(0)

    # ## 输出摄像屏幕的大小尺寸
    # width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    # height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    # print("width:", width, "height:", height) ## width = 640,height = 480

    fourcc = cv2.VideoWriter.fourcc('m', 'p', '4', 'v') # 指定输出视频的格式
    ## VideoWriter(fileName,fourcc,fps,frameSize[:,iscolor])创建VideoWriter对象

    ## 调用摄像头录制视频并保存时,文件后缀名为mp4无法正常播放,改为mp4v后可以正常进行播放
    ## 参考文献:https://blog.csdn.net/bgmcat/article/details/120751531
    out = cv2.VideoWriter('./output.mp4v', fourcc, 20, (640,480))
    while (cap.isOpened()):
        ## 添加判断,相机是否成功打开
        ret, frame = cap.read()
        if ret:
            ## 调用Canny进行图像的边缘检测
            # frame = cv2.Canny(frame, 100, 200)
            out.write(frame)
            cv2.imshow('video', frame)
            c = cv2.waitKey(1)
            if c == 27:
                break
        else:
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()

A few notes about this code:

  • cap = cv2.VideoCapture(0) is used to obtain the default built-in camera of the laptop, if the external camera can change the incoming parameters
  • fourcc = cv2.VideoWriter.fourcc('m', 'p', '4', 'v') is used to specify the format of the output video, there are many other formats that can be modified, please search by yourself
  • VideoWriter() constructor->VideoWriter(filePath, Videotype, fps, frameSize), after the instantiation of the class is completed, write the frame with the help of the write() function
  • The frameSize passed in the VideoWriter() constructor must be consistent with its own computer , which can be determined with the help of the commented code
  • The file suffix of the output video must be mp4v instead of mp4, otherwise the video can be saved normally, but cannot be played externally

3. Multi-frame pictures are synthesized into video

​ As usual, first post the code for your reference:

def createVideo(filePath):
    ## 创建视频合成格式
    fourcc = cv2.VideoWriter.fourcc('m', 'p', '4', 'v')  # 指定输出视频的格式
    ## VideoWriter(fileName,fourcc,fps,frameSize[:,iscolor])创建VideoWriter对象

    ## 此处的图像大小必须与原始图像的大小保持一致,否则会报错
    out = cv2.VideoWriter('./TestOutput.mp4', fourcc, 5, (220,480)) # 此处的frame.shape = (480, 220, 3)

    ## filePath即为图片保存路径
    fileNames = os.listdir(filePath)
    print(fileNames)
    for file in fileNames:
        ## 错误:cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
        ## 代码中出现任何问题基本上都会报此错误,检查代码即可,不必过分纠结于错误提示
        # frame = cv2.imread('./' + str(file))
        frame = cv2.imread(str(filePath) + '/' + str(file))
        # print(frame.shape)
        out.write(frame)
        cv2.imshow('video',frame)
        c = cv2.waitKey(1)
        if c == 27:
            break

    out.release()
    cv2.destroyAllWindows()

A few small notes about this code:

  • The object written by the write method of VideoWriter is the object returned after imread, and the image must be read by the imread function
  • The frameSize in the VideoWriter constructor must also match the size of the picture, but unlike camera recording, the position needs to be changed here , that is, frame.shpe outputs (480, 220, 3), and fills in (220,480) in the constructor
  • Here you can use the file extension of mp4 instead of mp4v

3. Summary

         ~~~~~~~~         To sum up, the core of the code lies in the construction and use of the VideoWriter() class. There are two points to note when using it:

  1. The last parameter frameSzie in the VideoWriter() class needs to be compatible with the computer screen and the size of the picture. When combining multiple frames of pictures into a video, you need to reverse the size of the picture to form a tuple and pass it in

  2. The write method of the VideoWriter() class does not pass in the picture itself, but the object after cap.read or imread

    Also, a note about image saving:

  • The suffix of the saved file of the video recorded by the camera cannot be mp4, otherwise the video cannot be played normally, and it should be changed to mp4v. Other formats should also work. I have not tried it myself. Note that it needs to match with fourcc

    Finally, a note about program execution:

  • Error reporting cv2.error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor', according to my own code debugging experience, no matter what error occurs in the program, this error will basically occur, so don't worry too much about the error message and pay attention to the code.

Guess you like

Origin blog.csdn.net/DALEONE/article/details/125359726