KilianK
KilianK

Reputation: 55

Convolve two same size matrices using numpy

I want to convolve two same-dimension matrices using numpy. According to the example on wikipedia this is a possible operation.

import numpy as np

f = np.array([[45, 60, 98],
              [46, 65, 98],
              [47, 65, 96]])

h = np.array([[ 0.1,  0.1,  0.1],
              [ 0.1,  0.2,  0.1],
              [ 0.1,  0.1,  0.1]])

print np.convolve(f,h)

console output

Why am I getting this error?

Upvotes: 1

Views: 1015

Answers (1)

Francesco Nazzaro
Francesco Nazzaro

Reputation: 2916

try:

import scipy.signal
import numpy as np

f = np.array([[45, 60, 98],
              [46, 65, 98],
              [47, 65, 96]])

h = np.array([[ 0.1,  0.1,  0.1],
              [ 0.1,  0.2,  0.1],
              [ 0.1,  0.1,  0.1]])

print scipy.signal.convolve2d(f, h, 'valid')

It should implement the convolution described in your image.

The output is np.array([[ 74.5]])

Upvotes: 3

Related Questions