title: Convolve a 3D array with three kernels (x, y, z) in python
body: <p>I have a 3D image and three kernels k1, k2, k3 in the x, y and z direction. </p>
  
  <pre><code>img = np.random.rand(64, 64, 54) #three dimensional image
  k1 = np.array([0.114, 0.141, 0.161, 0.168, 0.161, 0.141, 0.114]) #the kernel along the 1st dimension
  k2 = k1 #the kernel along the 2nd dimension
  k3 = k1 #the kernel along the 3nd dimension
  </code></pre>
  
  <p>I can use <code>numpy.convolve</code> iteratively to calculate the convolution like this:</p>
  
  <pre><code>for i in np.arange(img.shape[0])
     for j in np.arange(img.shape[1])
        oneline=img[i,j,:]
        img[i,j,:]=np.convolve(oneline, k1, mode='same')
  
  for i in np.arange(img.shape[1])
     for j in np.arange(img.shape[2])
        oneline=img[:,i,j]
        img[:,i,j]=np.convolve(oneline, k2, mode='same') 
  
  for i in np.arange(img.shape[0])
     for j in np.arange(img.shape[2])
        oneline=img[i,:,j]
        img[i,:,j]=np.convolve(oneline, k3, mode='same') 
  </code></pre>
  
  <p>Is there an easier way to do it? Thanks.    </p>