d3dsample.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. // Sample demonstrating interoperability of OpenCV UMat with Direct X surface
  3. // Base class for Direct X application
  4. */
  5. #include <string>
  6. #include <iostream>
  7. #include <queue>
  8. #include "opencv2/core.hpp"
  9. #include "opencv2/core/directx.hpp"
  10. #include "opencv2/core/ocl.hpp"
  11. #include "opencv2/imgproc.hpp"
  12. #include "opencv2/videoio.hpp"
  13. #include "winapp.hpp"
  14. #define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
  15. class D3DSample : public WinApp
  16. {
  17. public:
  18. enum MODE
  19. {
  20. MODE_CPU,
  21. MODE_GPU_RGBA,
  22. MODE_GPU_NV12
  23. };
  24. D3DSample(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
  25. WinApp(width, height, window_name)
  26. {
  27. m_shutdown = false;
  28. m_mode = MODE_CPU;
  29. m_modeStr[0] = cv::String("Processing on CPU");
  30. m_modeStr[1] = cv::String("Processing on GPU RGBA");
  31. m_modeStr[2] = cv::String("Processing on GPU NV12");
  32. m_demo_processing = false;
  33. m_cap = cap;
  34. }
  35. ~D3DSample() {}
  36. virtual int create() { return WinApp::create(); }
  37. virtual int render() = 0;
  38. virtual int cleanup()
  39. {
  40. m_shutdown = true;
  41. return WinApp::cleanup();
  42. }
  43. protected:
  44. virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  45. {
  46. switch (message)
  47. {
  48. case WM_CHAR:
  49. if (wParam == '1')
  50. {
  51. m_mode = MODE_CPU;
  52. return EXIT_SUCCESS;
  53. }
  54. if (wParam == '2')
  55. {
  56. m_mode = MODE_GPU_RGBA;
  57. return EXIT_SUCCESS;
  58. }
  59. if (wParam == '3')
  60. {
  61. m_mode = MODE_GPU_NV12;
  62. return EXIT_SUCCESS;
  63. }
  64. else if (wParam == VK_SPACE)
  65. {
  66. m_demo_processing = !m_demo_processing;
  67. return EXIT_SUCCESS;
  68. }
  69. else if (wParam == VK_ESCAPE)
  70. {
  71. return cleanup();
  72. }
  73. break;
  74. case WM_CLOSE:
  75. return cleanup();
  76. case WM_DESTROY:
  77. ::PostQuitMessage(0);
  78. return EXIT_SUCCESS;
  79. }
  80. return ::DefWindowProc(hWnd, message, wParam, lParam);
  81. }
  82. // do render at idle
  83. virtual int idle() { return render(); }
  84. protected:
  85. bool m_shutdown;
  86. bool m_demo_processing;
  87. MODE m_mode;
  88. cv::String m_modeStr[3];
  89. cv::VideoCapture m_cap;
  90. cv::Mat m_frame_bgr;
  91. cv::Mat m_frame_rgba;
  92. cv::TickMeter m_timer;
  93. };
  94. static const char* keys =
  95. {
  96. "{c camera | 0 | camera id }"
  97. "{f file | | movie file name }"
  98. };
  99. template <typename TApp>
  100. int d3d_app(int argc, char** argv, std::string& title)
  101. {
  102. cv::CommandLineParser parser(argc, argv, keys);
  103. std::string file = parser.get<std::string>("file");
  104. int camera_id = parser.get<int>("camera");
  105. parser.about(
  106. "\nA sample program demonstrating interoperability of DirectX and OpenCL with OpenCV.\n\n"
  107. "Hot keys: \n"
  108. " SPACE - turn processing on/off\n"
  109. " 1 - process DX surface through OpenCV on CPU\n"
  110. " 2 - process DX RGBA surface through OpenCV on GPU (via OpenCL)\n"
  111. " 3 - process DX NV12 surface through OpenCV on GPU (via OpenCL)\n"
  112. " ESC - exit\n\n");
  113. parser.printMessage();
  114. cv::VideoCapture cap;
  115. if (file.empty())
  116. cap.open(camera_id);
  117. else
  118. cap.open(file.c_str());
  119. if (!cap.isOpened())
  120. {
  121. printf("can not open camera or video file\n");
  122. return EXIT_FAILURE;
  123. }
  124. int width = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);
  125. int height = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);
  126. std::string wndname = title;
  127. TApp app(width, height, wndname, cap);
  128. try
  129. {
  130. app.create();
  131. return app.run();
  132. }
  133. catch (const cv::Exception& e)
  134. {
  135. std::cerr << "Exception: " << e.what() << std::endl;
  136. return 10;
  137. }
  138. catch (...)
  139. {
  140. std::cerr << "FATAL ERROR: Unknown exception" << std::endl;
  141. return 11;
  142. }
  143. }