application_trace.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* OpenCV Application Tracing support demo. */
  2. #include <iostream>
  3. #include <opencv2/core.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <opencv2/highgui.hpp>
  6. #include <opencv2/videoio.hpp>
  7. #include <opencv2/core/utils/trace.hpp>
  8. using namespace cv;
  9. using namespace std;
  10. static void process_frame(const cv::UMat& frame)
  11. {
  12. CV_TRACE_FUNCTION(); // OpenCV Trace macro for function
  13. imshow("Live", frame);
  14. UMat gray, processed;
  15. cv::cvtColor(frame, gray, COLOR_BGR2GRAY);
  16. Canny(gray, processed, 32, 64, 3);
  17. imshow("Processed", processed);
  18. }
  19. int main(int argc, char** argv)
  20. {
  21. CV_TRACE_FUNCTION();
  22. cv::CommandLineParser parser(argc, argv,
  23. "{help h ? | | help message}"
  24. "{n | 100 | number of frames to process }"
  25. "{@video | 0 | video filename or cameraID }"
  26. );
  27. if (parser.has("help"))
  28. {
  29. parser.printMessage();
  30. return 0;
  31. }
  32. VideoCapture capture;
  33. std::string video = parser.get<string>("@video");
  34. if (video.size() == 1 && isdigit(video[0]))
  35. capture.open(parser.get<int>("@video"));
  36. else
  37. capture.open(samples::findFileOrKeep(video)); // keep GStreamer pipelines
  38. int nframes = 0;
  39. if (capture.isOpened())
  40. {
  41. nframes = (int)capture.get(CAP_PROP_FRAME_COUNT);
  42. cout << "Video " << video <<
  43. ": width=" << capture.get(CAP_PROP_FRAME_WIDTH) <<
  44. ", height=" << capture.get(CAP_PROP_FRAME_HEIGHT) <<
  45. ", nframes=" << nframes << endl;
  46. }
  47. else
  48. {
  49. cout << "Could not initialize video capturing...\n";
  50. return -1;
  51. }
  52. int N = parser.get<int>("n");
  53. if (nframes > 0 && N > nframes)
  54. N = nframes;
  55. cout << "Start processing..." << endl
  56. << "Press ESC key to terminate" << endl;
  57. UMat frame;
  58. for (int i = 0; N > 0 ? (i < N) : true; i++)
  59. {
  60. CV_TRACE_REGION("FRAME"); // OpenCV Trace macro for named "scope" region
  61. {
  62. CV_TRACE_REGION("read");
  63. capture.read(frame);
  64. if (frame.empty())
  65. {
  66. cerr << "Can't capture frame: " << i << std::endl;
  67. break;
  68. }
  69. // OpenCV Trace macro for NEXT named region in the same C++ scope
  70. // Previous "read" region will be marked complete on this line.
  71. // Use this to eliminate unnecessary curly braces.
  72. CV_TRACE_REGION_NEXT("process");
  73. process_frame(frame);
  74. CV_TRACE_REGION_NEXT("delay");
  75. if (waitKey(1) == 27/*ESC*/)
  76. break;
  77. }
  78. }
  79. return 0;
  80. }