赞
踩
一、摄像头的调用
涉及函数:
函数1:cv2.Videoture()
函数2:ret, frame = cap.read()
- import cv2
-
- # 打开内置摄像头,设置成1或者其他值来调用其他摄像头
- cap = cv2.VideoCapture(0)
-
- # 确认摄像头是否成功打开
- if cap.isOpened() is False:
- print("Error")
- exit(1)
-
- while True:
- ret, frame = cap.read()
- # 显示图像帧
- cv2.imshow('frame', frame)
- # cv2.waitKey(1)中的1代表每隔1ms采集一帧,按q键退出采集
- # 摄像头采集速度一般是30帧~25帧,
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
-
- cap.release() # 释放VideoCapture
- cv2.destroyAllWindows()
二、播放和保存视频
注意:保存视频的时候,会存在视频编码。
指定写入视频帧编码格式,函数:
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
保存视频涉及的函数;
cv2.VideoWrite("output.avi", fourcc, 20.0, (640,480))
参数说明;
- import cv2
-
- # 打开视频文件
- cap = cv2.VideoCapture("./girl.mp4")
-
- # 确认视频是否成功打开
- if cap.isOpened() is False:
- print("error")
- exit(1)
-
- # 获取图片帧宽高
- frame_width = int(cap.get(3))
- frame_height = int(cap.get(4))
-
- # 创建保存视频,指定保存视频名称,指定视频编码器,视频帧率,图像帧尺寸
- fourcc = cv2.VideoWriter_fourcc('M', 'J', 'p', 'G')
- out = cv2.VideoWriter("girl_output.avi", fourcc, 30, (frame_width, frame_height))
-
- # 读取一帧图像,当视频帧读取完毕ret标识符为False
- ret, frame = cap.read()
-
- while ret:
- cv2.imshow("frame", frame) # 显示图像帧
- cv2.waitKey(20) # 帧间隔为20ms
- frame = cv2.flip(frame, 0) # 对图像进行水平翻转,注意这里可以换成其他图像操作
- out.write(frame) # 将frame写入视频
- ret, frame = cap.read() # 读取下一帧
-
- cap.release() # 释放VideoCapture对象
- out.release() # 释放保存的VideoWriter对象
- cv2.destroyAllWindows()
案列1:摄像头采集的视频保存
- import cv2
-
- cap = cv2.VideoCapture(0)
-
- fourcc = cv2.VideoWriter_fourcc('F', 'L', 'V', '1')
- out = cv2.VideoWriter("output.flv", fourcc, 20.0, (640, 480))
-
- while cap.isOpened():
- ret, frame = cap.read()
- if ret == True:
- out.write(frame)
- cv2.imshow("frame", frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- else:
- break
-
- cap.release()
- out.release()
- cv2.destroyAllWindows()
案列2:播放案例1生成的视频文件,并修改格式
- import cv2
-
- cap = cv2.VideoCapture("./output.flv")
-
- fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
- fps = cap.get(cv2.CAP_PROP_FPS) # 获取图像的帧率
- frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
- frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
- print("fps:{0}; width:{1}; height:{2}".format(fps, frame_width, frame_height))
-
- out = cv2.VideoWriter('output2map4.mp4', fourcc, fps, (frame_width, frame_height))
-
- while True:
- ret, frame = cap.read()
- if ret == True:
- # 1:水平翻转;0:竖直翻转
- frame = cv2.flip(frame, 1)
- out.write(frame)
- cv2.imshow('frame', frame)
- if cv2.waitKey(20) & 0xFf == ord('q'):
- break
- else:
- break
-
- cap.release()
- out.release()
- cv2.destroyAllWindows()
案例3:视频转化为图片(间隔帧存储)
- import cv2
- import os
-
- cap = cv2.VideoCapture(r"G:\热成像数据集\合成后的视频文件\总.mp4")
- c = 1 # 定义计数器
-
- if cap.isOpened():
- ret, frame = cap.read()
- else:
- # 利用断言语句抛出异常
- ret = False
- assert "视频文件无法读取"
-
- timeF = 30 # 视频帧计数间隔频率
-
- while ret: # 视频帧结束标志
- ret, frame = cap.read()
- if (c % timeF == 0):
- cv2.imshow('frame', frame)
- # cv2.imwrite(r'G:\热成像数据集\合成后的视频文件\image/' + str(c) + '.jpg', frame) # imwrite在py3中无法保存中文路径
- cv2.imencode('.jpg', frame)[1].tofile(r'G:\热成像数据集\合成后的视频文件\image/' + str(c) + '.jpg') # 存储为图像
- print('G:\热成像数据集\合成后的视频文件\image/' + '%06d' % c + '.jpg')
- c = c + 1
- cv2.waitKey(1)
- # print('ing')
-
- cap.release()
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。