title: gausian blur image processing matrix multiplication
body: <p>I am trying to implement Image Filters such as Gausian Blur in python.</p>
  <p>I encountered a problem when I tried to optimise my code to allow a 5 by 5 kernel.</p>
  <p>My aim is to allow any nxn Gausian Kernel to be applied to an image.</p>
  <p>The current implementation</p>
  <pre><code>def gaussianOperator(roi, kernel):
      container = np.copy(roi)
      size = container.shape
  
      for i in range(size[0] - 2):
          for j in range(size[1] - 2):
              container[i+1][j+1] = np.sum(roi[i:i + 5, j:j + 5].dot(kernel))
              # container[i+1][j+1] = g                                      
              
      return container  
  </code></pre>
  <p>The error I am currently experiencing after trying to allow n by n kernels is this:</p>
  <blockquote>
  <p>shapes (5,4) and (5,5) not aligned: 4 (dim 1) != 5 (dim 0)</p>
  </blockquote>
  <p>The line where I am computing a sum and a dot product used to be like this:</p>
  <pre><code> gx = roi[i - 1][j - 1] * kX[0][0] + roi[i][j - 1] * kX[0][2] + roi[i + 1][j - 1] * kX[1][0] + roi[i - 1][j + 1] * kX[1][2] + roi[i][j + 1] * kX[2][0] + roi[i + 1][j + 1] * kX[2][2] 
  </code></pre>
  <p>This bit of code worked perfectly on a 3 by 3 kernel.</p>
  <p>The function to create the kernel is this:</p>
  <pre><code>def gkern(l=5, sig=1.):
      
      ax = np.linspace(-(l - 1) / 2., (l - 1) / 2., l)
      gauss = np.exp(-0.5 * np.square(ax) / np.square(sig))
      kernel = np.outer(gauss, gauss)
      
      return kernel / np.sum(kernel)
  </code></pre>
  <p>How can the code be modified?</p>