title: Image remains unchanged after convolution
body: <p>I'm attempting to perform Gaussian blur calculations based on source from the <a href="http://www.aforgenet.com/framework/docs/html/f074e0dd-865c-fd5f-ba0a-80e336a0eaea.htm" rel="nofollow">AForge</a> framework. At present though I have something wrong with my calculations in that I am getting the same pixel data out of the process as I am putting in. (Something to do, I think with calculating the divider)</p>
  
  <p>The process comes in two parts:</p>
  
  <ol>
  <li><p>Create a Gaussian filter based on a kernel created with the set size.</p></li>
  <li><p>Process the filter against an array of pixels (rgba structure) to return the transformed pixels which are later converted to a bitmap.</p></li>
  </ol>
  
  <p>The method that I have for creating the original kernel that is converted to an integer based one has been tested against other programs and is correct in its implementation.</p>
  
  <p>Any help would be greatly appreciated. I've been working on this for the last 12 hours.</p>
  
  <p><strong>The method that creates a filter</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Create a 2 dimensional Gaussian kernel using the Gaussian G(x y)  
  /// function for blurring images.
  /// &lt;/summary&gt;
  /// &lt;param name="kernelSize"&gt;Kernel Size&lt;/param&gt;
  /// &lt;returns&gt;A Gaussian Kernel with the given size.&lt;/returns&gt;
  public double[,] CreateGuassianBlurFilter(int kernelSize)
  {
      // Create kernel
      double[,] kernel = this.CreateGaussianKernel2D(kernelSize);
      double min = kernel[0, 0];
  
      // Convert to integer blurring kernel. First of all the integer kernel
      // is calculated from Kernel2D
      // by dividing all elements by the element with the smallest value.
      double[,] intKernel = new double[kernelSize, kernelSize];
      int divider = 0;
  
      for (int i = 0; i &lt; kernelSize; i++)
      {
          for (int j = 0; j &lt; kernelSize; j++)
          {
              double v = kernel[i, j] / min;
  
              if (v &gt; ushort.MaxValue)
              {
                  v = ushort.MaxValue;
              }
  
              intKernel[i, j] = (int)v;
  
              // Collect the divider
              divider += (int)intKernel[i, j];
          }
      }
  
      // Update filter
      this.Divider = divider;
      return intKernel;
  }
  </code></pre>
  
  <p><strong>And the method that performs the convolution:</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>/// &lt;summary&gt;
  /// Processes the given kernel to produce an array of pixels representing a 
  /// bitmap.
  /// &lt;/summary&gt;
  /// &lt;param name="pixels"&gt;The raw pixels of the image to blur&lt;/param&gt;
  /// &lt;param name="kernel"&gt;
  /// The Gaussian kernel to use when performing the method&lt;/param&gt;
  /// &lt;returns&gt;An array of pixels representing the bitmap.&lt;/returns&gt;
  public Pixel[,] ProcessKernel(Pixel[,] pixels, double[,] kernel)
  {
      int width = pixels.GetLength(0);
      int height = pixels.GetLength(1);
      int kernelLength = kernel.GetLength(0);
      int radius = kernelLength &gt;&gt; 1;
      int kernelSize = kernelLength * kernelLength;
      Pixel[,] result = new Pixel[width, height];
  
      // For each line
      for (int y = 0; y &lt; height; y++)
      {
          // For each pixel
          for (int x = 0; x &lt; width; x++)
          {
              // The number of kernel elements taken into account
              int processedKernelSize;
  
              // Colour sums
              double blue;
              double alpha;
              double divider;
              double green;
              double red = green = blue = alpha = divider = 
                           processedKernelSize = 0;
  
              // For each kernel row
              for (int i = 0; i &lt; kernelLength; i++)
              {
                  int ir = i - radius;
                  int position = y + ir;
  
                  // Skip the current row
                  if (position &lt; 0)
                  {
                      continue;
                  }
  
                  // Outwith the current bounds so break.
                  if (position &gt;= height)
                  {
                      break;
                  }
  
                  // For each kernel column
                  for (int j = 0; j &lt; kernelLength; j++)
                  {
                      int jr = j - radius;
                      position = x + jr;
  
                      // Skip the column
                      if (position &lt; 0)
                      {
                          continue;
                      }
  
                      if (position &lt; width)
                      {
                          double k = kernel[i, j];
                          Pixel pixel = pixels[x, y];
  
                          divider += k;
  
                          red += k * pixel.R;
                          green += k * pixel.G;
                          blue += k * pixel.B;
                          alpha += k * pixel.A;
  
                          processedKernelSize++;
                      }
                  }
              }
  
              // Check to see if all kernel elements were processed
              if (processedKernelSize == kernelSize)
              {
                  // All kernel elements are processed; we are not on the edge.
                  divider = this.Divider;
              }
              else
              {
                  // We are on an edge; do we need to use dynamic divider or not?
                  if (!this.UseDynamicDividerForEdges)
                  {
                      // Apply the set divider.
                      divider = this.Divider;
                  }
              }
  
              // Check and apply the divider
              if ((long)divider != 0)
              {
                  red /= divider;
                  green /= divider;
                  blue /= divider;
                  alpha /= divider;
              }
  
              // Add any applicable threshold.
              red += this.Threshold;
              green += this.Threshold;
              blue += this.Threshold;
              alpha += this.Threshold;
  
              result[x, y].R = (byte)((red &gt; 255) 
                             ? 255 : ((red &lt; 0) ? 0 : red));               
              result[x, y].G = (byte)((green &gt; 255) 
                             ? 255 : ((green &lt; 0) ? 0 : green));
              result[x, y].B = (byte)((blue &gt; 255) 
                             ? 255 : ((blue &lt; 0) ? 0 : blue));
              result[x, y].A = (byte)((alpha &gt; 255) 
                             ? 255 : ((alpha &lt; 0) ? 0 : alpha));
          }
      }
  
      return result;
  }
  </code></pre>