I know the equivalent functions of conv2
and corr2
of MATLAB are scipy.signal.correlate
and scipy.signal.convolve
. But the function imfilter
has the property of dealing with the outside the bounds of the array. Like as symmetric
, replicate
and circular
. Can Python do that things
Just to add some solid code, I wanted imfilter(A, B)
equivalent in python for simple 2-D image and filter (kernel). I found that following gives same result as MATLAB:
import scipy.ndimage
import numpy as np
scipy.ndimage.correlate(A, B, mode='constant').transpose()
For given question, this will work:
scipy.ndimage.correlate(A, B, mode='nearest').transpose()
Note that for some reason MATLAB returns transpose of the expected answer.
See documentation here for more options.
Edit 1:
There are more options given by MATLAB as documented here. Specifically, if we wish to use the 'conv'
option, we have MATLAB code (for example):
imfilter(x, f, 'replicate', 'conv')
This has python equivalence with:
scipy.ndimage.convolve(x, f, mode='nearest')
Note the 'replicate'
in MATLAB is same as 'nearest'
in SciPy in python.
Using the functions scipy.ndimage.filters.correlate
and scipy.ndimage.filters.convolve
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With