title: How to obtain mean and std as Gabor Wavelet Feature
body: <p>i'm trying to implement Gabor Wavelet feature as described in this paper:
  <em>"Texture Features for Browsing and Retrieval of Image Data"</em></p>
  
  <p>the feature vector is composed from mean and standard deviation (example of feature vector below has scale=4 and orientation=6)</p>
  
  <p><img src="https://i.stack.imgur.com/Q2KM8.png" alt="enter image description here"></p>
  
  <p>Implementation code:</p>
  
  <pre><code>void gabor_main(int argc, char **argv)
  {
      int img_height;             // height of input image
      int img_width;              // width of input image
      int side;                   // side (filter dimension = (2*side+1)*(2*side+1)) = 60
      int scale;                  // number of scale
      int orientation;            // number of orientation
      int flag;                   // flag (removing the DC term) = 0 (False)
  
      FILE* fp;
      unsigned char *tmp_raw_img; // temporary raw image data
      double Ul;                  // Uh (highest spatial frequency)
      double Uh;                  // Ul (lowest spatial frequency)
  
      Matrix* img_mat;            // input image
      Matrix* F_r;                // result, real part
      Matrix* F_i;                // result, imaginary part
      Matrix* F_m;                // result, magnitude of real part and imaginary part
  
      scale = 4;
      orientation = 6;
      Ul = 0.1;
      Uh = 0.4;
      flag = 0;
      side = 60;
  
  
      ...
  
  
      /* ----------------- Reading raw image ----------------- */
  
  
      ...
  
  
      /* ----------------- Gabor filtered outputs ----------------- */
      CreateMatrix(&amp;F_r, img_height * scale, img_width * orientation); // memory allocation of real part matrix of the output 
      CreateMatrix(&amp;F_i, img_height * scale, img_width * orientation); // memory allocation of imaginary part matrix of the output
      CreateMatrix(&amp;F_m, img_height * scale, img_width * orientation); // // memory allocation of magnitude of the output
  
      GaborFilteredImg(F_r, F_i, img_mat, side, Ul, Uh, scale, orientation, flag);
  
  
      /* ----------------- Compute Feature Vector ----------------- */
      // Magnitude of complex value
      for (int h = 0; h &lt; (img_height * scale); h++)
      {
          for (int w = 0; w &lt; (img_width * orientation); w++)
          {
              F_m-&gt;data[h][w] = sqrt(F_r-&gt;data[h][w] * F_r-&gt;data[h][w] + F_i-&gt;data[h][w] * F_i-&gt;data[h][w]);
          }
      }
  
      for(int i = 0; i &lt; scale; i++)
      {
          for(int j = 0;j &lt; orientation; j++)
          {
              double avg = Average(F_m, img_height, img_width, i, j);
              double std = StandardDeviation(F_m, img_height, img_width, i, j);
  
              // Print the result
              std::cout &lt;&lt; avg &lt;&lt; " " &lt;&lt; std &lt;&lt; "\n";
          }
      }    
  
      FreeMatrix(F_r);
      FreeMatrix(F_i);
      FreeMatrix(F_m);
  }
  </code></pre>
  
  <p>code of mean and standard deviation:</p>
  
  <pre><code>double Average(Matrix* F_m, int img_height, int img_width, int scale, int orientation)
  {
      double avg = 0.0;
  
      for (int h = (img_height * scale); h &lt; (img_height * (scale + 1)); h++)
      {
          for (int w = (img_width * orientation); w &lt; (img_width * (orientation + 1)); w++)
          {
              avg += F_m-&gt;data[h][w];
          }
      }
      avg /= (img_height * img_width);
  
      return avg;
  }
  
  double StandardDeviation(Matrix* F_m, int img_height, int img_width, int scale, int orientation)
  {
      double std = 0.0;
      double avg = Average(F_m, img_height, img_width, scale, orientation);
  
      for (int h = (img_height * scale); h &lt; (img_height * (scale + 1)); h++)
      {
          for (int w = (img_width * orientation); w &lt; (img_width * (orientation + 1)); w++)
          {
              double dif = F_m-&gt;data[h][w] - avg;
              std += (dif * dif);
          }
      }
      std = sqrt(std / (img_height * img_width));
  
      return std;
  }
  </code></pre>
  
  <p>note:
  code of function of GaborFilteredImg i copied from this <a href="http://vision.ece.ucsb.edu/texture/software/gabor.c" rel="nofollow noreferrer">http://vision.ece.ucsb.edu/texture/software/gabor.c</a></p>
  
  <p>i would like to ask if the code i wrote (starting from "Compute Texture Feature" section) is correct. I am not sure in getting mean and std given output F_r (real part) and F_i(imaginary part). Basically i calculate the mean and std for every response of gabor filter bank</p>
  
  <p>===UPDATE===</p>
  
  <p><img src="https://i.stack.imgur.com/9XR3C.png" alt="enter image description here"></p>
  
  <p>Those F_r and F_i are the result of gabor filtering using scale=4 and orientation=6. 
  Both F_r and F_i have dimension (img_height * scale) * (img_width * orientation) basically are composed of grids for each response of gabor filter bank.</p>
  
  <p>Then i compute the magnitude F_m(x,y) = sqrt(F_r(x, y) * F_r(x, y) + F_i(x, y) * F_i(x, y))</p>
  
  <p>Finally i calculate the feature vector which is the mean and standard deviation of F_m</p>
  
  <p>===IMAGES===</p>
  
  <p>Image input (real): <a href="http://goo.gl/kc5BG" rel="nofollow noreferrer">http://goo.gl/kc5BG</a></p>
  
  <p>Gabor banks (real) : <a href="http://goo.gl/0qM4E" rel="nofollow noreferrer">http://goo.gl/0qM4E</a></p>
  
  <p>Gabor banks (imaginary) : <a href="http://goo.gl/r7Fnk" rel="nofollow noreferrer">http://goo.gl/r7Fnk</a></p>
  
  <p>Output (real) : <a href="http://goo.gl/nxVMn" rel="nofollow noreferrer">http://goo.gl/nxVMn</a></p>
  
  <p>Output (imaginary) : <a href="http://goo.gl/SnD7p" rel="nofollow noreferrer">http://goo.gl/SnD7p</a></p>