OpenCV-python study notes (b) Video Processing

Video image consists of a series of series of image is called a frame, the frame rate of frames are acquired at fixed time intervals obtained from the video frame rate and the number of frames to be referred to as occurring within 1s, i.e. FPS
when video from independently extracted frame, the image processing method can be used to process it, so as to achieve the purpose of processing video

1 VideoCapture class

1.1 Introduction class function

VideoCapture class common function including initialization, opening, frame capture, release, and other property, the following brief description thereof

1. Initialization:
the OpenCV cv2.VideoCapture provided for turning on the camera and the camera initialization is completed VideoCapture class syntax is as follows:

cap = cv2.VideoCapture(摄像头ID号)

(1) .ID No. ID number of the camera, the default is -1, if a plurality of randomly selected from a camera, generally a value of 1, 2 Alternatively, if the laptop is not an external camera, then it should default 0 notebook is the camera
(2) captures a subject for the return value, which is the object class cv2.VideoCapture
Meanwhile, if the need to read the video file can be converted into a video parameter file full path

2.cv2.VideoCapture.open () () function and the function cv2.VideoCapture.isOpened:
In general, the use of cap = cv2.VideoCapture () function to complete the initialization of the camera. But also to prevent initialization error, you can use cv2.VideoCapture.isOpened () function to detect the initialization is successful
The syntax is:

retval = cv2.VideoCapture.isOpened()

(1) If the successful return value is True, otherwise it is False
if initialization fails camera cv2.VideoCapture.open (camera ID number) to open the camera is available again

3. Frame Capture:
capture a frame used function cv2.VideoCapture.read () function, its syntax is:

retval,image = cv2.VideoCapture.read()

(1). Image is captured frame to return, if capture is not successful, then the value of control
(2). Retval whether capture detection is successful, success is True

4. Release:
when not required it is necessary to close the camera function cv2.VideoCapture.release () is used for this, whose syntax is:

cv2.VideoCapture.release()

For example, currently there is a cap VideoCapture objects you want to release it, you can use the statement: cap.release ()

The property is set
Sometimes, I need to get the properties cv2.VideoCapture object, or change the type of an object's properties
cv2.VideoCapture.get () used to obtain the corresponding attributes, its syntax is:

retval = cv2.VideoCapture.get(propld)

(1). This corresponds propld property class object cv2.VideoCapture
e.g., a class object cv2.VideoCapture cvc, then
a. The width of the current frame may be acquired by the object cvc.get (cv2.CAP_PROP_FRAME_WIDTF)
B. By cvc .get (cv2.CAP_PROP_FRAME_HEIGHT) to obtain the current height of the frame object
function cv2.VideoCapture.set () to set the properties cv2.VideoCapture class object, the function syntax is:

retval = cv2.VideoCapture.set(propld,value)

. (1) propld cv2.VideoCapture class corresponding to the object's properties
(2) the need to set the attribute values of value.
Attribute values and meanings as follows:
Here Insert Picture Description
Here Insert Picture Description
6.cv2.VideoCapture.grab () function and cv2.VideoCapture.retrieve ( ) function
generally most convenient if the reading of a video camera data is to use the function cv2.VideoCapture.read ()
However, if the need to synchronize a set of camera or a long video data is required cv2.VideoCapture.grab () function and cv2.VideoCapture.retrieve () function to get the data of a plurality of cameras. (Actually cv2.VoidCapture.read () will be appreciated by cv2.VideoCapture.grab () function and cv2.VideoCapture.retrieve () function of the composition)
function cv2.VideoCapture.grab () syntax used to point to the next frame The format is:

retval,image = cv2.VideoCapture.grab()

(1) pointing to the next frame if the function succeeds, the return value retval is True, otherwise it is False
(2) Image video frame returned. If not successful, returns a null image.
For use with a set of camera:

success0 = cameraCapture0.grab()
success1 = cameraCapture1.grab()
if success0 and success1 : 
	frame0 = cameraCapture0.retrieve()
	frame1 = cameraCapture1.retrieve()

Also it can be used to read video files

1.2 Capture Video Camera

A simple example:

from cv2 import cv2
 
# 调用usb摄像头
cap = cv2.VideoCapture(0)
# 设置视频帧宽高
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
 
# 显示
while (cap.isOpened()):
    ret, frame = cap.read() 
    cv2.imshow("window",frame)
    
    if cv2.waitKey(1)&0xFF == ord("q"):
        break
        
# 关闭
cap.release()        
cv2.destroyAllWindows()

1.3 play video files

Above embodiment, may be provided by FPS () waitKey parameters to achieve slow release of the results

2 VideoWriter class

OpenCV in cv2.VideoWriter () class can save the picture sequence is a video file, may modify various properties of the video, the video may complete the conversion type

2.1 Introduction class function

1. Constructor
OpenCV provide constructor for class cv2.VideoWriter, use it to achieve initialization. The syntax is:

VideoWriterobject = cv2.VideoWriter(filename,fourcc,fps,frameSize,isColor)

(1) . filename为目标视频的完整路径
(2) . fourcc为视频编码类型(格式)
在OpenCV中用函数cv2.VideoWriter_fourcc()来指定视频编码格式,其有4个字符参数。这4个字符参数构成了编/解码器的"四字标记"每个编/解码器都有一个这样的标记,下面列出几个常用的标记 :
a. cv2.VideoWriter_fourcc(‘I’,‘4’,‘2’,‘0’)表示未压缩的 YUV 颜色编码格式,色度子采样为 4:2:0。该编码格式具有较好的兼容性,但产生的文件较大,文件扩展名为.avi。
b . cv2.VideoWriter_fourcc(‘P’,‘I’,‘M’,‘I’)表示 MPEG-1 编码类型,生成的文件的扩展名为.avi。
c . cv2.VideoWriter_fourcc(‘X’,‘V’,‘I’,‘D’)表示 MPEG-4 编码类型。如果希望得到的视频大小为平均值,可以选用这个参数组合。该组合生成的文件的扩展名为.avi。
d . cv2.VideoWriter_fourcc(‘T’,‘H’,‘E’,‘O’)表示 Ogg Vorbis 编码类型,文件的扩展名为.ogv。
e . cv2.VideoWriter_fourcc(‘F’,‘L’,‘V’,‘I’)表示 Flash 视频,生成的文件的扩展名为.flv。
若还想了解更多则可以去 http://www.fourcc.org上查询
若将fourcc置为-1则会在执行时会弹出一个对话框,可以根据自己的需要选择压缩程序和压缩质量
(3) . fps为帧速率
(4) . frameSize为每一帧的长和宽
(5) . isColor表示是否为彩色图像
例如 :

fourcc = -1
out = cv2.VideoWriter('output.avi',fourcc,20,(1024,768))
# 通过对话设置编/解码格式

2.write函数
cv2.VideoWriter类中的函数cv2.VideoWriter.write()用于写入下一帧的视频。该函数的语法格式为 :

cv2.VideoWriter.write(img)

(1) . 式中img为要写入的视频帧。通常情况下为BGR模式
例如,有一个视频帧frame要写入名为out的cv2.VideoWriter对象中,则使用语句 : out
.write(frame)

3.释放
在不需要cv2.VideoWriter类对象时需要将其释放,用到的函数为 : cv2.VideoWriter.release(),其语法格式为 :

cv2.VideoWriter.release()

例如当前有一cv2.VideoWriter对象out,则out.release()可以将其释放

2.2 保存视频

保存视频包括创建对象、写入视频、释放对象等多个步骤,下面对各个步骤做简单介绍
1.创建对象
在创建对象前应设置好参数。
(1) . 设置好具体的文件名,例如 : filename = ‘output.avi’
(2) . 使用cv2.VideoWriter_fourcc()确定编/解码的类型例如 : fourcc = cv2.VideoWriter_fourcc(*‘XVD’)
(3) . 确定视频的帧速率如 : fps = 20
(4) . 确定视频的长和宽,例如 : size = (640,480)
然后利用上述参数创建对象,例如 :

out = cv2.VideoWriter(filename,fource,fps,size)

当然也可以直接将参数输入到里面进行创建

2. Write video
, for example, to write the read out video frames within the object frame created:
out, Write (frame)

3. Release the object
out.release ()

Complete code:

from cv2 import cv2 as cv 
import numpy as np

# 开启摄像头
cap = cv.VideoCapture(0)
# 确定编/解码的类型
fourcc = cv.VideoWriter_fourcc('I','4','2','0')
# 创建对象并设置参数
out = cv.VideoWriter('output.avi',fourcc,20,(640,480))

while(cap.isOpened()):
	# 读取视频帧
    ret,frame = cap.read()
    if ret == True:
    	# 写入视频帧
        out.write(frame)
        # 显示
        cv.imshow('frame',frame)
        # esc退出
        if cv.waitKey(1) == 27 : 
            break
    else:
        break

# 释放并关闭窗口
cap.release()
out.release()
cv.destroyAllWindows()

Run the above program, the program will capture video content of the current camera, and save video files in the current directory called "output.avi" in.

3 Operating Basics Video

Canny edge detection as an example here to

# 提取视频的Canny边缘检测结果
import numpy as np
from cv2 import cv2 as cv 

cap = cv.VideoCapture('viptrain.avi')

while(cap.isOpened):
	ret,frame = cap.read()
	frame = cv.Canny(frame,100,200)
	cv.imshow('frame',frame)
	if cv.waitKey(1) == 27:
		break

cap.release()
cv.destroyAllWindows()

Run the program corresponding to the video is played Canny edge detection.

Reference article from: "Easy Start OpenCV: Python for" Lili Zong

Released nine original articles · won praise 0 · Views 440

Guess you like

Origin blog.csdn.net/k903161661/article/details/104425144