I am trying to perform a 2d convolution in python using numpy
I have a 2d array as follows with kernel H_r for the rows and H_c for the columns
data
@Tashus comment bellow is correct, and @dudemeister's answer is thus probably more on the mark. The function he suggested is also more efficient, by avoiding a direct 2D convolution and the number of operations that would entail.
I believe you are doing two 1d convolutions, the first per columns and the second per rows, and replacing the results from the first with the results of the second.
Notice that numpy.convolve with the 'same'
argument returns an array of equal shape to the largest one provided, so when you make the first convolution you already populated the entire data
array.
One good way to visualize your arrays during these steps is to use Hinton diagrams, so you can check which elements already have a value.
You can try to add the results of the two convolutions (use data[:,c] += ..
instead of data[:,c] =
on the second for
loop), if your convolution matrix is the result of using the one dimensional H_r
and H_c
matrices like so:
Another way to do that would be to use scipy.signal.convolve2d with a 2d convolution array, which is probably what you wanted to do in the first place.