Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The equivalent function of Matlab imfilter in Python

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

like image 305
Samuel Avatar asked Mar 03 '14 08:03

Samuel


2 Answers

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.

like image 162
KrnTneJa Avatar answered Sep 28 '22 01:09

KrnTneJa


Using the functions scipy.ndimage.filters.correlate and scipy.ndimage.filters.convolve

like image 32
Samuel Avatar answered Sep 28 '22 02:09

Samuel