[OpenCV] read and saved Python video
Reprinted: https://blog.csdn.net/wsp_1138886114/article/details/84798977
Operating Environment Anaconda=5.3 | python=3.7
First, access to video from the camera
Creating a VideoCapture object. It is the name of the device parameters can be indexed or video files (will be mentioned below). Equipment index only numbers which camera specified. 0 represents a first camera, a second camera represents. After that, you can capture video frame by frame. The last release of Capture.
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read() #读取帧
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #灰度化展示
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'): #按‘q’退出
break
#释放资源并关闭窗口
cap.release()
cv2.destroyAllWindows()
Second, read the video file
Play video from a file, change the camera and video index file name. When the display frame, select the appropriate cv2.waitKey () time, if the value is too small, the video will be very fast, if it is too large, the video will be very slow (which may be used to display the video in slow motion). Under normal circumstances, to 25 msec.
import cv2
cap = cv2.VideoCapture('IMG_1521.mp4')
while(cap.isOpened()):
ret, frame = cap.read()
cv2.imshow('frame',frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
2.1 acquiring various properties of the camera / video - common function
cv2.VideoCapture.get(propId)
Video access certain functions, wherein propId is a number from 0 to 18, each number denotes a video attribute (Property Identifier).retval = cv2.VideoCapture.set(propId,value)
Some of these values can be cap.set (propId, value) to be modified, value is modified value.
For example: to check the width and height of the frame by cap.get (3) and cap.get (4), the default value is 640x480. Now changed to 320x240, useret = cap.set(3, 320)
andret = cap.set(4, 240)
.retval,image= cv2.VideoCapture.read([,image])
Fetch, decode and returns the next video frame. Returns the value of true indicate grab success. This function is a combination of Grab () and retrieve (), which is the most convenient way. If there is no frame, the function returns false, and outputs an empty image.retval, image = cv2.VideoCapture.retrieve([, image[, flag]])
And decoding the return video frame grabberretval = cv2.VideoCapture.grab()
The next frame grab from video files or camera. true to grab success. This function is mainly used for multi-camera.cv2.VideoCapture.release()
Close the file or video camera equipment.
retval = cv2.VideoCapture.get(propId) #打开视频文件或者相机设备进行视频捕获。
propId 常见取值如下:
"""
cv2.CAP_PROP_POS_MSEC: 视频文件的当前位置(ms)
cv2.CAP_PROP_POS_FRAMES: 从0开始索引帧,帧位置。
cv2.CAP_PROP_POS_AVI_RATIO:视频文件的相对位置(0表示开始,1表示结束)
cv2.CAP_PROP_FRAME_WIDTH: 视频流的帧宽度。
cv2.CAP_PROP_FRAME_HEIGHT: 视频流的帧高度。
cv2.CAP_PROP_FPS: 帧率
cv2.CAP_PROP_FOURCC: 编解码器四字符代码
cv2.CAP_PROP_FRAME_COUNT: 视频文件的帧数
cv2.CAP_PROP_FORMAT: retrieve()返回的Mat对象的格式。
cv2.CAP_PROP_MODE: 后端专用的值,指示当前捕获模式
cv2.CAP_PROP_BRIGHTNESS:图像的亮度,仅适用于支持的相机
cv2.CAP_PROP_CONTRAST: 图像对比度,仅适用于相机
cv2.CAP_PROP_SATURATION:图像饱和度,仅适用于相机
cv2.CAP_PROP_HUE: 图像色调,仅适用于相机
cv2.CAP_PROP_GAIN: 图像增益,仅适用于支持的相机
cv2.CAP_PROP_EXPOSURE: 曝光,仅适用于支持的相机
cv2.CAP_PROP_CONVERT_RGB:布尔标志,指示是否应将图像转换为RGB。
"""
Third, the video save
OpenCV provides an interface VideoWriter save for the video,
<VideoWriter object> = cv.VideoWriter( filename, fourcc, fps, frameSize[, isColor] )
Function parameters:
filename: a name to be saved video
fourcc: 4-byte code the given video codec
[( 'P', 'I' , 'M', '1') is an MPEG-1 codec]
[( 'M', 'J', 'P', 'G') is a motion jpeg codec]
fps: frame rate
frameSize: frame size
retval = cv2.VideoWriter_fourcc( c1, c2, c3, c4 )
The string 4 is connected fourcc code.cv.VideoWriter.write( image )
Save the image as a frame of video files.
isColor: If true, the video is color, grayscale or video, the default is true
import cv2
cap = cv2.VideoCapture(0) #打开相机
#创建VideoWriter类对象
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read() #捕获一帧图像
if ret==True:
frame = cv2.flip(frame,0) #沿x轴翻转
out.write(frame) #保存帧
cv2.imshow('frame',frame) #显示帧
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release() #关闭相机
out.release()
cv2.destroyAllWindows()
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
Acknowledgments
https://blog.csdn.net/qq_18995069/article/details/82772944