Video reading and writing
To get a video in OpenCV, we need to create a VideoCapture object and specify the video file you want to read:
Create an object that reads the video
cap = cv.VideoCapture(filepath)
parameter:
filepath: video file path
Attribute information of the video
2.1. Get some properties of the video,
retval = cap.get(propId)
parameter:
propId: a number from 0 to 18, each number represents the property of the video
Common attributes are:
2.2 Modify the attribute information of the video
cap.set(propId
,
value)
parameter:
proid: the index of the attribute, corresponding to the table above
value: the modified attribute value
Determine whether the image is read successfully
isornot = cap.isOpened()
Returns true if the read is successful, otherwise returns False
Get a frame of video
ret, frame = cap.read()
parameter:
ret: If the acquisition is successful, return True, if the acquisition fails, return False
Frame: The image of a certain frame obtained
Call cv.imshow() to display the image, use cv.waitkey() to set the appropriate duration when displaying the image, if it is too low, the video will play very fast, if it is too high, it will play very slowly, usually we set 25ms is fine.
Finally, call cap.realease() to release the video
Example:
import numpy as np
import cv2 as cv
# 1.获取视频对象
cap = cv.VideoCapture('DOG.wmv')
# 2.判断是否读取成功
while(cap.isOpened()):
# 3.获取每一帧图像
ret, frame = cap.read()
# 4. 获取成功显示图像
if ret == True:
cv.imshow('frame',frame)
# 5.每一帧间隔为25ms
if cv.waitKey(25) & 0xFF == ord('q'):
break
# 6.释放视频对象
cap.release()
cv.destoryAllwindows()
2 save the video
In OpenCV, we use the VedioWriter object to save the video, and specify the name of the output file in it, as follows:
Create an object for video writing
out = cv2.VideoWriter(filename,fourcc, fps, frameSize)
parameter:
filename : the location where the video is saved
fourcc : A 4-byte code specifying the video codec
fps : frame rate
frameSize : frame size
Set the codec of the video as follows,
retval = cv2.VideoWriter_fourcc( c1, c2, c3, c4 )
parameter:
c1, c2, c3, c4: are the 4-byte codes of the video codec. Find the list of available codes in fourcc.org , which are closely related to the platform. The commonly used ones are:
In Windows: DIVX (.avi)
In OS: MJPG (.mp4), DIVX (.avi), X264 (.mkv).
Use cap.read() to get each frame of image in the video, and use out.write() to write a frame of image into the video.
Use cap.release() and out.release() to release resources.
Example:
import cv2 as cv
import numpy as np
# 1. 读取视频
cap = cv.VideoCapture("DOG.wmv")
# 2. 获取图像的属性(宽和高,),并将其转换为整数
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# 3. 创建保存视频的对象,设置编码格式,帧率,图像的宽高等
out = cv.VideoWriter('outpy.avi',cv.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
while(True):
# 4.获取视频中的每一帧图像
ret, frame = cap.read()
if ret == True:
# 5.将每一帧图像写入到输出文件中
out.write(frame)
else:
break
# 6.释放资源
cap.release()
out.release()
cv.destroyAllWindows()
Summarize
Read the video:
Read video: cap = cv.VideoCapture()
Judging that the read is successful: cap.isOpened()
Read each frame image: ret, frame = cap.read()
Get attributes: cap.get(proid)
Set properties: cap.set(proid,value)
Resource release: cap.release()
save video
Save video: out = cv.VideoWrite()
Video writing: out.write()
Resource release: out.release()