代码:

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;
using namespace std;


int main(int argc, char* argv[])
{
    cout<<"OpenCV4 Example."<<endl;
    cout<<"OpenCV Version = "<<getVersionString()<<endl;
    webcam();
    
    return 0;
}

void webcam()
{
    Mat frame;
    VideoCapture cap;
    int deviceID = 0;             // 0 = open default camera
    int apiID = cv::CAP_ANY;      // 0 = autodetect default API
    cap.open(deviceID, apiID);
    if (!cap.isOpened()) {
        cerr << "ERROR! Unable to open camera\n";
        return;
    }
    cout << "Start grabbing" << endl << "Press any key to terminate" << endl;
    for (;;)
    {
        cap.read(frame);
        if (frame.empty()) {
            cerr << "ERROR! blank frame grabbed\n";
            break;
        }
        imshow("Live", frame);
        if (waitKey(40) >= 0)
            break;
    }
}

运行结果:

./opencv-demo 
OpenCV4 Example.
OpenCV Version = 4.5.2
Start grabbing
Press any key to terminate

运行效果:
opencv-webcam-capture.png

本文链接地址:https://const.net.cn/138.html

标签: none

添加新评论