From 1622572018b35982441b53b93c78cf6610fb1799 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 22 Jul 2018 15:16:43 +0200 Subject: [PATCH] Add in the relative weighting of the variational refinement terms. --- equations.frag | 14 +++++++++----- flow.cpp | 13 +++++++++++++ smoothness.frag | 5 ++++- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/equations.frag b/equations.frag index 3dc21ec..d7a39ba 100644 --- a/equations.frag +++ b/equations.frag @@ -8,6 +8,12 @@ uniform sampler2D diff_flow_tex, base_flow_tex; uniform sampler2D beta_0_tex; uniform sampler2D smoothness_x_tex, smoothness_y_tex; +// Relative weighting of intensity term. +uniform float delta; + +// Relative weighting of gradient term. +uniform float gamma; + // TODO: Consider a specialized version for the case where we know that du = dv = 0, // since we run so few iterations. @@ -68,9 +74,8 @@ void main() // // TODO: Evaluate squaring β_0. // FIXME: Should the penalizer be adjusted for 0..1 intensity range instead of 0..255? - // TODO: Multiply by some alpha. float beta_0 = texture(beta_0_tex, tc).x; - float k1 = beta_0 * inversesqrt(beta_0 * (I_x * du + I_y * dv + I_t) * (I_x * du + I_y * dv + I_t) + 1e-6); + float k1 = delta * beta_0 * inversesqrt(beta_0 * (I_x * du + I_y * dv + I_t) * (I_x * du + I_y * dv + I_t) + 1e-6); float A11 = k1 * I_x * I_x; float A12 = k1 * I_x * I_y; float A22 = k1 * I_y * I_y; @@ -108,7 +113,7 @@ void main() // (see derivatives.frag). float beta_x = 1.0 / (I_xx * I_xx + I_xy * I_xy + 1e-7); float beta_y = 1.0 / (I_xy * I_xy + I_yy * I_yy + 1e-7); - float k2 = inversesqrt( + float k2 = gamma * inversesqrt( beta_x * (I_xx * du + I_xy * dv + I_xt) * (I_xx * du + I_xy * dv + I_xt) + beta_y * (I_xy * du + I_yy * dv + I_yt) * (I_xy * du + I_yy * dv + I_yt) + 1e-6); @@ -121,8 +126,7 @@ void main() b2 -= k_x * I_xy * I_xt + k_y * I_yy * I_yt; // E_S term, sans the part on the right-hand side that deals with - // the neighboring pixels. - // TODO: Multiply by some gamma. + // the neighboring pixels. The gamma is multiplied in in smoothness.frag. float smooth_l = textureOffset(smoothness_x_tex, tc, ivec2(-1, 0)).x; float smooth_r = texture(smoothness_x_tex, tc).x; float smooth_d = textureOffset(smoothness_y_tex, tc, ivec2( 0, -1)).x; diff --git a/flow.cpp b/flow.cpp index f16eda2..1761881 100644 --- a/flow.cpp +++ b/flow.cpp @@ -30,6 +30,11 @@ constexpr unsigned coarsest_level = 5; constexpr unsigned finest_level = 1; constexpr unsigned patch_size_pixels = 12; +// Weighting constants for the different parts of the variational refinement. +// These don't correspond 1:1 to the values given in the DIS paper, +// since we have different normalizations and ranges in some cases. +float vr_gamma = 10.0f, vr_delta = 5.0f, vr_alpha = 10.0f; + // Some global OpenGL objects. GLuint nearest_sampler, linear_sampler, smoothness_sampler; GLuint vertex_vbo; @@ -571,6 +576,7 @@ private: GLuint smoothness_vao; GLuint uniform_flow_tex, uniform_diff_flow_tex; + GLuint uniform_alpha; }; ComputeSmoothness::ComputeSmoothness() @@ -590,6 +596,7 @@ ComputeSmoothness::ComputeSmoothness() uniform_flow_tex = glGetUniformLocation(smoothness_program, "flow_tex"); uniform_diff_flow_tex = glGetUniformLocation(smoothness_program, "diff_flow_tex"); + uniform_alpha = glGetUniformLocation(smoothness_program, "alpha"); } void ComputeSmoothness::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, int level_width, int level_height) @@ -598,6 +605,7 @@ void ComputeSmoothness::exec(GLuint flow_tex, GLuint diff_flow_tex, GLuint smoot bind_sampler(smoothness_program, uniform_flow_tex, 0, flow_tex, nearest_sampler); bind_sampler(smoothness_program, uniform_diff_flow_tex, 1, diff_flow_tex, nearest_sampler); + glProgramUniform1f(smoothness_program, uniform_alpha, vr_alpha); GLuint smoothness_fbo; // TODO: cleanup glCreateFramebuffers(1, &smoothness_fbo); @@ -646,6 +654,7 @@ private: GLuint uniform_diff_flow_tex, uniform_base_flow_tex; GLuint uniform_beta_0_tex; GLuint uniform_smoothness_x_tex, uniform_smoothness_y_tex; + GLuint uniform_gamma, uniform_delta; }; SetupEquations::SetupEquations() @@ -670,6 +679,8 @@ SetupEquations::SetupEquations() uniform_beta_0_tex = glGetUniformLocation(equations_program, "beta_0_tex"); uniform_smoothness_x_tex = glGetUniformLocation(equations_program, "smoothness_x_tex"); uniform_smoothness_y_tex = glGetUniformLocation(equations_program, "smoothness_y_tex"); + uniform_gamma = glGetUniformLocation(equations_program, "gamma"); + uniform_delta = glGetUniformLocation(equations_program, "delta"); } void SetupEquations::exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex, GLuint base_flow_tex, GLuint beta_0_tex, GLuint smoothness_x_tex, GLuint smoothness_y_tex, GLuint equation_tex, int level_width, int level_height) @@ -683,6 +694,8 @@ void SetupEquations::exec(GLuint I_x_y_tex, GLuint I_t_tex, GLuint diff_flow_tex bind_sampler(equations_program, uniform_beta_0_tex, 4, beta_0_tex, nearest_sampler); bind_sampler(equations_program, uniform_smoothness_x_tex, 5, smoothness_x_tex, smoothness_sampler); bind_sampler(equations_program, uniform_smoothness_y_tex, 6, smoothness_y_tex, smoothness_sampler); + glProgramUniform1f(equations_program, uniform_delta, vr_delta); + glProgramUniform1f(equations_program, uniform_gamma, vr_gamma); GLuint equations_fbo; // TODO: cleanup glCreateFramebuffers(1, &equations_fbo); diff --git a/smoothness.frag b/smoothness.frag index 583231e..adc3dcf 100644 --- a/smoothness.frag +++ b/smoothness.frag @@ -6,6 +6,9 @@ const float eps_sq = 0.001 * 0.001; uniform sampler2D flow_tex, diff_flow_tex; +// Relative weighting of smoothness term. +uniform float alpha; + // This must be a macro, since the offset needs to be a constant expression. #define get_flow(x_offs, y_offs) \ (textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xy + \ @@ -13,7 +16,7 @@ uniform sampler2D flow_tex, diff_flow_tex; float diffusivity(float u_x, float u_y, float v_x, float v_y) { - return inversesqrt(u_x * u_x + u_y * u_y + v_x * v_x + v_y * v_y + eps_sq); + return alpha * inversesqrt(u_x * u_x + u_y * u_y + v_x * v_x + v_y * v_y + eps_sq); } void main() -- 2.39.2