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