2d convolution using python and numpy

前端 未结 7 915
鱼传尺愫
鱼传尺愫 2020-12-05 15:48

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          


        
相关标签:
7条回答
  • 2020-12-05 16:14

    Edit [Jan 2019]

    @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.

    Possible Problem

    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.

    Possible Solution

    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.

    0 讨论(0)
提交回复
热议问题