opencv advanced 09- video processing cv2.VideoCapture example (open the local computer camera)

Video signal (hereinafter referred to as video) is a very important source of visual information, and it is a type of signal that is often processed in the visual processing process. In fact, a video is made up of a series of images called frames, and frames are taken from the video at regular intervals. The speed of acquiring (playing) frames is called the frame rate, and its unit is usually expressed in "frames per second", which represents the number of frames that appear within 1 second, and the corresponding English is FPS (Frames Per Second). If independent frames are extracted from the video, they can be processed by image processing methods to achieve the purpose of video processing.

OpenCV provides cv2.VideoCapture class and cv2.VideoWriter class to support various types of video files. In different operating systems, the file types they support may vary, but video files in AVI format are supported in various operating systems.

VideoCapture class

OpenCV provides cv2.VideoCapture 类来处理视频. The way cv2.VideoCapture class handles video is very simple and fast, and it can handle both video files and camera information.

Class function introduction

Common functions of cv2.VideoCapture class include initialization, opening, frame capture, release, property setting, etc.
These functions are briefly introduced below.

1. Initialize
OpenCV provides a constructor for the cv2.VideoCapture class cv2.VideoCapture(), which is used to open the camera and complete the initialization of the camera. The syntax of this function is:

Capture object = cv2.VideoCapture("Camera ID number")

In the formula:

  • "Camera ID" is the ID number of the camera. It should be noted that this parameter is the ID number of the camera device (camera), not the file name. Its default value is -1, which means randomly selecting a camera; if there are multiple cameras, use the number "0" to represent the first camera, use the number "1" to represent the second camera, and so on.
    So, if there is only one camera, you can use either "0" or "-1" as the camera ID number. On some platforms, if the value of this parameter is "-1", OpenCV will pop up a window to let the user manually select the camera they want to use.

  • "Capture object" is the return value, which is the object of cv2.VideoCapture class.
    For example, to initialize the current camera, you can use the statement:

cap = cv2.VideoCapture(0)

When introducing the function cv2.VideoCapture() on the OpenCV official website, it is particularly emphasized that after the video is processed, remember to release the camera object.
This (constructor) function can also be used to initialize a video file. When initializing a video file, the parameter is the file name. The form of the function at this time is:

Capture object = cv2.VideoCapture("file name")

For example, to open the video file named "vtest.avi" in the current directory, you can use the statement:

cap = cv2.VideoCapture('vtest.avi')
  1. cv2.VideoCapture.open() function and cv2.VideoCapture.isOpened() function

In general, use the cv2.VideoCapture() function to complete the initialization of the camera. Sometimes, in order to prevent initialization errors, you can use the function cv2.VideoCapture.isOpened() to check whether the initialization is successful. The syntax of this function is:

retval = cv2.VideoCapture.isOpened()

This function will judge whether the current camera is initialized successfully:

  • If successful, the return value retval is True.
  • If unsuccessful, the return value retval is False.

If the camera initialization fails, you can use the function cv2.VideoCapture.open() to open the camera. The syntax of this function is:

retval = cv2.VideoCapture.open( index )

In the formula:

  • index is the camera ID number.
  • retval is the return value, when the camera (or video file) is successfully opened, the return value is True.

Similarly, the functions cv2.VideoCapture.isOpened() and cv2.VideoCapture.open() can also be used to process video files. When processing video files, the parameter of the function cv2.VideoCapture.open() is the file name, and its grammatical format is:

retval = cv2.VideoCapture.open( filename )

3. Capture frame

After the camera is initialized successfully, frame information can be captured from the camera. Capture frame is used by 函数cv2.VideoCapture.read(). The syntax of this function is:

retval, image=cv2.VideoCapture.read()

In the formula:

  • image is the returned captured frame, or null if no frame was captured.
  • retval indicates whether the capture is successful, if successful, the value is True, otherwise it is False.

4. Release
When the camera is not needed, turn off the camera. Turn off the camera using the function

cv2.VideoCapture.release()。

The syntax of this function is:

None=cv2.VideoCapture.release()

For example, there is currently an object cap of the VideoCapture class, to release it, you can use the statement:

cap.release()

5 Property setting
Sometimes, we need to obtain the properties of cv2.VideoCapture class object, or change the properties of this class object. The function cv2.VideoCapture.get() is used to obtain the properties of the cv2.VideoCapture class object. The syntax format of this function is:

retval = cv2.VideoCapture.get( propId )

In the formula, the parameter propId corresponds to the property of the cv2.VideoCapture class object.
For example, if there is a cv2.VideoCapture class object cvc, then:

  • Through cvc.get(cv2.CAP_PROP_FRAME_WIDTH), the width of the current frame object can be obtained.
  • Through cvc.get(cv2.CAP_PROP_FRAME_HEIGHT), the height of the current frame object can be obtained.

The function cv2.VideoCapture.set() is used to set the properties of the cv2.VideoCapture class object. The syntax of this function is:

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

In the formula, propId corresponds to the attribute of the cv2.VideoCapture class object, and value corresponds to the value of the attribute propid.
For example, if there is a cv2.VideoCapture class object cvc, then:

  • The statement ret = cvc.set(cv2.CAP_PROP_FRAME_WIDTH, 640) sets the width of the current frame object to
    640 pixels.
  • The statement ret = cvc.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) sets the height of the current frame object to 480 pixels.

The property values ​​and meanings of the cv2.VideoCapture class object are shown in Table 18-1.

insert image description here
insert image description here

5. cv2.VideoCapture.grab() function cv2.VideoCapture.retrieve() function

In general, if you need to read the video data of a camera, the easiest way is to use the function
cv2.VideoCapture.read(). However, if you need to synchronize the video data of a group or a multihead camera (such as a stereo camera or Kinect), this function will not be able to do it. The function cv2.VideoCapture.read() can
be understood as being composed of the function cv2.VideoCapture.grab() and the function cv2.VideoCapture.retrieve(). The function
cv2.VideoCapture.grab() is used to point to the next frame, and the function cv2.VideoCapture.retrieve() is used to decode and return a frame.

Therefore, you can use the functions cv2.VideoCapture.grab() and function cv2.VideoCapture.retrieve() to obtain data from multiple cameras.

The function cv2.VideoCapture.grab() is used to point to the next frame, and its syntax format is:

retval= cv2.VideoCapture.grab( )

If the function successfully points to the next frame, the return value retval is True.
The function cv2.VideoCapture.retrieve() is used to decode and return the video frame captured by the function v2.VideoCapture.grab().

The syntax of this function is:

retval, image = cv2.VideoCapture.retrieve( )

In the formula:

  • image is the returned video frame, or an empty image if unsuccessful.
  • retval is a Boolean value, if unsuccessful, returns False; otherwise, returns True.

For a group of cameras, the following code can be used to capture video frames from different cameras:

success0 = cameraCapture0.grab()
success1 = cameraCapture1.grab()
if success0 and success1:
frame0 = cameraCapture0.retrieve()
frame1 = cameraCapture1.retrieve()
与 VideoCapture 类内的其他函数一样,cv2.VideoCapture.grab()和 cv2.VideoCapture.retrieve()

Can also be used to read video files.

Example: Show videos of hot guys or hot girls

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(cap.isOpened()):
 ret, frame = cap.read()
 cv2.imshow('frame',frame)
 c = cv2.waitKey(1)
 if c==27: #ESC 键
    break
cap.release()
cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/hai411741962/article/details/132339794