Reading, displaying and saving camera video in OpenCV Explanation and actual combat (with Python source code)

If you need the source code, please like and follow the collection and leave a private message in the comment area~~~

OpenCV is not only capable of processing images, but also video. A video is constructed from a large number of images, which are taken from the video at regular intervals. In this way, the image processing method can be used to process these images, and then the purpose of processing video can be achieved. To process the video, you need to read, display and save the video and other related operations

1. VideoCapture class

The camera refers to the video read from the camera in real time. In order to read and display the camera video, OpenCV provides related methods of the VideoCapture class. These methods include the initialization method of the camera and the method of checking whether the initialization of the camera is successful. From the camera The method of reading frames and the method of turning off the camera, etc. are explained in turn below.

The VideoCapture class provides the construction method VideoCapture, which is used to complete the initialization of the camera. The syntax format is as follows

capture=cv2.VideoCapture(index)

capture The camera to open

index The device index of the camera

The order of the number of cameras and their device indexes is determined by the operating system, because OpenCV does not provide any method to query the number of cameras and their device indexes

When the index is 0, it means to open the built-in camera, when the index is 1, it means to open the first external camera connected to the computer

 The isOpened method can detect whether the camera is opened successfully

After the opening is successful, the frame can be read from the camera

retval,image=cv2.VideoCapture.read()

retval whether to read the frame

The frame read by image can be understood as an image

In the Opencv documentation manual, it is particularly emphasized that the camera should be turned off when the camera is not needed, and the release method is provided for this purpose

2. Read and display camera video

 Write a program below to open the built-in camera of the notebook to read and display the video in real time. The effect is as follows

During this time, if the space bar is pressed, the camera will be turned off

 Part of the code is as follows

import cv2

capture = cv2(0) # 打开笔记本内置摄像头
while (capture.iened()): # 笔记本内置摄像头被打开后
    retval, image = capture.read() # 从摄像头中实时读取视频
    cv2.imshow("Video", image) # 在窗口中显示读取到的视频
    key = cv2.waitK# 窗口的图像刷新时间为1毫秒
    if key == 32: # 如果按下空格键
        break
capture.release() # 关闭笔记本内置摄像头
cv2.destroyAllWindows() # 销毁显示摄像头视频的窗口

 3. Convert the camera video from color video to grayscale video

The color video that is read and displayed is converted to a grayscale video, and when the space bar is pressed, the camera is turned off

 

 Part of the code is as follows

import cv2

capture = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 打开笔记本内置摄像头
while (capture.()): # 笔记本内置摄像头被打开后
    retval, image capture.read() # 从摄像头中实时读取视频
    # 把彩色视频转灰度视频
    image_Gray = cv2tColor(image,cv2.COLOR_BGR2GRAY)
    if retval == True: # 读取到摄像头视频后
        cv2.imshow("Video", image) # 在窗口中显示彩色视频
        cv2.imshow("Video_Gray", image_Gray) # 在窗口中显示灰度视频
    key = cv2.waitKey(1) # 窗口的图像刷新时间为1毫秒
    if key == 32: # 如果按下空格键
        break
capture.release() # 关闭笔记本内置摄像头
cv2.destroyAllWindows() # 销毁显示摄像头视频的窗口

4. Display and save the image of the camera video at a certain moment

When pressing the space bar, close the camera, save and display the image in the camera video at this time

Part of the code is as follows to save the path can be modified by yourself

import cv2

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 打开笔记本内置摄像头
while (cap.isOpened()): # 笔记本内置摄像头被打开后
    ret, frame = cap.read() # 从摄像头中实时读取视频
    cv2.imshow("Video", frame) # 在窗口中显示视频
    k = cv2(1) # 图像的刷新时间为1毫秒
    if k == 32: # 按下空格键
        cap.release() # 关闭笔记本内置摄像头
        cv2.destroyWindow("Video") # 销毁名为Video的窗口
        cv2.imwte("D:/copy.png", frame) # 保存按下空格键时摄像头视频中的图像
        cv2.ims'img', frame) # 显示按下空格键时摄像头视频中的图像
        cv2.waitKey() # 刷新图像
        break
cv2.destroyAllWindows() # 销毁显示图像的窗口

 5. Read and display 2 camera videos

Write a program below. When the built-in camera of the notebook is turned on to read and display the video in real time, an external camera connected to the notebook is opened. When the space bar is pressed, the two cameras are destroyed at the same time.

Part of the code is as follows

import cv2

cap_Inner = cv2.VideoCure(0, cv2.CAP_DSHOW) # 打开笔记本内置摄像头
cap_ter = cv2.VideoCare(1, cv2.CADSHOW) # 打开一个连接笔记本的外置摄像头
while (cap_Inner.isOpened() & cap_Outer.isOpened()): # 两个摄像头都被打开后
    retval, img_Inner = cap_Inner.read() # 从笔记本内置摄像头中实时读取视频
    ret, img_Outer = cap_Outer.read() # 从连接笔记本的外置摄像头中实时读取视频
    # 在窗口中显示笔记本内置摄像头读取到的视频
    cv2.imshow("Vi_Inner", img_Inner)
    # 在窗口中显示连接笔记本的外置摄像头读取到的视频
    cv2.imshow("Video_Outer", img_Outer)
    key = cv2.waitKey(1) # 窗口的图像刷新时间为1毫秒
    if key == 32: # 如果按下空格键
        brea
cap_Inner.release() # 关闭笔记本内置摄像头
cap_Outerelease( # 关闭连接笔记本的外置摄像头
cv2.deroyAllWdows() # 销毁显示摄像头视频的窗口

It's not easy to create and find it helpful, please like, follow and collect~~~

Guess you like

Origin blog.csdn.net/jiebaoshayebuhui/article/details/128755315