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