import os
import cv2
# ======================cap.get()========================#
# CV_CAP_PROP_POS_MSEC 视频当前点的毫秒值或视频捕捉的时间戳
# CV_CAP_PROP_POS_FRAMES 下次将被捕获的0基索引的帧
# CV_CAP_PROP_POS_AVI_RATIO 视频文件的相关位置: 0 - start of the film, 1 - end of the film.
# CV_CAP_PROP_FRAME_WIDTH 视频流帧的宽度
# CV_CAP_PROP_FRAME_HEIGHT 视频流帧的高.
# CV_CAP_PROP_FPS 帧率.
# CV_CAP_PROP_FOURCC 4字符编码的编码器.
# CV_CAP_PROP_FRAME_COUNT 视频文件的帧数.
# CV_CAP_PROP_FORMAT 由retrieve()返回矩阵对象的格式 .
# CV_CAP_PROP_MODE 后端指定值指示当前捕捉的模式.
# CV_CAP_PROP_BRIGHTNESS 图像亮度 (只对摄像头).
# CV_CAP_PROP_CONTRAST 图像对比度 (only for cameras).
# CV_CAP_PROP_SATURATION 图像饱和度 (only for cameras).
# CV_CAP_PROP_HUE 色调 (only for cameras).
# CV_CAP_PROP_GAIN 增益(only for cameras).
# CV_CAP_PROP_EXPOSURE 曝光(only for cameras).
# CV_CAP_PROP_CONVERT_RGB 布尔型标记图像是否应该被转换为RGB.
# CV_CAP_PROP_WHITE_BALANCE 白平衡(目前不支持)
# CV_CAP_PROP_RECTIFICATION 立体相机的矫正标记(note: only supported by DC1394 v 2.x backend currently)
# ======================cap.get()========================#
def videoCapture(video_name):
# 参数表示的摄像头的编号;如果读视频的话将视频文件路径传递进来
cap = cv2.VideoCapture(0)
# 获取视频帧的宽度高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 获取视频的FPS
fps = cap.get(cv2.CAP_PROP_FPS)
# 获取视频的编码
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
# 保存成视频格式
writer = cv2.VideoWriter(video_name, fourcc, fps, (width, height))
# 判断视频对象是否获取成功,成功则返回True
while cap.isOpened():
# 读取视频帧,ret是读取状态,ret为True表示读取成功,如果为False说明读取失败或者是到视频结尾了
ret, frame = cap.read()
cv2.imshow('video', frame)
key = cv2.waitKey(24)
# 保存视频帧
writer.write(frame)
# 如果按下q,退出
if key == ord('q'):
break
# 释放摄像头
cap.release()
# 释放所有显示图像的窗口
cv2.destroyAllWindows()
def video2image():
# 参数表示的摄像头的编号;如果读视频的话将视频文件路径传递进来
cap = cv2.VideoCapture(0)
# 获取视频帧的宽度高度
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 获取视频的FPS
fps = cap.get(cv2.CAP_PROP_FPS)
# 获取视频的编码
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
print(f"video width:{width};video height:{height}; fps:{fps};fourcc:{fourcc}")
# 每一帧之间的间隔
timeF = int(fps)
n = 1 # 视频帧总数
i = 1 # 保存帧总数
while cap.isOpened():
# 读取视频帧,ret是读取状态,ret为True表示读取成功,如果为False说明读取失败或者是到视频结尾了
ret, frame = cap.read()
if ret:
break
# 每间隔timeF帧进行存储一次
if (n % timeF == 0):
i += 1
print(f"保存的第{i}帧图像")
save_path = os.path.join('image', f"{i}.jpg")
cv2.imwrite(save_path, frame)
n += 1
cv2.waitKey(1)
# 释放摄像头或者视频
cap.release()
cv2读取视频
最新推荐文章于 2024-05-24 13:54:45 发布