videocapture_basic.cpp 1.5 KB

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