美文网首页
video 和 img 互相转换

video 和 img 互相转换

作者: 谢小帅 | 来源:发表于2018-12-12 15:06 被阅读38次
  1. img2video,文件夹下的一组图片,按照文件名排序,输出成视频
import cv2
import os


def img2video(imgpath, video):
    img_list = os.listdir(imgpath)
    img_list.sort(key=lambda t: int(t[:t.index('.')]))

    video_w, video_h = 640, 480
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    vw = cv2.VideoWriter(video, fourcc, 20, (video_w, video_h))

    for img in img_list:
        frame = cv2.imread(imgpath + img)  # any depth, cus default 8bit
        vw.write(frame)
        print('write', img)

    vw.release()


if __name__ == '__main__':
    img2video(imgpath='../img/lab_disp/seg_cdisp/', video='../img/lab_disp/seg_cdisp.avi')
  1. video2img,视频逐帧导出
import cv2
import os


def video2img(video, imgpath):
    if not os.path.exists(imgpath):
        os.mkdir(imgpath)
    cap = cv2.VideoCapture(video)
    cnt = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        cv2.imwrite(os.path.join(imgpath, str(cnt) + '.png'), frame)
        cnt += 1
        print(cnt)
    cap.release()


if __name__ == '__main__':
    video2img(video='lab/depth.avi', imgpath='out_d')
  1. video2video 对文件错误的 avi 格式视频进行重写
import cv2

"""
对文件错误的avi格式视频进行重写,导入PPT
"""

video_path = r'E:\2018.11.28 RedNet Semantic Segmentation using RGBD\rgb.avi'

video_w, video_h = 623, 293
fourcc = cv2.VideoWriter_fourcc(*'XVID')
vw = cv2.VideoWriter('rgb.avi', fourcc, 20, (video_w, video_h))

cap = cv2.VideoCapture(video_path)

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break
    vw.write(frame)

vw.release()
cap.release()

相关文章

网友评论

      本文标题:video 和 img 互相转换

      本文链接:https://www.haomeiwen.com/subject/aatphqtx.html