Advertisement
Guest User

convolve_gabor.shadron

a guest
Jan 18th, 2018
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <math_constants>
  2.  
  3. param float th     = 0 : range(-PI, PI); // orientation
  4. param float psi    = 0 : range(-PI, PI); // phase
  5. param float lam    = 1 : range(0.1, 10); // wavelength
  6. param float sig    = 1 : range(0.1, 10); // envelope standard deviation
  7. param float gam    = 1 : range(0.1, 10); // aspect ratio
  8. param int convSize = 11 : range(3, 101); // convolutional filter size
  9.  
  10. image Input = file() : map(clamp);
  11.  
  12. glsl vec4 gabor(vec2 position) {
  13.     vec2 coords = position * 2.0 - 1.0;
  14.     vec2 c_s = vec2(coords.x * cos(th) + coords.y * sin(th),
  15.                    -coords.x * sin(th) + coords.y * cos(th));
  16.     float num = pow(c_s.x, 2.0) + pow(gam, 2.0) * pow(c_s.y, 2.0);
  17.     float den = 2 * pow(sig, 2.0);
  18.     float exp_term = exp(-num / den);
  19.     float sin_term = sin(2.0 * PI * c_s.x / lam + psi);
  20.     float gabor = exp_term * sin_term;
  21.     return vec4(gabor, gabor, gabor, 1.0);
  22. }
  23.  
  24. image Gabor = glsl(gabor, 301, 301);
  25.  
  26. glsl vec4 convolve(vec2 position) {
  27.     vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
  28.     float filt_val = 0.0;
  29.     for(int i = 0; i < convSize; ++i) {
  30.         for(int j = 0; j < convSize; ++j) {
  31.             float offx = (j - floor(convSize / 2.0)) * shadron_PixelSize.x;
  32.             float offy = (i - floor(convSize / 2.0)) * shadron_PixelSize.y;
  33.             float xpos = position.x + offx;
  34.             float ypos = position.y + offy;
  35.             vec4 currentPixel = texture(Input, vec2(xpos, ypos));
  36.  
  37.             float filtX = j / (convSize - 1.0);
  38.             float filtY = i / (convSize - 1.0);
  39.             vec4 filterPixel  = texture(Gabor, vec2(filtX, filtY));
  40.             filt_val += filterPixel.r;
  41.             sum += currentPixel * filterPixel;
  42.         }
  43.     }
  44.     return sum / filt_val;
  45. }
  46.  
  47. image Output = glsl(convolve, sizeof(Input));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement