learning target
- Master the methods of reading video files, displaying videos, and saving video files
1 Read video from file and play it
In OpenCV, if we want to get a video, 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
-
Video attribute information
2.1. Get some attributes of the video,
retval = cap.get(propId)
parameter:
- propId: a number from 0 to 18, each number represents the attribute of the video.
Common attributes are:
2.2 Modify the attribute information of the video
cap.set(propId,value)
parameter:
proid
: Index of the attribute, corresponding to the table abovevalue
: 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 successfulTrue
, it will be returned. If the acquisition fails, it will be returned.False
Frame
: Obtained image of a certain frame
-
Call the display image and set the appropriate duration when displaying the image. If it is too low, the video will play very fast. If it is too high, the video will play very slowly. Normally we can set it to 25ms
cv.imshow()
.cv.waitkey()
-
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 video
In OpenCV we use VedioWriter
objects to save videos, in which we specify the name of the output file, as shown below:
- Create an object for video writing
out = cv2.VideoWriter(filename,fourcc, fps, frameSize)
parameter:
filename
: The location where the video is savedfourcc
: 4-byte code specifying the video codecfps
: Frame rateframeSize
: Frame size- Set the codec of the video as shown below,
retval = cv2.VideoWriter_fourcc( c1, c2, c3, c4 )
parameter:
-
c1,c2,c3,c4
: is the 4-byte code of the video codec. The list of available codes can be found in fourcc.org. It is closely related to the platform. Commonly used ones are:
in Windows: DIVX (.avi)
in OS: MJPG (.mp4), DIVX (.avi), X264 (.mkv). -
Use to
cap.read()
obtain each frame of image in the video, and use out.write() to write a certain frame of image into the video. -
Use
cap.release()
andout.release()
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 the video:
cap = cv.VideoCapture()
- Determine the read is successful:
cap.isOpened()
- Read each frame of image:
ret,frame = cap.read()
- Get properties:
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()