From ea102901530755948bb7c0ea83dddc36fdb5ab4b Mon Sep 17 00:00:00 2001 From: Alexey Ershov Date: Thu, 30 Jul 2015 19:57:11 +0300 Subject: [PATCH] change displaying of fps to time (ms) --- samples/opengl/opengl_interop.cpp | 133 +++++++++++++++--------------- samples/opengl/winapp.hpp | 49 +++++++++++ 2 files changed, 114 insertions(+), 68 deletions(-) diff --git a/samples/opengl/opengl_interop.cpp b/samples/opengl/opengl_interop.cpp index 38ab2ce5e2..eccdf802c8 100644 --- a/samples/opengl/opengl_interop.cpp +++ b/samples/opengl/opengl_interop.cpp @@ -113,23 +113,6 @@ public: } #endif - static float getFps() - { - static std::queue time_queue; - - int64 now = cv::getTickCount(); - int64 then = 0; - time_queue.push(now); - - if (time_queue.size() >= 2) - then = time_queue.front(); - - if (time_queue.size() >= 25) - time_queue.pop(); - - return time_queue.size() * (float)cv::getTickFrequency() / (now - then); - } - #if defined(__linux__) int handle_event(XEvent& e) { @@ -230,7 +213,7 @@ public: return 0; } - void print_info(int mode, float fps, cv::String oclDevName) + void print_info(int mode, float time, cv::String oclDevName) { #if defined(WIN32) || defined(_WIN32) HDC hDC = m_hDC; @@ -253,7 +236,7 @@ public: y += tm.tmHeight; buf[0] = 0; - sprintf_s(buf, sizeof(buf)-1, "FPS: %2.1f", fps); + sprintf_s(buf, sizeof(buf)-1, "Time: %2.1f", time); ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); y += tm.tmHeight; @@ -266,7 +249,7 @@ public: #elif defined(__linux__) char buf[256+1]; - snprintf(buf, sizeof(buf)-1, "FPS: %2.1f Mode: %s Device: %s", fps, m_modeStr[mode].c_str(), oclDevName.c_str()); + snprintf(buf, sizeof(buf)-1, "Time: %2.1f Mode: %s Device: %s", time, m_modeStr[mode].c_str(), oclDevName.c_str()); XStoreName(m_display, m_window, buf); #endif } @@ -297,60 +280,20 @@ public: } bool do_buffer = use_buffer(); + switch (get_mode()) { - case 0: - // no processing + case 0: // no processing + m_timer.clear(); break; - case 1: - { - // process video frame on CPU - cv::Mat m(m_height, m_width, CV_8UC4); - - if (do_buffer) - buffer.copyTo(m); - else - texture.copyTo(m); - - if (!m_disableProcessing) - { - // blur texture image with OpenCV on CPU - cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); - } - - if (do_buffer) - buffer.copyFrom(m, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true); - else - texture.copyFrom(m, true); - + case 1: // process frame on CPU + processFrameCPU(texture, buffer); break; - } - - case 2: - { - // process video frame on GPU - cv::UMat u; - - if (do_buffer) - u = cv::ogl::mapGLBuffer(buffer); - else - cv::ogl::convertFromGLTexture2D(texture, u); - - if (!m_disableProcessing) - { - // blur texture image with OpenCV on GPU with OpenCL - cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); - } - - if (do_buffer) - cv::ogl::unmapGLBuffer(u); - else - cv::ogl::convertToGLTexture2D(u, texture); + case 2: // process frame on GPU + processFrameGPU(texture, buffer); break; - } - } // switch if (do_buffer) // buffer -> texture @@ -385,7 +328,7 @@ public: glXSwapBuffers(m_display, m_window); #endif - print_info(m_mode, getFps(), m_oclDevName); + print_info(m_mode, m_timer.time(Timer::UNITS::MSEC), m_oclDevName); } @@ -400,6 +343,60 @@ public: protected: + void processFrameCPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer) + { + cv::Mat m(m_height, m_width, CV_8UC4); + + bool do_buffer = use_buffer(); + + m_timer.start(); + + if (do_buffer) + buffer.copyTo(m); + else + texture.copyTo(m); + + if (!m_disableProcessing) + { + // blur texture image with OpenCV on CPU + cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); + } + + if (do_buffer) + buffer.copyFrom(m, cv::ogl::Buffer::PIXEL_UNPACK_BUFFER, true); + else + texture.copyFrom(m, true); + + m_timer.stop(); + } + + void processFrameGPU(cv::ogl::Texture2D& texture, cv::ogl::Buffer& buffer) + { + cv::UMat u; + + bool do_buffer = use_buffer(); + + m_timer.start(); + + if (do_buffer) + u = cv::ogl::mapGLBuffer(buffer); + else + cv::ogl::convertFromGLTexture2D(texture, u); + + if (!m_disableProcessing) + { + // blur texture image with OpenCV on GPU with OpenCL + cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); + } + + if (do_buffer) + cv::ogl::unmapGLBuffer(u); + else + cv::ogl::convertToGLTexture2D(u, texture); + + m_timer.stop(); + } + #if defined(WIN32) || defined(_WIN32) int setup_pixel_format() { diff --git a/samples/opengl/winapp.hpp b/samples/opengl/winapp.hpp index 3d83e09789..cf36774955 100644 --- a/samples/opengl/winapp.hpp +++ b/samples/opengl/winapp.hpp @@ -22,6 +22,54 @@ #define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; } +class Timer +{ +public: + enum UNITS + { + USEC = 0, + MSEC, + SEC + }; + + Timer() : m_t0(0), m_diff(0) + { + m_tick_frequency = (float)cv::getTickFrequency(); + + m_unit_mul[USEC] = 1000000; + m_unit_mul[MSEC] = 1000; + m_unit_mul[SEC] = 1; + } + + void clear() + { + m_t0 = m_diff = 0; + } + + void start() + { + m_t0 = cv::getTickCount(); + } + + void stop() + { + m_diff = cv::getTickCount() - m_t0; + } + + float time(UNITS u = UNITS::MSEC) + { + float sec = m_diff / m_tick_frequency; + + return sec * m_unit_mul[u]; + } + +public: + float m_tick_frequency; + int64 m_t0; + int64 m_diff; + int m_unit_mul[3]; +}; + class WinApp { public: @@ -218,4 +266,5 @@ protected: int m_width; int m_height; std::string m_window_name; + Timer m_timer; };