2015-05-22 19:12:00 +08:00
|
|
|
/*
|
|
|
|
// Sample demonstrating interoperability of OpenCV UMat with Direct X surface
|
|
|
|
// Base class for Direct X application
|
|
|
|
*/
|
2015-05-16 05:40:05 +08:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include "opencv2/core/directx.hpp"
|
|
|
|
#include "opencv2/core/ocl.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#include "opencv2/videoio.hpp"
|
|
|
|
|
|
|
|
#include "winapp.hpp"
|
|
|
|
|
|
|
|
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
|
|
|
|
|
|
|
|
|
2015-07-14 04:40:45 +08:00
|
|
|
class Timer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Timer() : m_t0(0), m_t1(0)
|
|
|
|
{
|
|
|
|
m_tick_frequency = (float)cv::getTickFrequency();
|
|
|
|
}
|
|
|
|
|
|
|
|
void start()
|
|
|
|
{
|
|
|
|
m_t0 = cv::getTickCount();
|
|
|
|
time_queue.push(m_t0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
if (time_queue.size() > 1)
|
|
|
|
m_t1 = time_queue.front();
|
|
|
|
|
|
|
|
if (time_queue.size() >= 25)
|
|
|
|
time_queue.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
float fps()
|
|
|
|
{
|
|
|
|
size_t sz = time_queue.size();
|
|
|
|
|
|
|
|
float fps = sz * m_tick_frequency / (m_t0 - m_t1);
|
|
|
|
|
|
|
|
return fps;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
float m_tick_frequency;
|
|
|
|
int64 m_t0;
|
|
|
|
int64 m_t1;
|
|
|
|
std::queue<int64> time_queue;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-16 05:40:05 +08:00
|
|
|
class D3DSample : public WinApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum MODE
|
|
|
|
{
|
|
|
|
MODE_CPU,
|
|
|
|
MODE_GPU
|
|
|
|
};
|
|
|
|
|
|
|
|
D3DSample(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
|
|
|
|
WinApp(width, height, window_name)
|
|
|
|
{
|
|
|
|
m_shutdown = false;
|
2015-07-10 03:41:05 +08:00
|
|
|
m_mode = MODE_CPU;
|
|
|
|
m_modeStr[0] = cv::String("Processing on CPU");
|
|
|
|
m_modeStr[1] = cv::String("Processing on GPU");
|
|
|
|
m_demo_processing = false;
|
2015-05-16 05:40:05 +08:00
|
|
|
m_cap = cap;
|
|
|
|
}
|
|
|
|
|
|
|
|
~D3DSample() {}
|
|
|
|
|
|
|
|
virtual int create() { return WinApp::create(); }
|
|
|
|
virtual int render() = 0;
|
|
|
|
virtual int cleanup()
|
|
|
|
{
|
|
|
|
m_shutdown = true;
|
|
|
|
return WinApp::cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
|
|
|
case WM_CHAR:
|
2015-07-10 03:41:05 +08:00
|
|
|
if (wParam == '1')
|
2015-05-16 05:40:05 +08:00
|
|
|
{
|
2015-07-10 03:41:05 +08:00
|
|
|
m_mode = MODE_CPU;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (wParam == '2')
|
|
|
|
{
|
|
|
|
m_mode = MODE_GPU;
|
2015-05-16 05:40:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (wParam == VK_SPACE)
|
|
|
|
{
|
2015-07-10 03:41:05 +08:00
|
|
|
m_demo_processing = !m_demo_processing;
|
2015-05-16 05:40:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (wParam == VK_ESCAPE)
|
|
|
|
{
|
|
|
|
return cleanup();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_CLOSE:
|
|
|
|
return cleanup();
|
|
|
|
|
|
|
|
case WM_DESTROY:
|
|
|
|
::PostQuitMessage(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
// do render at idle
|
|
|
|
virtual int idle() { return render(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool m_shutdown;
|
2015-07-10 03:41:05 +08:00
|
|
|
bool m_demo_processing;
|
2015-05-16 05:40:05 +08:00
|
|
|
MODE m_mode;
|
2015-07-10 03:41:05 +08:00
|
|
|
cv::String m_modeStr[2];
|
2015-05-16 05:40:05 +08:00
|
|
|
cv::VideoCapture m_cap;
|
|
|
|
cv::Mat m_frame_bgr;
|
|
|
|
cv::Mat m_frame_rgba;
|
2015-07-14 04:40:45 +08:00
|
|
|
Timer m_timer;
|
2015-05-16 05:40:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-05-19 21:08:25 +08:00
|
|
|
static void help()
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"\nSample demonstrating interoperability of DirectX and OpenCL with OpenCV.\n"
|
|
|
|
"Hot keys: \n"
|
2015-07-10 03:41:05 +08:00
|
|
|
" SPACE - turn processing on/off\n"
|
|
|
|
" 1 - process DX surface through OpenCV on CPU\n"
|
|
|
|
" 2 - process DX surface through OpenCV on GPU (via OpenCL)\n"
|
|
|
|
" ESC - exit\n\n");
|
2015-05-19 21:08:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char* keys =
|
|
|
|
{
|
|
|
|
"{c camera | true | use camera or not}"
|
|
|
|
"{f file | | movie file name }"
|
|
|
|
"{h help | false | print help info }"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename TApp>
|
|
|
|
int d3d_app(int argc, char** argv, std::string& title)
|
|
|
|
{
|
2015-05-16 05:40:05 +08:00
|
|
|
cv::CommandLineParser parser(argc, argv, keys); \
|
|
|
|
bool useCamera = parser.has("camera"); \
|
|
|
|
string file = parser.get<string>("file"); \
|
|
|
|
bool showHelp = parser.get<bool>("help"); \
|
2015-05-19 21:08:25 +08:00
|
|
|
|
|
|
|
if (showHelp)
|
|
|
|
help();
|
|
|
|
|
|
|
|
parser.printMessage();
|
|
|
|
|
|
|
|
cv::VideoCapture cap;
|
|
|
|
|
|
|
|
if (useCamera)
|
|
|
|
cap.open(0);
|
|
|
|
else
|
|
|
|
cap.open(file.c_str());
|
|
|
|
|
|
|
|
if (!cap.isOpened())
|
|
|
|
{
|
|
|
|
printf("can not open camera or video file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = (int)cap.get(CAP_PROP_FRAME_WIDTH);
|
|
|
|
int height = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
|
|
|
|
|
|
|
|
std::string wndname = title;
|
|
|
|
|
|
|
|
TApp app(width, height, wndname, cap);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
app.create();
|
|
|
|
return app.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (cv::Exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << "FATAL ERROR: Unknown exception" << std::endl;
|
|
|
|
return 11;
|
|
|
|
}
|
2015-05-16 05:40:05 +08:00
|
|
|
}
|