c++ opencv loop reads camera data and displays it

The following is a sample code that uses the OpenCV library to read and display camera data cyclically in C++:

```

include <opencv2/opencv.hpp>

using namespace cv;

int main() { // Open the default camera VideoCapture cap(0); if (!cap.isOpened()) { std::cout << "Unable to open the camera!" << std::endl; return -1; }

while (true)
{
    // 读取摄像头数据
    Mat frame;
    cap >> frame;

    // 显示图像
    imshow("摄像头", frame);

    // 等待按键,退出循环
    if (waitKey(30) >= 0)
        break;
}

return 0;

} ```

This code first opens the default camera (usually camera number 0), and returns -1 if it cannot be opened. Then in an infinite loop, the camera data is continuously read and images are displayed until the user presses any key to exit the loop. In each loop, the program calls the waitKey function to wait 30 milliseconds to allow the display window to respond to other events.

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/130429903