openCV 视频分解及合成
程序员文章站
2022-03-25 18:50:13
1. 视频分解 2. 打开摄像头获取图片 3. 图片合成视频 ......
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()
上一篇: 如何在调试状态下部署局域网网站