Image Sequence & Video 的互相转换

魏宸
2023-12-01

把图像序列转换成视频

import os
import cv2
import numpy as np
path="C:/Users/007/Desktop/TwoViewUnsynch-master/first"
#path=os.path.join(dir,)
filelist=os.listdir(path)

img=cv2.imread(path+'/'+'2011_09_26_drive_0005_sync_0000000000.png')
print(img.shape)   # 设定视频大小,一般和图像帧一样
   
fps=15 #帧率
size=(img.shape[1],img.shape[0]) #需要转为视频的图片的尺寸
video_dir="C:/Users/007/Desktop/TwoViewUnsynch-master/first.mp4"

fourcc=cv2.VideoWriter_fourcc('M','J','P','G') #opencv3.0
videoWriter=cv2.VideoWriter(video_dir,fourcc,fps,size)
k=-1
for i in range(k+1, k+len(filelist), 1):
    im_name=path +'/2011_09_26_drive_0005_sync_00000000'+ str(i)+ '.png'
    frame=cv2.imread(im_name)
    videoWriter.write(frame)
    print(im_name)

videoWriter.release()
print('finish')

视频分帧

import cv2
import os

# 从.mov 类型的视频中提取图像 , 更换一下后缀就可以处理.avi视频了
def splitFrames(sourceFileName):

    # 在这里把后缀接上
    video_path = os.path.join('C:/Users/007/Desktop/data/book/', sourceFileName + '.mov')
    outPutDirName = 'C:/Users/007/Desktop/data/book/' + sourceFileName + '/'
	# 注意更换path的时候有三个地方需要一起修改   还有main中的
	
    if not os.path.exists(outPutDirName):
        #如果文件目录不存在则创建目录
        os.makedirs(outPutDirName)

    cap = cv2.VideoCapture(video_path) # 打开视频文件
    num = 1
    while True:
        # success 表示是否成功,data是当前帧的图像数据;.read读取一帧图像,移动到下一帧
        success, data = cap.read()
        if not success:
            break
        # im = Image.fromarray(data, mode='RGB') # 重建图像
        # im.save('C:/Users/Taozi/Desktop/2019.04.30/' +str(num)+".jpg") # 保存当前帧的静态图像
        cv2.imwrite( outPutDirName +str(num)+".jpg", data)

        num = num + 1

        # if num % 20 == 0:
        #     cv2.imwrite('./Video_dataset/figures/' + str(num) + ".jpg", data)

        print(num)
    cap.release()

# 从.mp4 数据类型的视频中提取图像
def splitFrames_mp4(sourceFileName):

    # 在这里把后缀接上
    video_path = os.path.join('/Users/huiqq/Desktop/experiment/', sourceFileName + '.mp4')
    times = 0
    # 提取视频的频率,每25帧提取一个
    # frameFrequency = 25
    # 输出图片到当前目录vedio文件夹下
    outPutDirName = '/Users/huiqq/Desktop/experiment/' + sourceFileName + '/'

    # 如果文件目录不存在则创建目录
    if not os.path.exists(outPutDirName):
        os.makedirs(outPutDirName)

    camera = cv2.VideoCapture(video_path)
    while True:
        times+=1
        res, image = camera.read()
        if not res:
            # print('not res , not image')
            break

        # if times%frameFrequency==0:
        #     cv2.imwrite(outPutDirName + str(times)+'.jpg', image)
        #     print(outPutDirName + str(times)+'.jpg')

        cv2.imwrite(outPutDirName + str(times) + '.jpg', image)
        print(times,end='\t')
    print('\n图片提取结束')
    camera.release()

if __name__ == '__main__':

    im_file = 'C:/Users/007/Desktop/data/book/'

    # for im_name in im_names:
    for im_name in os.listdir(im_file):
        suffix_file = os.path.splitext(im_name)[-1]
        if suffix_file == '.mp4':
            print('~~~~~~~~~~ 从.mp4 视频提取图像 ~~~~~~~~~~~~~~~')

            sourceFileName = os.path.splitext(im_name)[0]
            splitFrames_mp4(sourceFileName)

        elif suffix_file == '.mov' :
            print('~~~~~~~~~~ 从.mov 视频提取图像 ~~~~~~~~~~~~~~~')

            sourceFileName = os.path.splitext(im_name)[0]
            splitFrames(sourceFileName)
 类似资料: