因为研究方向问题需要先视频处理,方便后面进行检测等,这里记录下常用的,方便以后

 

测试所用的视频:https://media.w3.org/2010/05/sintel/trailer.mp4

 

VideoCapture中get和set函数常用的参数如下,值得注意的是在opencv2版本中需要按下面加上CV_前缀,但是在opencv3中是不需要CV_前缀的,下面19个参数依次对应整数0-18

python-opencv-视频处理常用函数

测试代码:

# coding: utf-8

import numpy as np
import cv2
import shutil
import os

print ("the opencv version: {}".format(cv2.__version__))

save_path = "./save_each_frames"
if(os.path.exists(save_path)):
    shutil.rmtree(save_path)
os.mkdir(save_path)

#cap = cv2.VideoCapture("./trailer.mp4")
cap = cv2.VideoCapture()
cap.open("./trailer.mp4")
if cap.isOpened() != True:
    os._exit(-1)

#get the total numbers of frame
totalFrameNumber = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print ("the number of frames is {}".format(totalFrameNumber))

#set the start frame to read the video
frameToStart = 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frameToStart);

#get the frame rate
rate = cap.get(cv2.CAP_PROP_FPS)
print ("the frame rate is {} fps".format(rate))

# get each frames and save
frame_num = 0
while True:
    ret, frame = cap.read()
    if ret != True:
        break
    img_path = os.path.join(save_path, str(frame_num)+".jpg")
    cv2.imwrite(img_path,frame)
    frame_num = frame_num + 1

cap.release()

测试结果:

python-opencv-视频处理常用函数

 

 

相关文章: