把图像序列转换成视频
import os
import cv2
import numpy as np
path="C:/Users/007/Desktop/TwoViewUnsynch-master/first"
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')
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
def splitFrames(sourceFileName):
video_path = os.path.join('C:/Users/007/Desktop/data/book/', sourceFileName + '.mov')
outPutDirName = 'C:/Users/007/Desktop/data/book/' + sourceFileName + '/'
if not os.path.exists(outPutDirName):
os.makedirs(outPutDirName)
cap = cv2.VideoCapture(video_path)
num = 1
while True:
success, data = cap.read()
if not success:
break
cv2.imwrite( outPutDirName +str(num)+".jpg", data)
num = num + 1
print(num)
cap.release()
def splitFrames_mp4(sourceFileName):
video_path = os.path.join('/Users/huiqq/Desktop/experiment/', sourceFileName + '.mp4')
times = 0
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:
break
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 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)