PICCANTE  0.4
The hottest HDR imaging library!
filter_disp.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_GL_FILTERING_FILTER_DISP_HPP
19 #define PIC_GL_FILTERING_FILTER_DISP_HPP
20 
21 #include "../../gl/filtering/filter.hpp"
22 
23 namespace pic {
24 
25 #define DEBUG_GL
26 
30 class FilterGLDisp: public FilterGL
31 {
32 protected:
33 
34  float sigma;
35  float sigma_s;
36  float sigma_r;
37 
41  void initShaders();
42 
43 public:
44 
48  FilterGLDisp();
49 
58  void update(float sigma, float sigma_s, float sigma_r, bool bUse, bool bLeft);
59 
68  static ImageGL *execute(std::string nameLeft,
69  std::string nameRight,
70  std::string nameDisp,
71  std::string nameOut)
72  {
73  ImageGL imgL(nameLeft);
74  ImageGL imgR(nameRight);
75  ImageGL imgD(nameDisp);
76 
77  imgL.generateTextureGL(GL_TEXTURE_2D, GL_FLOAT, false);
78  imgR.generateTextureGL(GL_TEXTURE_2D, GL_FLOAT, false);
79  imgD.generateTextureGL(GL_TEXTURE_2D, GL_FLOAT, false);
80 
81  FilterGLDisp filter;
82 
83  ImageGL *imgOut = filter.Process(TripleGL(&imgL, &imgR, &imgD), NULL);
84  imgOut->loadToMemory();
85  imgOut->Write(nameOut);
86  return imgOut;
87  }
88 };
89 
91 {
92  sigma = 2.0f;
93  sigma_s = 2.0f;
94  sigma_r = 0.05f;
95  initShaders();
96 }
97 
99 {
101  (
102  uniform sampler2D u_texL; \n
103  uniform sampler2D u_texR; \n
104  uniform sampler2D u_texD; \n
105  uniform int halfKernelSize; \n
106  uniform float sigma; \n
107  uniform float sigma_s2; \n
108  uniform float sigma_r2; \n
109  uniform float bUse; \n
110  uniform float bLeft; \n
111  out vec4 f_color; \n
112 
113  vec4 fetchDispCol(ivec2 coords) {
114  float shiftf = texelFetch(u_texD, coords, 0).x;
115 
116  if(shiftf > 1e-3) {
117  coords.x += int(shiftf);
118  vec3 col_R = texelFetch(u_texR, coords, 0).xyz;
119  return vec4(col_R, shiftf);
120  } else {
121  return vec4(0.0);
122  }
123  }
124 
125  /* (x --> disp)
126  (y --> mask)
127  (z --> score) */
128 
129  void main(void) {
130  \n
131  f_color = vec4(0.0);
132  ivec2 coords = ivec2(gl_FragCoord.xy);
133  \n
134  vec2 delta;
135  vec3 acc = vec3(0.0);
136  float tot = 0.0;
137  vec3 refDisp = texelFetch(u_texD, coords, 0).xyz;
138  vec3 refCol = texelFetch(u_texL, coords, 0).xyz;
139 
140  for(int i = -halfKernelSize; i <= halfKernelSize; i++) {
141  delta.y = float(i);
142 
143  for(int j = -halfKernelSize; j <= halfKernelSize; j++) {
144  delta.x = float(j);
145 
146  //Color fetch
147  ivec2 tmpCoords = coords + ivec2(j, i);
148  vec3 tmpCol = texelFetch(u_texL, tmpCoords, 0).xyz;
149 
150  //Disparity fetch
151  vec3 tmpDisp = texelFetch(u_texD, tmpCoords, 0).xyz;
152  tmpCoords.x += int(bLeft * tmpDisp.x);
153  vec3 tmpCol2 = texelFetch(u_texR, tmpCoords, 0).xyz;
154 
155  //Spatial weight
156  float ws = exp(-dot(delta, delta) / sigma_s2);
157 
158  //Disparity weight
159  float deltaDisp = tmpDisp.x - refDisp.x;
160  float wd = exp(-deltaDisp * deltaDisp / sigma_r2);
161 
162  //Other pixels: color similarity
163  vec3 diffCol = tmpCol - refCol;
164  float wc = exp(-dot(diffCol, diffCol) / sigma);
165 
166  if(bUse < 0.5f) {
167  wc = 1.0f;
168  }
169 
170  //((tmpDisp.z+1e-6)*sigma/tmpDisp.y))*bUse;
171 
172  //Weights
173  float w = wd * ws;
174  tot += w * wc;
175  acc += tmpCol * w * wc;
176  /* acc += (tmpCol+tmpCol2*wc)*w;
177  tot += (1.0+wc)*w;*/
178  }
179  }
180 
181  acc /= tot;
182  f_color = vec4(acc, 1.0);
183  }
184  );
185 
186  technique.initStandard("330", vertex_source, fragment_source, "FilterGLDisp");
187 
188  float sigma_s2 = 2.0f * sigma_s * sigma_s;
189  float sigma_r2 = 2.0f * sigma_r * sigma_r;
190  int halfKernelSize = PrecomputedGaussian::getKernelSize(sigma_s) >> 1;
191 
192  technique.bind();
193  technique.setUniform1f("sigma_s2", sigma_s2);
194  technique.setUniform1f("sigma_r2", sigma_r2);
195  technique.setUniform1f("sigma", sigma * sigma * 2.0f);
196  technique.setUniform1i("halfKernelSize", halfKernelSize);
197  technique.setUniform1f("bUse", 1.0f);
198  technique.setUniform1f("bLeft", -1.0f);
199 
200  technique.setUniform1i("u_texL", 0);
201  technique.setUniform1i("u_texR", 1);
202  technique.setUniform1i("u_texD", 2);
203  technique.unbind();
204 }
205 
206 void FilterGLDisp::update(float sigma, float sigma_s, float sigma_r, bool bUse,
207  bool bLeft)
208 {
209  this->sigma = sigma;
210  this->sigma_r = sigma_r;
211  this->sigma_s = sigma_s;
212 
213  int halfKernelSize = PrecomputedGaussian::getKernelSize(sigma_s) >> 1;
214 
215  float sigma_s2 = 2.0f * sigma_s * sigma_s;
216  float sigma_r2 = 2.0f * sigma_r * sigma_r;
217 
218  technique.bind();
219  technique.setUniform1i("u_texL", 0);
220  technique.setUniform1i("u_texR", 1);
221  technique.setUniform1i("u_texD", 2);
222  technique.setUniform1f("sigma_s2", sigma_s2);
223  technique.setUniform1f("sigma_r2", sigma_r2);
224 
225  if(bUse) {
226  technique.setUniform1f("bUse", 1.0f);
227  } else {
228  technique.setUniform1f("bUse", 0.0f);
229  }
230 
231  if(bLeft) {
232  technique.setUniform1f("bLeft", 1.0f);
233  } else {
234  technique.setUniform1f("bLeft", -1.0f);
235  }
236 
237  technique.setUniform1f("sigma", sigma * sigma * 2.0f);
238  technique.setUniform1i("halfKernelSize", halfKernelSize);
239  technique.unbind();
240 }
241 
242 } // end namespace pic
243 
244 #endif /* PIC_GL_FILTERING_FILTER_DISP_HPP */
245 
TechniqueGL technique
Definition: display.hpp:45
static int getKernelSize(float sigma)
KernelSize computes the size of a kernel in pixel give its sigma.
Definition: precomputed_gaussian.hpp:121
GLuint generateTextureGL(GLenum target, GLenum format_type, bool mipmap)
generateTextureGL
void bind()
bind
Definition: display.hpp:189
void update(float sigma, float sigma_s, float sigma_r, bool bUse, bool bLeft)
update
#define MAKE_STRING(input_string)
The ImageGL class.
Definition: image.hpp:42
float sigma_r
Definition: filter_disp.hpp:36
FilterGLDisp()
FilterGLDisp.
bool Write(std::string nameFile, LDR_type typeWrite, int writerCounter)
Write saves an Image into a file on the disk.
Definition: filter_radial_basis_function.hpp:1924
void setUniform1f(const char *name_uniform, float value0)
SetUniform1f.
Definition: display.hpp:247
virtual ImageGL * Process(ImageGLVec imgIn, ImageGL *imgOut)
Process.
Definition: display.hpp:258
The FilterGL class.
Definition: filter.hpp:35
The FilterGL class.
Definition: display.hpp:35
The FilterGLDisp class.
Definition: filter_disp.hpp:30
float sigma
Definition: filter_disp.hpp:34
void initShaders()
initShaders
bool initStandard(std::string version_number, std::string vertex_shader_source, std::string fragment_shader_source, std::string name)
initStandard
Definition: display.hpp:114
PIC_INLINE ImageGLVec TripleGL(ImageGL *img1, ImageGL *img2, ImageGL *img3)
TripleGL creates a triple for filters input.
Definition: image_vec.hpp:67
void unbind()
unbind
Definition: display.hpp:197
void loadToMemory()
loadToMemory
std::string vertex_source
Definition: display.hpp:57
Definition: bilateral_separation.hpp:25
static ImageGL * execute(std::string nameLeft, std::string nameRight, std::string nameDisp, std::string nameOut)
execute
Definition: filter_disp.hpp:68
std::string fragment_source
Definition: display.hpp:57
float sigma_s
Definition: filter_disp.hpp:35
void setUniform1i(const char *name_uniform, int value0)
SetUniform.
Definition: display.hpp:236