简体   繁体   English

使用 opencv 将彩色/灰度图像转换为具有可控帧速率的视频

[英]color/grayscale images to videos with controllable frame rate using opencv

I am writing a function to create a video from images in a folder.我正在编写 function 以从文件夹中的图像创建视频。 I am able to do it separately for grayscale or color but unable to generalize.我可以单独为灰度或颜色做,但无法概括。 Could you please help?能否请你帮忙? Here is what I have written till now for grayscale images:这是我到目前为止为灰度图像编写的内容:

import cv2
import os

def images_to_video(images_folder, output_video_path, fps):
    images = [img for img in os.listdir(images_folder)]
    images.sort()
    frame = cv2.imread(os.path.join(images_folder, images[0]))
    height, width = frame.shape
    video = cv2.VideoWriter(output_video_path, 0, fps, (width,height))
    for image in images:
        video.write(cv2.imread(os.path.join(images_folder, image)))
    cv2.destroyAllWindows()
    video.release()

This question helped a bit but I need to generalize to grayscale/color and my frame rate is static (not variable).这个问题有点帮助,但我需要概括为灰度/颜色,我的帧速率是 static(不可变)。

You just need to add a variable for channel.您只需要为通道添加一个变量。 It will not be used actually.它不会被实际使用。 It will just take value of 1 for grayscale and 3 for color image.灰度图像只取值 1,彩色图像取值 3。

import cv2
import os

def images_to_video(images_folder, output_video_path, fps):
    images = [img for img in os.listdir(images_folder)]
    images.sort()
    frame = cv2.imread(os.path.join(images_folder, images[0]))
    height, width, channels = frame.shape
    video = cv2.VideoWriter(output_video_path, 0, fps, (width,height))
    for image in images:
        video.write(cv2.imread(os.path.join(images_folder, image)))
    cv2.destroyAllWindows()
    video.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM