· 6 years ago · Jan 24, 2020, 09:50 PM
1// From OpenCV Documents Online (2020)
2
3#include <opencv2/core.hpp>
4#include <opencv2/videoio.hpp>
5#include <opencv2/highgui.hpp>
6#include <iostream>
7#include <stdio.h>
8
9using namespace cv;
10using namespace std;
11
12int main(int, char**)
13{
14 Mat frame;
15 //--- INITIALIZE VIDEOCAPTURE
16 VideoCapture cap;
17 // open the default camera using default API
18 // cap.open(0);
19 // OR advance usage: select any API backend
20 int deviceID = 0; // 0 = open default camera
21 int apiID = cv::CAP_ANY; // 0 = autodetect default API
22 // open selected camera using selected API
23 cap.open(deviceID + apiID);
24 // check if we succeeded
25 if (!cap.isOpened()) {
26 cerr << "ERROR! Unable to open camera\n";
27 return -1;
28 }
29 //--- GRAB AND WRITE LOOP
30 cout << "Start grabbing" << endl
31 << "Press any key to terminate" << endl;
32 for (;;)
33 {
34 // wait for a new frame from camera and store it into 'frame'
35 cap.read(frame);
36 // check if we succeeded
37 if (frame.empty()) {
38 cerr << "ERROR! blank frame grabbed\n";
39 break;
40 }
41 // show live and wait for a key with timeout long enough to show images
42 imshow("Live", frame);
43 if (waitKey(5) >= 0)
44 break;
45 }
46 // the camera will be deinitialized automatically in VideoCapture destructor
47 return 0;
48}