openCV 视频分解及合成
1. 视频分解
import cv2
# **************************
# 分解视频
cap=cv2.VideoCapture('1.mp4')#获取一个视频cap
isOpen=cap.isOpened()#判断是否打开
fps=cap.get(cv2.CAP_PROP_FPS)#频率,一秒多少张图片
width=int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))#宽
height=int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))#高
count=cap.get(cv2.CAP_PROP_FRAME_COUNT)
print('height,width,count',height,width,count)
i=0
while(isOpen):
if i==10:
break
else:
i+=1
(flag,frame)=cap.read()#读取每一张图片 flag:是否读取成功 frame:读取的照片
fileName='image'+str(i)+'.jpg'
if flag:
cv2.imwrite('./img/'+fileName,frame)
print('end!')
2. 打开摄像头获取图片
# 打开摄像头
cap=cv2.VideoCapture(0) #值为0 打开摄像头
isOpen=cap.isOpened()
i=1
while isOpen:
i+=1
(flag,frame)=cap.read()##读取每一张图片 flag:是否读取成功 frame:读取的照片
cv2.imshow(str(i),frame)
if flag:
cv2.imwrite('./img/'+str(i)+'.jpg',frame)
# 释放对象
if i == 20:
break
cap.release()
3. 图片合成视频
import cv2
fps = 24
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
videoWriter = cv2.VideoWriter('test.avi', fourcc, fps, (1080,1920))
for i in range(1,1000):
p2=i
img12 = cv2.imread('./img/image1.jpg')
# cv2.imshow('img', img12)
# cv2.waitKey(1000/int(fps))
print(str(p2))
videoWriter.write(img12)
videoWriter.release()