[opencv+image processing] (Gui Features in OpenCV) 1-1 camera: capture camera video, read video frames, record video

The total code library address of this column The code path https://github.com/xiawei20161308104/xv_opencv_tutorials
of this section

  1. xv_opencv_tutorials/VideoRelated/get_started_with_videos.py
  2. xv_opencv_tutorials/VideoRelated/get_and_set_video.py
  3. xv_opencv_tutorials/VideoRelated/save_video.py

Refer to the official websitehttps://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html

0. The new opencv functions involved in this section

  • From the device, it can be a camera or a local video, the function of getting video cv.VideoCapture()❗️This function is to create an VideoCaptureor VideoWriterobject, the real reading and writing is to call read and write through the created object. Video can be in different formats, such as AVI, MP4, MOV, etc. These formats define the encoding method, resolution, frame rate and other parameters of the video file.
  • video write functioncv.VideoWriter()
  • read videocv.VideoCapture.read()
  • write videocv.VideoWriter.write(frame)
  • Open, determine whether to openisOpened () open()
  • Get parameters, change parameterscv.VideoCapture.get(propId) cv.VideoCapture.set(propId, value)
  • release videocv.VideoCapture.release()

1. What is video

1️⃣ What is a frame, what is the speed of the frame, and what affects it

The basic component of a video is a frame , a series of still images that are played in a continuous manner at a certain rate to form a dynamic image, such as 30 frames per second. The number of frames per second of a video is called the frame rate (Frame Rate), usually expressed in "fps" (Frames Per Second). For example, 30fps means that the video contains 30 frames per second . The frame rate determines the smoothness and realism of the video. Higher frame rates can make videos appear smoother because they refresh images more quickly. For example, a 60fps video will look smoother than a 30fps video. In addition, higher frame rates also reduce blur and judder in videos because they better capture motion. But a higher frame rate will result in larger files and more complex codecs because more frames need to be processed.

Overall, choosing the right frame rate can improve your video viewing experience and ensure the best possible visuals. Usually, movies and TV shows have a frame rate of 24fps or 30fps, while games and animations usually have a frame rate of 60fps, and now generally higher 120fps are common.

2️⃣What is a frame, what is a video resolution, and what is the resolution related to

Each frame is made up of pixels, the smallest unit in an image that can contain color and brightness information. Video resolution refers to the number of pixels per frame in a video , usually expressed in horizontal and vertical pixels. For example, a video with a resolution of 1920x1080 means that each frame has 1920 horizontal pixels and 1080 vertical pixels. Resolution depends on capture and playback devices.
Resolution of the camera: The resolution of the camera determines the number of pixels in each frame of the video. High-resolution cameras capture more pixels, resulting in higher-resolution video.
Resolution of the playback device: The resolution of the playback device determines the final resolution of the video. If the resolution of the video is higher than that of the playback device, the video will be scaled down to fit the playback device, which may result in distorted and blurry images.

2. Get video from camera

1️⃣Steps

  1. Create an VideoCaptureobject first, and its parameter is the device index or the name of the video file. The device index is only used to specify the number of the camera. Usually, write 0 to represent the default camera of the notebook. If you have an external camera, you need to pass 1 to select the second cameras, and so on.
  2. After that, you can 逐帧获取image.
  3. The last thing 释放资源is to create the VideoCapture object.

2️⃣ code

git code address

import cv2 as cv

# 创建VideoCapture对象,用创建的对象去做之后的操作
cap = cv.VideoCapture(0)
# 检测有无摄像头正常使用
# 这是一个必要的验证,当cap为空的时候,后续调用会报错。
if not cap.isOpened():
    print("Cannot open camera")
    exit()

# 获取视频流是一个连续的循环过程,一直在获取,不是说获取到一帧就可以了
while True:
    # 通过创建的VideoCapture对象逐帧获取视频,会返回两个参数,ret返回true和false代表是否正常获取到帧,以及视频是否结束
    # frame代表获取到的帧
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("不能正常读取视频帧")
        break
    # 将获取到的视频帧,也就是一幅幅图像,转为灰度图
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # 展示
    cv.imshow('frame', gray)
    # q退出获取视频流
    if cv.waitKey(1) == ord('q'):
        break
# 释放资源
cap.release()
cv.destroyAllWindows()

3️⃣Effect

insert image description here

4️⃣ Some related overloaded functions

  1. VideoCaptureThe parameter can choose the file path, for example:cv2.VideoCapture('test_video.mp4')
  2. isOpened()Determine whether it is open, cv.VideoCapture.openused to open a video stream, for example: cv.VideoCapture.open('test_video.mp4'), if the device or video is opened, it will returntrue

3. Obtain the parameters of the video device through opencv and set new parameters

1️⃣ Obtain device parameters

cv.VideoCapture.get( propId )To obtain device parameters, propld fills in the parameter options provided by cv2, and the official website provides 70 kinds of parameters that can be queried. The return value is the value of the changed parameter. If the query parameter does not exist or is not supported, no error will be reported , and 0 will be returned.
Here are just a few common ones. There are also some devices that need to support the brightness, saturation, etc.

2️⃣Change device parameters

cv.VideoCapture.set( propId, value )Changing device parameters from propId to value may be invalid even if it returns true. Whether it is valid depends on the device hardware, driver and API backend.

propld parameter value return value
cv.CAP_PROP_POS_MSEC Returns the current position of the video file in milliseconds.
cv.CAP_PROP_FRAME_WIDTH Returns the width of the frames in the video stream.
cv.CAP_PROP_FRAME_HEIGHT Returns the height of a frame in the video stream.
cv.CAP_PROP_FPS Returns the video frame rate.
cv.CAP_PROP_FRAME_COUNT Returns the number of frames in the video file.
cv.CAP_PROP_POS_MSEC Queries the current position of the video file in milliseconds.

3️⃣ code

git warehouse address

import cv2 as cv

# 创建VideoCapture对象,用创建的对象去做之后的操作
cap2 = cv.VideoCapture(0)

while True:
    # 获取设备参数,cv.CAP_PROP_FRAME_WIDTH,CAP_PROP_FRAME_HEIGHT是cv2提供的参数选项
    width, height = cap2.get(cv.CAP_PROP_FRAME_WIDTH), cap2.get(cv.CAP_PROP_FRAME_HEIGHT)
    # 我这里是1280.0 720.0
    print(width, height)

    # 以原分辨率的两倍来捕获
    cap2.set(cv.CAP_PROP_FRAME_WIDTH, width * 2)
    cap2.set(cv.CAP_PROP_FRAME_HEIGHT, height * 2)
    _, frame2 = cap2.read()

    gray_double = cv.cvtColor(frame2, cv.COLOR_BGR2GRAY)
    # 展示
    cv.imshow('doubel read', gray_double)
    # q退出获取视频流
    if cv.waitKey(1) == ord('q'):
        break
# 释放资源
cap2.release()
cv.destroyAllWindows()

  • Effect:

insert image description here
Obviously, the length has doubled and the width has not changed, because whether it actually works depends on the hardware

4. Record video and save

1️⃣ steps

  1. Define the encoding method and create a VideoWriter object. There are five parameters. VideoWriter (const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true) are:
    • The output file name, such as 'output.avi'
    • Encoding method FourCC code
    • Frame rate FPS
    • The resolution size to save
    • The last one is the isColor flag. If it is "True", the encoder expects color frames, otherwise grayscale frames are used.
  2. Define a VideoCapture
  3. Execute the write function to write the content captured by VideoCapture to the specified location according to the set method

2️⃣ Code

git warehouse address

import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)
# 定义编码方式创建VideoWriter对象
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('../imgs/output_video.avi', fourcc, 20.0, (640, 480))

# 当cap打开状态执行
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    # 写入文件
    out.write(frame)
    cv.imshow('frame', frame)
    if cv.waitKey(1) == ord('q'):
        break
# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

3️⃣ Effect

insert image description here

4️⃣ Supplementary video encoding method

视频编码Is the process of converting video signals into digital signals for storage and transmission on digital media devices.
Common encoding methods are

H.264 : H.264 is a widely used video coding standard, also known as AVC (Advanced Video Coding). It supports high-quality video compression and can provide high-definition video and streaming media.
H.265 : H.265, also known as HEVC (High Efficiency Video Coding), is a new video coding standard that provides higher compression ratio and better image quality than H.264.
MPEG-2 : MPEG-2 is a widely used video encoding standard for high-definition video such as digital television, DVD, and Blu-ray Disc.
MPEG-4 : MPEG-4 is a coding standard that supports video, audio, and multimedia content, and is also used for streaming and web video.
VP9 : VP9 is a video coding standard developed by Google that offers better compression efficiency and image quality than H.264, and supports 4K and 8K video.
AV1 : AV1 is a next-generation open video coding standard developed by the Alliance for Open Media, which aims to provide higher compression efficiency and better image quality.

FourCCis a four-character code used to identify video codecs, usually consisting of four capital letters, used to identify video decoders and encoders. For example, the FourCC code of the MPEG-4 video format is "MP4V", and the FourCC code of the H.264 video format is "H264". In the video editing and processing software of FourCC code 好处是, the correct codec can be selected, so as to ensure that the video file can be played and edited correctly, and interact between different platforms and applications.

Guess you like

Origin blog.csdn.net/cvxiayixiao/article/details/130519349