Qt example of using opencv to operate the camera

Overview

This is an example of Qt using opencv lib to operate the camera

detailed

1. Introduction

    This is an example of Qt using opencv lib to operate the camera

2. Code implementation process

    Create a new VideoCapture object, then read the Mat image data through this object, and then convert the Mat data into a QImage and display it on the QLabel.

VideoCapture not only supports reading from video files (.avi, .mpg format), but also supports reading directly from cameras (such as the computer's own camera). To obtain a video, you need to create a VideoCapture object first. There are three ways to create a VideoCapture object:  cop

1. Read the video from the file (.MPG or .AVI format). After the object is created, OpenCV will open the file and prepare to read it. If the opening is successful, we can start reading the frames of the video, and The member function isOpened() of cv::VideoCapture will return true (it is recommended to use this member function to determine whether the opening is successful when opening a video or camera).

cv::VideoCapture capture(const string& filename);  // 从视频文件读取 

cv::VideoCapture capture("C:/Users/DADA/DATA/gogo.avi");  // 从视频文件读取

2. Read the video from the camera. In this case, we will give an identifier to represent the camera we want to access and its handshake method with the operating system. For cameras, this identifier is a number - if there is only 1 camera, it is 0, if there are multiple cameras in the system, just increase it upwards. The other part of the identifier is the camera domain, which is used to indicate the type of camera. The value of this domain can be any of the following predefined constants.

cv::VideoCapture capture(int device );  //视频捕捉设备 id ---笔记本电脑的用0表示

 When creating a video capture object this way, the identifier we pass is the sum of the domain index and the camera index. For example:

cv::VideoCapture capture(cv::CAP_IEEE1394 + 1);

       In this example VideoCapture will try to open the second (numbered starting from 0) 1394 camera. In most cases, since we only have one camera, there is no need to specify the camera's domain. At this time, using cv::CAP_ANY is an efficient way (that is, 0, so there is no need to specify it specifically).

3. First create a capture object, and then set the open information through the member function open(). The operation is as follows.

cv::VideoCapture VideoCapture;  这里的第二个VideoCapture是一个对象名
VideoCapture.open( "C:/Users/DADA/DATA/gogo.avi" );

There are two ways to read video frames into the cv::Mat matrix: one is the read() operation; the other is the ">>" operation.

cv::Mat frame;  
cap.read(frame); //读取方式一  
cap >> frame; //读取方式二

This time we are using method 2

void MainWindow::openCamara()
{
    m_capture.open(0);
 
    m_timer->start(50);
 
}
 
void MainWindow::closeCamer()
{
    m_timer->stop();
 
    m_capture.release();
}

This project will use the lib compiled by opencv. You can download the source code and compile it yourself.

    opencv3.4.5 source code link https://opencv.org/releases.html

    

3.png

Guess you like

Origin blog.csdn.net/hanjiepo/article/details/133221347