refactoring, renaming and commenting

pull/3471/head
kallaballa 2 years ago
parent f8a5c86b7a
commit a12f1cb582
  1. 31
      src/common/subsystems.hpp
  2. 6
      src/font/font-demo.cpp
  3. 51
      src/optflow/optflow-demo.cpp

@ -353,28 +353,6 @@ void end() {
GL_CHECK(glFinish());
}
void push() {
GL_CHECK(glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS));
GL_CHECK(glPushAttrib(GL_ALL_ATTRIB_BITS));
GL_CHECK(glMatrixMode(GL_MODELVIEW));
GL_CHECK(glPushMatrix());
GL_CHECK(glMatrixMode(GL_PROJECTION));
GL_CHECK(glPushMatrix());
GL_CHECK(glMatrixMode(GL_TEXTURE));
GL_CHECK(glPushMatrix());
}
void pop() {
GL_CHECK(glMatrixMode(GL_TEXTURE));
GL_CHECK(glPopMatrix());
GL_CHECK(glMatrixMode(GL_PROJECTION));
GL_CHECK(glPopMatrix());
GL_CHECK(glMatrixMode(GL_MODELVIEW));
GL_CHECK(glPopMatrix());
GL_CHECK(glPopClientAttrib());
GL_CHECK(glPopAttrib());
}
void init() {
glewExperimental = true;
glewInit();
@ -404,14 +382,14 @@ std::string get_info() {
return reinterpret_cast<const char*>(glGetString(GL_VERSION));
}
void acquire_from_gl(cv::UMat &m) {
void acquire_from_gl(cv::UMat& m) {
gl::begin();
GL_CHECK(cv::ogl::convertFromGLTexture2D(*gl::frame_buf_tex, m));
//The OpenGL frameBuffer is upside-down. Flip it. (OpenCL)
cv::flip(m, m, 0);
}
void release_to_gl(cv::UMat &m) {
void release_to_gl(cv::UMat& m) {
//The OpenGL frameBuffer is upside-down. Flip it back. (OpenCL)
cv::flip(m, m, 0);
GL_CHECK(cv::ogl::convertToGLTexture2D(m, *gl::frame_buf_tex));
@ -423,7 +401,6 @@ void blit_frame_buffer_to_screen() {
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, WIDTH, HEIGHT, 0, 0, WIDTH, HEIGHT, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
bool display() {
@ -503,11 +480,7 @@ void init(bool debug = false) {
exit(24);
}
nvgCreateFont(vg, "serif", "assets/LinLibertine_RB.ttf");
/*nvgCreateFont(vg, "sans-bold", "fonts/DejaVuSans-Bold.ttf");
nvgCreateFont(vg, "sans", "fonts/DejaVuSans.ttf");
*/
}
} //namespace nvg

@ -7,6 +7,7 @@ constexpr const char *OUTPUT_FILENAME = "font-demo.mkv";
constexpr const int VA_HW_DEVICE_INDEX = 0;
constexpr double FPS = 60;
constexpr float FONT_SIZE = 40.0f;
#include "../common/subsystems.hpp"
#include <string>
@ -81,7 +82,7 @@ int main(int argc, char **argv) {
nvgTranslate(vg, 0, translateY);
for (std::string line; std::getline(iss, line); ) {
if(skipLines <= 0) {
if(skipLines == 0) {
if(((translateY + y) / FONT_SIZE) < maxLines) {
nvgText(vg, WIDTH/2.0, y, line.c_str(), line.c_str() + line.size());
y += FONT_SIZE;
@ -119,10 +120,11 @@ int main(int argc, char **argv) {
//Encode the frame using VAAPI on the GPU.
writer << videoFrame;
print_fps();
++cnt;
if(cnt == std::numeric_limits<size_t>().max())
cnt = 1;
print_fps();
}
return 0;

@ -1,30 +1,14 @@
#define CL_TARGET_OPENCL_VERSION 120
#include <cmath>
//WIDTH and HEIGHT have to be specified before including subsystems.hpp
constexpr unsigned long WIDTH = 1920;
constexpr unsigned long HEIGHT = 1080;
constexpr unsigned long DIAG = hypot(double(WIDTH), double(HEIGHT));
constexpr bool OFFSCREEN = false;
constexpr int VA_HW_DEVICE_INDEX = 0;
//Visualization parameters
constexpr float FG_SCALE = 0.5f;
constexpr float FG_LOSS = 4.7; //in percent
constexpr float SCENE_CHANGE_THRESH = 0.29f;
constexpr float SCENE_CHANGE_THRESH_DIFF = 0.1f;
constexpr float MAX_POINTS = 250000.0;
constexpr float POINT_LOSS = 25; // in percent
constexpr int MAX_STROKE = 17;
constexpr float HUE = 36; //in degress
constexpr float SATURATION = 100; //in percent
constexpr float LIGHTNESS = 60; //in percent
constexpr float ALPHA = 2.85; //in percent
constexpr int GLOW_KERNEL_SIZE = WIDTH / 120 % 2 == 0 ? WIDTH / 120 + 1 : WIDTH / 120;
#include "../common/subsystems.hpp"
#include <vector>
#include <string>
@ -33,6 +17,29 @@ constexpr int GLOW_KERNEL_SIZE = WIDTH / 120 % 2 == 0 ? WIDTH / 120 + 1 : WIDTH
#include <opencv2/imgproc.hpp>
#include <opencv2/optflow.hpp>
/** Visualization parameters **/
// Generate the foreground at this scale.
constexpr float FG_SCALE = 0.5f;
// On every frame the foreground loses on brightness. specifies the loss in percent.
constexpr float FG_LOSS = 4.7;
// Peak thresholds for the scene change detection. Raising them makes the detection less sensitive but
// the default should be fine.
constexpr float SCENE_CHANGE_THRESH = 0.29f;
constexpr float SCENE_CHANGE_THRESH_DIFF = 0.1f;
// The theoretical maximum number of points to track which is scaled by the density of detected points
// and therefor is usually much smaller.
constexpr float MAX_POINTS = 250000.0;
// How many of the tracked points to lose intentionally, in percent.
constexpr float POINT_LOSS = 25;
// The theoretical maximum size of the drawing stroke which is scaled by the area of the convex hull
// of tracked points and therefor is usually much smaller.
constexpr int MAX_STROKE = 17;
// Intensity of glow defined by kernel size. The default scales with the image diagonal.
constexpr int GLOW_KERNEL_SIZE = DIAG / 138 % 2 == 0 ? DIAG / 138 + 1 : DIAG / 138;
//hue, saturation, lightness and alpha all from 0 to 255
const cv::Scalar COLOR(26, 255, 153, 7);
using std::cerr;
using std::endl;
using std::vector;
@ -77,7 +84,7 @@ bool detect_scene_change(const cv::UMat& srcMotionMaskGrey, float thresh, float
void visualize_sparse_optical_flow(const cv::UMat& prevGrey, const cv::UMat &nextGrey, vector<cv::Point2f> &detectedPoints,
const float scaleFactor, const int maxStrokeSize,
const float hueDegress, const float satPercent, const float lightPercent, const float alphaPercent,
const cv::Scalar color,
int maxPoints, float pointLossPercent) {
static vector<cv::Point2f> hull, prevPoints, nextPoints, newPoints;
static vector<cv::Point2f> upPrevPoints, upNextPoints;
@ -115,7 +122,7 @@ void visualize_sparse_optical_flow(const cv::UMat& prevGrey, const cv::UMat &nex
using kb::nvg::vg;
nvgBeginPath(vg);
nvgStrokeWidth(vg, stroke);
nvgStrokeColor(vg, nvgHSLA(hueDegress / 360.0, satPercent / 100.f, lightPercent / 100.0f, 255 * (alphaPercent / 100.0f)));
nvgStrokeColor(vg, nvgHSLA(color[0] / 255.0, color[1] / 255.f, color[2] / 255.0f, color[3]));
for (size_t i = 0; i < prevPoints.size(); i++) {
if (status[i] == 1 && err[i] < (1.0 / density) && upNextPoints[i].y >= 0 && upNextPoints[i].x >= 0 && upNextPoints[i].y < nextGrey.rows / scaleFactor && upNextPoints[i].x < nextGrey.cols / scaleFactor && !(upPrevPoints[i].x == upNextPoints[i].x && upPrevPoints[i].y == upNextPoints[i].y)) {
@ -230,7 +237,7 @@ int main(int argc, char **argv) {
nvg::clear();
if (!downPrevGrey.empty()) {
if (!detect_scene_change(downMotionMaskGrey, SCENE_CHANGE_THRESH, SCENE_CHANGE_THRESH_DIFF)) {
visualize_sparse_optical_flow(downPrevGrey, downNextGrey, detectedPoints, FG_SCALE, MAX_STROKE, HUE, SATURATION, LIGHTNESS, ALPHA, MAX_POINTS, POINT_LOSS);
visualize_sparse_optical_flow(downPrevGrey, downNextGrey, detectedPoints, FG_SCALE, MAX_STROKE, COLOR, MAX_POINTS, POINT_LOSS);
}
}
nvg::end();

Loading…
Cancel
Save