1

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?

2
  • 2
    For 2D conv, look into scipy's version. Numpy's one is only for 1D. Commented Apr 5, 2016 at 10:02
  • makes sense, but scipy.convolve(...) gives the same output. So the problem lies somewhere else. Commented Apr 5, 2016 at 10:06

1 Answer 1

3

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]])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.