0

I'm using scipy.signal.convolve to apply a simple filter to a grayscale picture My inputs are as follows:
kk -> filer (2x2)
im -> image (500x800) opened via pillow

>>> from scipy.signal import convolve as cv
>>> kk
[[1, 2], [1, 2]]
>>> im.size
(500, 800)
>>> cvRes = cv(im,kk,'same')

When I apply the convolution, I was expecting the result to be in a shape of (500,800), i.e. same as the input image (im), but the results are in shape of (800,500).

>>> cvRes = cv(im,kk,'same')
>>> cvRes.shape
(800, 500)

I'm a bit confused regarding this output, I think I maybe missing something or misunderstanding how the library is supposed to work.
Appreciate any help in regards to how to get a non flipped output and if I can just flip the x/y when trying to get the value of single pixel?

Thanks!

mhk777
  • 83
  • 1
  • 9

1 Answers1

1

This is a side effect of PIL. A PIL image with size (800,500) has 500 rows of 800 columns. When that becomes a numpy array, it has a shape of (500,800). So, it's not that the array is being transposed, it's that the two modules number the axes differently.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Thanks a ton, that explains it. Would transposing the image before convolution be a good idea? – mhk777 May 12 '21 at 04:11
  • 1
    No. Remember, these aren't transposed. It's 500 rows of 800 columns. PIL notates that as (800,500) because that's how we think of bitmaps. Numpy notates that as (500,800). You just need to make sure your convolution matrix is the same: rows of columns, not (x,y). – Tim Roberts May 12 '21 at 05:40
  • so just using a flipped pair of coordinates will do the trick when reading the output np array, i.e use `cvRes[y,x]` instead of `cvRes[x,y]` ... if I got your point right, that is :) – mhk777 May 13 '21 at 02:54
  • 1
    RIght. In numpy, the first axis is "rows", which is the y coordinate in image thinking. – Tim Roberts May 13 '21 at 04:07
  • thanks a lot Tim, really appreciate your help :) – mhk777 May 13 '21 at 18:11