2015-07-06 22:46:18 +08:00
|
|
|
/*
|
|
|
|
// Sample demonstrating interoperability of OpenCV UMat with OpenGL texture.
|
|
|
|
// At first, the data obtained from video file or camera and placed onto
|
|
|
|
// OpenGL texture, following mapping of this OpenGL texture to OpenCV UMat
|
|
|
|
// and call cv::Blur function. The result is mapped back to OpenGL texture
|
|
|
|
// and rendered through OpenGL API.
|
|
|
|
*/
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <windows.h>
|
|
|
|
#elif defined(__linux__)
|
|
|
|
# include <X11/X.h>
|
|
|
|
# include <X11/Xlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <queue>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "opencv2/core.hpp"
|
|
|
|
#include "opencv2/core/opengl.hpp"
|
|
|
|
#include "opencv2/core/ocl.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
|
|
|
#include "opencv2/videoio.hpp"
|
|
|
|
|
|
|
|
#include "winapp.hpp"
|
|
|
|
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
# pragma comment(lib, "opengl32.lib")
|
|
|
|
# pragma comment(lib, "glu32.lib")
|
|
|
|
#endif
|
|
|
|
|
2015-07-06 22:46:18 +08:00
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
class GLWinApp : public WinApp
|
|
|
|
{
|
|
|
|
public:
|
2015-08-01 06:16:06 +08:00
|
|
|
enum MODE
|
|
|
|
{
|
|
|
|
MODE_CPU = 0,
|
|
|
|
MODE_GPU
|
|
|
|
};
|
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
GLWinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
|
|
|
|
WinApp(width, height, window_name)
|
|
|
|
{
|
2015-08-01 06:16:06 +08:00
|
|
|
m_shutdown = false;
|
|
|
|
m_use_buffer = false;
|
|
|
|
m_demo_processing = true;
|
|
|
|
m_mode = MODE_CPU;
|
|
|
|
m_modeStr[0] = cv::String("Processing on CPU");
|
|
|
|
m_modeStr[1] = cv::String("Processing on GPU");
|
|
|
|
m_cap = cap;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~GLWinApp() {}
|
|
|
|
|
2018-03-30 17:48:54 +08:00
|
|
|
virtual void cleanup() CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
m_shutdown = true;
|
2015-06-29 16:46:31 +08:00
|
|
|
#if defined(__linux__)
|
2015-06-01 23:16:22 +08:00
|
|
|
glXMakeCurrent(m_display, None, NULL);
|
|
|
|
glXDestroyContext(m_display, m_glctx);
|
|
|
|
#endif
|
2015-06-29 16:46:31 +08:00
|
|
|
WinApp::cleanup();
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2018-03-30 17:48:54 +08:00
|
|
|
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
|
|
|
case WM_CHAR:
|
2015-08-01 06:16:06 +08:00
|
|
|
if (wParam == '1')
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
2015-08-01 06:16:06 +08:00
|
|
|
set_mode(MODE_CPU);
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-08-01 06:16:06 +08:00
|
|
|
}
|
|
|
|
if (wParam == '2')
|
|
|
|
{
|
|
|
|
set_mode(MODE_GPU);
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-07-06 22:46:18 +08:00
|
|
|
}
|
|
|
|
else if (wParam == '9')
|
|
|
|
{
|
|
|
|
toggle_buffer();
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
else if (wParam == VK_SPACE)
|
|
|
|
{
|
2015-08-01 06:16:06 +08:00
|
|
|
m_demo_processing = !m_demo_processing;
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
else if (wParam == VK_ESCAPE)
|
|
|
|
{
|
2015-06-29 16:46:31 +08:00
|
|
|
cleanup();
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_CLOSE:
|
2015-06-29 16:46:31 +08:00
|
|
|
cleanup();
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
|
|
|
|
case WM_DESTROY:
|
|
|
|
::PostQuitMessage(0);
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ::DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
2018-03-30 17:48:54 +08:00
|
|
|
int handle_event(XEvent& e) CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
switch(e.type)
|
|
|
|
{
|
|
|
|
case ClientMessage:
|
|
|
|
if ((Atom)e.xclient.data.l[0] == m_WM_DELETE_WINDOW)
|
|
|
|
{
|
|
|
|
m_end_loop = true;
|
2015-06-29 16:46:31 +08:00
|
|
|
cleanup();
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Expose:
|
|
|
|
render();
|
|
|
|
break;
|
|
|
|
case KeyPress:
|
|
|
|
switch(keycode_to_keysym(e.xkey.keycode))
|
|
|
|
{
|
|
|
|
case XK_space:
|
2015-08-01 06:16:06 +08:00
|
|
|
m_demo_processing = !m_demo_processing;
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
case XK_1:
|
2015-08-01 06:16:06 +08:00
|
|
|
set_mode(MODE_CPU);
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
case XK_2:
|
2015-08-01 06:16:06 +08:00
|
|
|
set_mode(MODE_GPU);
|
2015-07-06 22:46:18 +08:00
|
|
|
break;
|
|
|
|
case XK_9:
|
|
|
|
toggle_buffer();
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
case XK_Escape:
|
|
|
|
m_end_loop = true;
|
2015-06-29 16:46:31 +08:00
|
|
|
cleanup();
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-30 17:48:54 +08:00
|
|
|
int init() CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
m_hDC = GetDC(m_hWnd);
|
|
|
|
|
|
|
|
if (setup_pixel_format() != 0)
|
|
|
|
{
|
|
|
|
std::cerr << "Can't setup pixel format" << std::endl;
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_hRC = wglCreateContext(m_hDC);
|
|
|
|
wglMakeCurrent(m_hDC, m_hRC);
|
|
|
|
#elif defined(__linux__)
|
|
|
|
m_glctx = glXCreateContext(m_display, m_visual_info, NULL, GL_TRUE);
|
|
|
|
glXMakeCurrent(m_display, m_window, m_glctx);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
glViewport(0, 0, m_width, m_height);
|
|
|
|
|
|
|
|
if (cv::ocl::haveOpenCL())
|
|
|
|
{
|
2015-06-29 16:46:31 +08:00
|
|
|
(void) cv::ogl::ocl::initializeContextFromGL();
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_oclDevName = cv::ocl::useOpenCL() ?
|
|
|
|
cv::ocl::Context::getDefault().device(0).name() :
|
|
|
|
(char*) "No OpenCL device";
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
} // init()
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
int get_frame(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
if (!m_cap.read(m_frame_bgr))
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-06-01 23:16:22 +08:00
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
cv::cvtColor(m_frame_bgr, m_frame_rgba, cv::COLOR_RGB2RGBA);
|
2015-06-01 23:16:22 +08:00
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
if (do_buffer)
|
2015-07-29 16:07:11 +08:00
|
|
|
buffer.copyFrom(m_frame_rgba, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
|
2015-07-06 22:46:18 +08:00
|
|
|
else
|
2015-07-29 16:07:11 +08:00
|
|
|
texture.copyFrom(m_frame_rgba, true);
|
2015-06-01 23:16:22 +08:00
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
void print_info(MODE mode, double time, cv::String& oclDevName)
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
HDC hDC = m_hDC;
|
|
|
|
|
|
|
|
HFONT hFont = (HFONT)::GetStockObject(SYSTEM_FONT);
|
|
|
|
|
|
|
|
HFONT hOldFont = (HFONT)::SelectObject(hDC, hFont);
|
|
|
|
|
|
|
|
if (hOldFont)
|
|
|
|
{
|
|
|
|
TEXTMETRIC tm;
|
|
|
|
::GetTextMetrics(hDC, &tm);
|
|
|
|
|
|
|
|
char buf[256+1];
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
buf[0] = 0;
|
2015-08-01 06:16:06 +08:00
|
|
|
sprintf_s(buf, sizeof(buf)-1, "Mode: %s OpenGL %s", m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture");
|
2015-06-01 23:16:22 +08:00
|
|
|
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
|
|
|
|
|
|
|
|
y += tm.tmHeight;
|
|
|
|
buf[0] = 0;
|
2015-08-01 06:16:06 +08:00
|
|
|
sprintf_s(buf, sizeof(buf)-1, "Time, msec: %2.1f", time);
|
2015-06-01 23:16:22 +08:00
|
|
|
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
|
|
|
|
|
|
|
|
y += tm.tmHeight;
|
|
|
|
buf[0] = 0;
|
|
|
|
sprintf_s(buf, sizeof(buf)-1, "OpenCL device: %s", oclDevName.c_str());
|
|
|
|
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
|
|
|
|
|
|
|
|
::SelectObject(hDC, hOldFont);
|
|
|
|
}
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
|
|
|
char buf[256+1];
|
2015-08-01 06:16:06 +08:00
|
|
|
snprintf(buf, sizeof(buf)-1, "Time, msec: %2.1f, Mode: %s OpenGL %s, Device: %s", time, m_modeStr[mode].c_str(), use_buffer() ? "buffer" : "texture", oclDevName.c_str());
|
2015-06-01 23:16:22 +08:00
|
|
|
XStoreName(m_display, m_window, buf);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:48:54 +08:00
|
|
|
void idle() CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:48:54 +08:00
|
|
|
int render() CV_OVERRIDE
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (m_shutdown)
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
|
|
|
|
int r;
|
|
|
|
cv::ogl::Texture2D texture;
|
2015-07-06 22:46:18 +08:00
|
|
|
cv::ogl::Buffer buffer;
|
2015-06-01 23:16:22 +08:00
|
|
|
|
2015-07-29 16:07:11 +08:00
|
|
|
texture.setAutoRelease(true);
|
|
|
|
buffer.setAutoRelease(true);
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
MODE mode = get_mode();
|
|
|
|
bool do_buffer = use_buffer();
|
|
|
|
|
|
|
|
r = get_frame(texture, buffer, do_buffer);
|
2015-06-01 23:16:22 +08:00
|
|
|
if (r != 0)
|
|
|
|
{
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
switch (mode)
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
2015-08-01 06:16:06 +08:00
|
|
|
case MODE_CPU: // process frame on CPU
|
|
|
|
processFrameCPU(texture, buffer, do_buffer);
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
case MODE_GPU: // process frame on GPU
|
|
|
|
processFrameGPU(texture, buffer, do_buffer);
|
2015-06-01 23:16:22 +08:00
|
|
|
break;
|
|
|
|
} // switch
|
|
|
|
|
2015-07-06 22:46:18 +08:00
|
|
|
if (do_buffer) // buffer -> texture
|
|
|
|
{
|
|
|
|
cv::Mat m(m_height, m_width, CV_8UC4);
|
|
|
|
buffer.copyTo(m);
|
2015-07-29 16:07:11 +08:00
|
|
|
texture.copyFrom(m, true);
|
2015-07-06 22:46:18 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
XWindowAttributes window_attributes;
|
|
|
|
XGetWindowAttributes(m_display, m_window, &window_attributes);
|
|
|
|
glViewport(0, 0, window_attributes.width, window_attributes.height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glLoadIdentity();
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
texture.bind();
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 0.1f);
|
|
|
|
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f, 0.1f);
|
|
|
|
glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, -1.0f, 0.1f);
|
|
|
|
glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, 1.0f, 0.1f);
|
|
|
|
glEnd();
|
|
|
|
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
SwapBuffers(m_hDC);
|
|
|
|
#elif defined(__linux__)
|
|
|
|
glXSwapBuffers(m_display, m_window);
|
|
|
|
#endif
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
print_info(mode, m_timer.getTimeMilli(), m_oclDevName);
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const cv::Exception& e)
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
void processFrameCPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
|
2015-07-31 00:57:11 +08:00
|
|
|
{
|
|
|
|
cv::Mat m(m_height, m_width, CV_8UC4);
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
m_timer.reset();
|
2015-07-31 00:57:11 +08:00
|
|
|
m_timer.start();
|
|
|
|
|
|
|
|
if (do_buffer)
|
|
|
|
buffer.copyTo(m);
|
|
|
|
else
|
|
|
|
texture.copyTo(m);
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
if (m_demo_processing)
|
2015-07-31 00:57:11 +08:00
|
|
|
{
|
|
|
|
// blur texture image with OpenCV on CPU
|
2018-08-27 06:04:46 +08:00
|
|
|
cv::blur(m, m, cv::Size(15, 15));
|
2015-07-31 00:57:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_buffer)
|
|
|
|
buffer.copyFrom(m, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true);
|
|
|
|
else
|
|
|
|
texture.copyFrom(m, true);
|
|
|
|
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
void processFrameGPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer, bool do_buffer)
|
2015-07-31 00:57:11 +08:00
|
|
|
{
|
|
|
|
cv::UMat u;
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
m_timer.reset();
|
2015-07-31 00:57:11 +08:00
|
|
|
m_timer.start();
|
|
|
|
|
|
|
|
if (do_buffer)
|
|
|
|
u = cv::ogl::mapGLBuffer(buffer);
|
|
|
|
else
|
|
|
|
cv::ogl::convertFromGLTexture2D(texture, u);
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
if (m_demo_processing)
|
2015-07-31 00:57:11 +08:00
|
|
|
{
|
|
|
|
// blur texture image with OpenCV on GPU with OpenCL
|
2018-08-27 06:04:46 +08:00
|
|
|
cv::blur(u, u, cv::Size(15, 15));
|
2015-07-31 00:57:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_buffer)
|
|
|
|
cv::ogl::unmapGLBuffer(u);
|
|
|
|
else
|
|
|
|
cv::ogl::convertToGLTexture2D(u, texture);
|
|
|
|
|
|
|
|
m_timer.stop();
|
|
|
|
}
|
|
|
|
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
int setup_pixel_format()
|
|
|
|
{
|
|
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
|
|
|
|
|
|
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|
|
|
pfd.nVersion = 1;
|
|
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
|
|
pfd.iPixelType = PFD_TYPE_RGBA;
|
|
|
|
pfd.cColorBits = 24;
|
|
|
|
pfd.cRedBits = 8;
|
|
|
|
pfd.cRedShift = 0;
|
|
|
|
pfd.cGreenBits = 8;
|
|
|
|
pfd.cGreenShift = 0;
|
|
|
|
pfd.cBlueBits = 8;
|
|
|
|
pfd.cBlueShift = 0;
|
|
|
|
pfd.cAlphaBits = 8;
|
|
|
|
pfd.cAlphaShift = 0;
|
|
|
|
pfd.cAccumBits = 0;
|
|
|
|
pfd.cAccumRedBits = 0;
|
|
|
|
pfd.cAccumGreenBits = 0;
|
|
|
|
pfd.cAccumBlueBits = 0;
|
|
|
|
pfd.cAccumAlphaBits = 0;
|
|
|
|
pfd.cDepthBits = 24;
|
|
|
|
pfd.cStencilBits = 8;
|
|
|
|
pfd.cAuxBuffers = 0;
|
|
|
|
pfd.iLayerType = PFD_MAIN_PLANE;
|
|
|
|
pfd.bReserved = 0;
|
|
|
|
pfd.dwLayerMask = 0;
|
|
|
|
pfd.dwVisibleMask = 0;
|
|
|
|
pfd.dwDamageMask = 0;
|
|
|
|
|
|
|
|
int pfmt = ChoosePixelFormat(m_hDC, &pfd);
|
|
|
|
if (pfmt == 0)
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-08-01 06:16:06 +08:00
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
if (SetPixelFormat(m_hDC, pfmt, &pfd) == 0)
|
|
|
|
return -2;
|
2015-08-01 06:16:06 +08:00
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
2015-06-01 23:16:22 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
KeySym keycode_to_keysym(unsigned keycode)
|
|
|
|
{ // note that XKeycodeToKeysym() is considered deprecated
|
|
|
|
int keysyms_per_keycode_return = 0;
|
|
|
|
KeySym *keysyms = XGetKeyboardMapping(m_display, keycode, 1, &keysyms_per_keycode_return);
|
|
|
|
KeySym keysym = keysyms[0];
|
|
|
|
XFree(keysyms);
|
|
|
|
return keysym;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
bool use_buffer() { return m_use_buffer; }
|
|
|
|
void toggle_buffer() { m_use_buffer = !m_use_buffer; }
|
|
|
|
MODE get_mode() { return m_mode; }
|
|
|
|
void set_mode(MODE mode) { m_mode = mode; }
|
2015-07-06 22:46:18 +08:00
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
private:
|
|
|
|
bool m_shutdown;
|
2015-08-01 06:16:06 +08:00
|
|
|
bool m_use_buffer;
|
|
|
|
bool m_demo_processing;
|
|
|
|
MODE m_mode;
|
|
|
|
cv::String m_modeStr[2];
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-06-01 23:16:22 +08:00
|
|
|
HDC m_hDC;
|
|
|
|
HGLRC m_hRC;
|
|
|
|
#elif defined(__linux__)
|
|
|
|
GLXContext m_glctx;
|
|
|
|
#endif
|
|
|
|
cv::VideoCapture m_cap;
|
|
|
|
cv::Mat m_frame_bgr;
|
|
|
|
cv::Mat m_frame_rgba;
|
|
|
|
cv::String m_oclDevName;
|
|
|
|
};
|
|
|
|
|
2015-07-29 18:51:03 +08:00
|
|
|
static const char* keys =
|
|
|
|
{
|
2018-08-25 10:37:54 +08:00
|
|
|
"{c camera | 0 | camera id }"
|
2015-07-29 18:51:03 +08:00
|
|
|
"{f file | | movie file name }"
|
|
|
|
};
|
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
using namespace cv;
|
2015-07-29 18:51:03 +08:00
|
|
|
using namespace std;
|
2015-06-01 23:16:22 +08:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2015-08-01 06:16:06 +08:00
|
|
|
cv::CommandLineParser parser(argc, argv, keys);
|
2018-08-25 10:37:54 +08:00
|
|
|
int camera_id = parser.get<int>("camera");
|
2015-08-01 06:16:06 +08:00
|
|
|
string file = parser.get<string>("file");
|
2015-07-29 18:51:03 +08:00
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
parser.about(
|
|
|
|
"\nA sample program demonstrating interoperability of OpenGL and OpenCL with OpenCV.\n\n"
|
|
|
|
"Hot keys: \n"
|
|
|
|
" SPACE - turn processing on/off\n"
|
|
|
|
" 1 - process GL data through OpenCV on CPU\n"
|
|
|
|
" 2 - process GL data through OpenCV on GPU (via OpenCL)\n"
|
|
|
|
" 9 - toggle use of GL texture/GL buffer\n"
|
|
|
|
" ESC - exit\n\n");
|
2015-07-29 18:51:03 +08:00
|
|
|
|
|
|
|
parser.printMessage();
|
|
|
|
|
2015-06-01 23:16:22 +08:00
|
|
|
cv::VideoCapture cap;
|
|
|
|
|
2018-08-25 10:37:54 +08:00
|
|
|
if (file.empty())
|
|
|
|
cap.open(camera_id);
|
2015-07-29 18:51:03 +08:00
|
|
|
else
|
|
|
|
cap.open(file.c_str());
|
|
|
|
|
|
|
|
if (!cap.isOpened())
|
|
|
|
{
|
|
|
|
printf("can not open camera or video file\n");
|
2018-08-25 10:37:54 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-07-29 18:51:03 +08:00
|
|
|
}
|
2015-06-01 23:16:22 +08:00
|
|
|
|
2015-08-01 06:16:06 +08:00
|
|
|
int width = (int)cap.get(CAP_PROP_FRAME_WIDTH);
|
2015-06-01 23:16:22 +08:00
|
|
|
int height = (int)cap.get(CAP_PROP_FRAME_HEIGHT);
|
2015-07-29 18:51:03 +08:00
|
|
|
|
2017-07-25 18:23:44 +08:00
|
|
|
#if defined(_WIN32)
|
2015-07-29 18:51:03 +08:00
|
|
|
string wndname = "WGL Window";
|
|
|
|
#elif defined(__linux__)
|
|
|
|
string wndname = "GLX Window";
|
|
|
|
#endif
|
2015-06-01 23:16:22 +08:00
|
|
|
|
|
|
|
GLWinApp app(width, height, wndname, cap);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
app.create();
|
|
|
|
return app.run();
|
|
|
|
}
|
2018-10-20 04:21:20 +08:00
|
|
|
catch (const cv::Exception& e)
|
2015-06-01 23:16:22 +08:00
|
|
|
{
|
2015-07-29 18:51:03 +08:00
|
|
|
cerr << "Exception: " << e.what() << endl;
|
2015-06-01 23:16:22 +08:00
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2015-07-29 18:51:03 +08:00
|
|
|
cerr << "FATAL ERROR: Unknown exception" << endl;
|
2015-06-01 23:16:22 +08:00
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
}
|