Image Sequence & Video 的互相转换

这段代码展示了如何使用OpenCV将图像序列转换为视频,以及从.mp4和.mov视频中提取图像帧。它定义了两个函数,一个用于合成视频,另一个用于分帧。通过指定帧率和输出尺寸,可以自定义视频的生成,同时可以根据需要调整提取图像的频率。
摘要由CSDN通过智能技术生成

把图像序列转换成视频

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值