What is the difference between scipy.ndimage.filters.convolve and scipy.signal.convolve?

As I understand it, these methods are implemented as C functions in the corresponding DLLs, and it seems that the ndimage version is faster (no implementation uses parallel code, such as blas or MKL calls).

Also, when I tried to verify that they were returning the same results by running the following code, the equality statement failed. I could not understand from the documentation what exactly the functional differences between the two methods should be (the documentation is not very clear about what 0 means relative to the location of the kernel, or from the examples, I deduced it in the center, but I could be wrong).

 from numpy import random, allclose from scipy.ndimage.filters import convolve as convolveim from scipy.signal import convolve as convolvesig a = random.random((100, 100, 100)) b = random.random((10,10,10)) conv1 = convolveim(a,b, mode = 'constant') conv2 = convolvesig(a,b, mode = 'same') assert(allclose(conv1,conv2)) 

Thanks!

+6
source share
2 answers

The two functions have different conventions for working with the border. To make your functions functionally the same, add the argument origin=-1 or origin=(-1,-1,-1) to the convolveim call:

 In [46]: a = random.random((100,100,100)) In [47]: b = random.random((10,10,10)) In [48]: c1 = convolveim(a, b, mode='constant', origin=-1) In [49]: c2 = convolvesig(a, b, mode='same') In [50]: allclose(c1,c2) Out[50]: True 

Move origin when dimensions b even. When they are odd, the functions are consistent when the default value origin=0 :

 In [88]: b = random.random((11,11,11)) In [89]: c1 = convolveim(a, b, mode='constant') In [90]: c2 = convolvesig(a, b, mode='same') In [91]: allclose(c1,c2) Out[91]: True 
+9
source

There is a very important difference. The implementation in the image package seems to be a typical limited version used in image processing to achieve the β€œsame” image size after convolution. Thus, it matches the β€œsame” option in the signal processing package if we use mode = 'constant', as in the above examples. It seems that the signal processing package implements a real strict definition of the convolution operator. Perhaps for this reason it is slower. Find attached examples with completely different results.

 In [13]: a=array([[1,2,1]]) In [14]: b=array([[1],[2],[1]]) In [17]: convolveim(a,b) Out[17]: array([[4, 8, 4]]) In [18]: convolveim(b,a) Out[18]: array([[4], [8], [4]]) In [19]: convolvesig(a,b) Out[19]: array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) In [20]: convolvesig(b,a) Out[20]: array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) 

Note that the implementation of the signal processing package is conditional, as expected for proper convolution. However, the implementation in the image package is not and provides a solution with the same parameters as the first parameter.

+3
source

Source: https://habr.com/ru/post/946396/


All Articles