1.视频转图片
import cv2
import numpy
import math
cap = cv2.VideoCapture("./帯広空港.mp4")
vedio_frame_count = cap.get(7) # 获取视频总帧数
vedio_fps = math.ceil(cap.get(5)) # 获取视频帧率
frame_width = cap.get(3) # 获取视频帧宽度
frame_height = cap.get(4) # 获取视频帧高度
print(vedio_frame_count,vedio_fps)
frame_id = 1
while(True):
ret, frame = cap.read()
if not ret or cv2.waitKey(30)==ord('q'):
break;
cv2.imshow("frame",frame)
frame_id += 1
cap.release()
cv2.destroyAllWindows()
- opencv参数列表
0 CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
1 CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2 CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
3 CV_CAP_PROP_FRAME_WIDTH #视频帧宽度
4 CV_CAP_PROP_FRAME_HEIGHT #视频帧高度
5 CV_CAP_PROP_FPS #视频帧速率
6 CV_CAP_PROP_FOURCC 4-character code of codec.
7 CV_CAP_PROP_FRAME_COUNT #视频总帧数
8 CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9 CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10 CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11 CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12 CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
13 CV_CAP_PROP_HUE Hue of the image (only for cameras).
14 CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15 CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16 CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17 CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
18 CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
19 CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
20 CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
21 CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)
2.图片转视频
# 图片转视频
import cv2
import os
img_dir = "./data_handle/img/" # 必须保证图片是相同大小的,否则会转换失败
img_list = os.listdir(img_dir)
frame_rate = 30 # 帧率
frame_shape = cv2.imread(os.path.join(img_dir,img_list[0])).shape[:-1] # 图片大小/帧shape
frame_shape = (frame_shape[1],frame_shape[0]) # 交换w和h
videoWriter = cv2.VideoWriter('result.mp4', cv2.VideoWriter_fourcc(*'MJPG'), frame_rate, frame_shape) # 初始化视频帧writer
# 开始逐帧写入视频帧
frame_id = 1
for img_filename in img_list:
img_path = os.path.join(img_dir,img_filename)
img = cv2.imread(img_path)
videoWriter.write(img)
frame_id += 1
if frame_id%100 == 0:
break
videoWriter.release()
评论 (0)