video

Video is a very important source of visual information. It is a kind of signal that is often processed in visual processing. In fact, a video is made up of a series of images called frames, which are taken from the video at fixed intervals. The rate at which a frame is obtained is called the frame rate, which is usually expressed in “frame rate per second” to represent the number of frames that occur in one second, or FPS as it is known to game fans.

If we extract independent frames from the video, we can use the method of image processing to process them, so as to achieve the purpose of video processing.

VideoCapture class

In OpenCV, it gives us the cv2.videocapture class to handle video.

Usually we use VideoCapture class by following several steps:

Initialize the

The VideoCapture class provides a constructor, cv2.videocapture (), to open and initialize the camera. Its constructor is defined as follows:

VideoCapture(camera ID)Copy the code

Camera ID The default value is -1, indicating that a random camera is selected. If you’re running on a device that has multiple cameras, use 0 for the device’s first camera, 1 for the device’s second camera, and so on. Of course, most desktops only have one camera, so you can use either 0 or -1. For example, the following code will do:

video=cv2.VideoCapture(0)
video=cv2.VideoCapture(-1)
Copy the code

Check whether the camera is initialized successfully

For the sake of program robustness, we usually check whether the camera is initialized successfully after we get the camera initialization.

Check whether the camera is initialization function of success for the cv2. VideoCapture. IsOpened (), the full are defined as follows:

result=cv2.VideoCapture.isOpened()
Copy the code

Result returns True on success or False on failure. When we fail to initialize the camera, we can also use the function cv2.videocapture. Open () to open the camera. The specific code is as follows:

video=cv2.VideoCapture(0)
result=cv2.VideoCapture.isOpened()
if result is False:
    result=cv2.VideoCapture.open(0)
Copy the code

The open argument is also the ID number of the camera, as in the previous constructor. The open() function also returns True for success or False for failure when the camera is turned on.

However, the cv2.videocapture.open () function can also open the video file directly. Such as:

result=cv2.VideoCapture.open(filename)
Copy the code

To capture the frame

Once the camera is initialized, we can capture frames from the camera.

In OpenCV, it gives us the frame capture function cv2.videocapture.read(). Its full definition is as follows:

retval,image=cv2.VideoCapture.read()
Copy the code

Retval indicates whether the capture is successful and returns the Boolean type. Image returns information about the captured frame (that is, the image). If no frame information is captured, this value is null.

The release of

After we capture a frame, or use a play camera resource, we need to release that resource, which means turn off the camera.

In OpenCV, it provides the function cv2.videocapture.release() to turn off the camera, which is used as follows:

cv2.VideoCapture.release()
Copy the code

These are the basic steps for OpenCV to capture information using the camera.

Attribute set

Sometimes we need to get some properties of the cv2.Videocapture camera, such as its resolution, pixels and so on. We need the cv2.videocapture. Get () function provided to us by OpenCV. Its full definition is as follows:

cameraInfo=cv2.VideoCapture.get(propId)
Copy the code

The propId parameter corresponds to the property of the cv2.videocapture class object. For example, to get the width of the camera image, the code looks like this:

cap=cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
Copy the code

Since we can get the width, we can also get the height, brightness and other information of the camera image, including saturation, contrast and so on. For more information, consult the development documentation, which will not be covered here.

Grab () and retrieve ()

In most cases, we only use one camera, and these steps and functions are perfectly fine. However, if you need to synchronize a set or a multi-headed camera, the above function is not adequate.

This time, we will need to use the cv2. VideoCapture. Grab () and cv2. VideoCapture. Retrieve () function. You can think of the function cv2.videocapture.read () as a combination of these two functions.

Function cv2. VideoCapture. Grab () is used to point to the next frame and function cv2. VideoCapture. Retrieve () is used to decode and returns a frame. So, for a set of cameras we can do this:

cap0=cv2.VideoCapture(0)
cap1=cv2.VideoCapture(0)
isCamera0=cap0.grab()
isCamera1=cap1.grab()
if isCamera0 and isCamera1:
	frame0=cap0.retrieve()
	frame1=cap1.retrieve()
Copy the code

Of course, these two functions can also read video files.

Capture camera video

Now that we know about the VideoCapture class. Next, let’s capture the video from the computer camera. The complete code is as follows:

import cv2

cap=cv2.VideoCapture(0)
while(cap.isOpened()):
    ret,frame=cap.read()
    cv2.imshow('video',frame)
    c=cv2.waitKey(1)
    if c==27:
        break
cap.release()
cv2.destroyAllWindows()
Copy the code

The code is simple. It first gets the computer’s camera and then wirelessly loops to see if it’s on. In the body of the loop, we read each frame and display it on the screen, and when the ESC key is pressed, exit.

After running, the effect is as follows:

Capture file video

As we’ve shown before, if you want to play a video file, you just change the camera name to the file name.

Next, let’s implement the video file, the code is as follows:

import cv2

cap=cv2.VideoCapture("38.mp4")
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
while(cap.isOpened()):
    ret,frame=cap.read()
    cv2.imshow('video',frame)
    c=cv2.waitKey(1)
    if c==27:
        break
cap.release()
cv2.destroyAllWindows()
Copy the code

Here, it does not run, interested can be “38.MP4” video to replace their own.

Achieve grayscale camera

With the two examples above, we know how to use the camera. However, generally when we use mobile phones, there are all kinds of special effects, such as beauty, black and white effect. So how do we achieve these effects?

Below, we take the grayscale image as an example to capture the video recorded by the camera as grayscale image. The specific code is as follows:

import cv2

cap=cv2.VideoCapture(0)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
while(cap.isOpened()):
    ret,frame=cap.read()
    frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    cv2.imshow('video',frame)
    c=cv2.waitKey(1)
    if c==27:
        break
cap.release()
cv2.destroyAllWindows()
Copy the code

After running, the video on our camera is gray. From here, we can also see that when processing the video signal, we only need to obtain the image Frame of each Frame and then process the Frame.