frame_rate.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "opencv2/opencv.hpp"
  2. #include <time.h>
  3. using namespace cv;
  4. using namespace std;
  5. int main(int argc, char** argv)
  6. {
  7. // Start default camera
  8. VideoCapture video(0);
  9. // With webcam get(CV_CAP_PROP_FPS) does not work.
  10. // Let's see for ourselves.
  11. double fps = video.get(cv::CAP_PROP_FPS);
  12. // If you do not care about backward compatibility
  13. // You can use the following instead for OpenCV 3
  14. // double fps = video.get(CAP_PROP_FPS);
  15. cout << "Frames per second using video.get(CV_CAP_PROP_FPS) : " << fps << endl;
  16. // Number of frames to capture
  17. int num_frames = 120;
  18. // Start and end times
  19. time_t start, end;
  20. // Variable for storing video frames
  21. Mat frame;
  22. cout << "Capturing " << num_frames << " frames" << endl ;
  23. // Start time
  24. time(&start);
  25. // Grab a few frames
  26. for(int i = 0; i < num_frames; i++)
  27. {
  28. video >> frame;
  29. }
  30. // End Time
  31. time(&end);
  32. // Time elapsed
  33. double seconds = difftime (end, start);
  34. cout << "Time taken : " << seconds << " seconds" << endl;
  35. // Calculate frames per second
  36. fps = num_frames / seconds;
  37. cout << "Estimated frames per second : " << fps << endl;
  38. // Release video
  39. video.release();
  40. return 0;
  41. }