0

I'm confused by the indexing of the filter array for scipy.ndimage.convolve.

The simplest example, Identity works as I expected -- I get the same matrix back.

But I thought that a filter like:

0, 0, 0
0, 0, 1
0, 0, 0

Should give me the original matrix shifted one column left, with zero padding on the right edge.

Instead, the result is shifting the original matrix to the right, adding zero padding to the left column.

Similar results for the shifts in the other dimension. Up seems down, and left seems right.

This makes me think I've got some fundamental misunderstanding about numpy indexing (which is quite possible) or what a convolution does (I think it's the sum of the products of each cell of the filter times the corresponding cell of the matrix, with the filter centered like a bullseye on the relevant cell of the matrix)

from scipy import ndimage

a = np.array([[1, 2, 0, 0],
              [5, 3, 7, 4],
              [0, 5, 1, 7],
              [9, 3, 0, 0]])

#k = np.array([[0,0,0],[0,1,0],[0,0,0]])   #identity

#expect: each cell in a' = its right neighbor in a
#actual: each cell in a' = its LEFT neighbor in a
k = np.array([[0,0,0],[0,0,1],[0,0,0]])  

cvo = ndimage.convolve(a, k, mode='constant', cval=0.0)

cvo
user3556757
  • 3,469
  • 4
  • 30
  • 70
  • Does this answer your question? [Understanding NumPy's Convolve](https://stackoverflow.com/questions/20036663/understanding-numpys-convolve) – mkrieger1 Jul 27 '20 at 12:35

1 Answers1

1

There is just a little difference between convolution ans cross-correlation (in which you are a bit confused).

Given a matrix and a filter:

If you multiply the filter as it is to the (corresponding cells of) matrix and then take the sum, then it is cross-correlation.

If you multiply the mirror image (up and down) of filter to the (corresponding cells of) matrix and then take the sum, then it is convolution.

SciPy goes as per the original definition, hence it gives such (correct) results (which you were not expecting).

When it comes to Deep-Learning/ Neural Networks, in that, the convolutions used has an argument flip_filters = True (maybe with different names in different libraries). So, here (in Deep Learning) they actually does cross-correlation.

To know more: Convolution Vs Correlation, What's the difference between convolution and crosscorrelation?

Rahul Vishwakarma
  • 1,446
  • 2
  • 7
  • 22