Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does scipy.signal.convolve2d calculate? [duplicate]

Tags:

python

scipy

I am currently a bit confused by the output of

#!/usr/bin/env python

import scipy.signal

image = [[1, 2, 3, 4, 5, 6, 7],
         [8, 9, 10, 11, 12, 13, 14],
         [15, 16, 17, 18, 19, 20, 21],
         [22, 23, 24, 25, 26, 27, 28],
         [29, 30, 31, 32, 33, 34, 35],
         [36, 37, 38, 39, 40, 41, 42],
         [43, 44, 45, 46, 47, 48, 49]]

filter_kernel = [[-1, 1, -1],
                 [-2, 3, 1],
                 [2, -6, 0]]

res = scipy.signal.convolve2d(image, filter_kernel,
                              mode='same', boundary='fill', fillvalue=0)
print(res)

It was

[[  -2   -8   -7   -6   -5   -4   28]
 [   3   -7  -10  -13  -16  -19   14]
 [ -18  -28  -31  -34  -37  -40    0]
 [ -39  -49  -52  -55  -58  -61  -14]
 [ -60  -70  -73  -76  -79  -82  -28]
 [ -81  -91  -94  -97 -100 -103  -42]
 [-101  -61  -63  -65  -67  -69  -57]]

I expected the top left element to be 3*1 + 1*2 + (-6) *8 + 0*9 = -43 (ommitting the padded zeros).

I thought this would expand the matrix image \in R^{7x7} to R^{9x9} by adding one 0 to the left / right and top / bottom. Then I thought the filter_kernel would be calculated by "sliding" it over the image. At each position, the numbers from the image are point-wise multiplied with the numbers from the kernel. The nine products are the summed up and written into res.

However, it is -2. Obviously, something different happens.

like image 258
Martin Thoma Avatar asked Jan 12 '17 12:01

Martin Thoma


People also ask

How many examples of scipysignal convolve2d are there?

Python convolve2d - 30 examples found. These are the top rated real world Python examples of scipysignal.convolve2d extracted from open source projects. You can rate examples to help us improve the quality of examples.

What are the different modes of convolution in SciPy signal?

I'm using scipy.signal.convolve2d instead of my own implementation for performance reasons. Checking the documentation, it mentions three different modes: full, valid and same. I've figured out, just by comparing results and shapes, that the valid mode corresponds to the operation the previous gif shows and that every place refers as "convolution".

What is the output of the convolution in in1 and in2?

Convolve in1 and in2 with output size determined by mode, and boundary conditions determined by boundary and fillvalue. First input. Second input. Should have the same number of dimensions as in1. The output is the full discrete linear convolution of the inputs.

How do you convolve two 2-dimensional arrays?

Convolve two 2-dimensional arrays. Convolve in1 and in2 with output size determined by mode, and boundary conditions determined by boundary and fillvalue. First input. Second input. Should have the same number of dimensions as in1. The output is the full discrete linear convolution of the inputs.


1 Answers

Convolution reverses the direction of one of the functions it works on. Check The definition on Wikipedia: one function is parameterized with τ and the other with -τ. The same applies to 2D convolution.

You need to mirror the kernel to get the expected resut:

filter_kernel = [[0, -6, 2],
                 [1, 3, -2],
                 [-1, 1, -1]]

res = scipy.signal.convolve2d(image, filter_kernel,
                              mode='same', boundary='fill', fillvalue=0)
print(res[0, 0])
# -43
like image 130
MB-F Avatar answered Oct 19 '22 17:10

MB-F