2
votes

I want to find a function that applies 2d filter or 3d filter in python. the function should receive the filter function and the data. Like the functions filter2 and imfilter in Matlab, or like the function scipy.ndimage.gaussian_filter() in python, that can deal with N-dimensions but is only for gaussian filter (and I want the filter to be an argument)

I think I can write a function that applies a filter using multidimensional convolution (scipy.ndimage.convolve()), but I was not successful for 1d filters. The code I used for 1d filtering: signal.convolve(signal, filter_, mode='same', method='auto'), and it gave a different result than the function signal.filtfilt(filter_, 1, signal, padlen=len(signal)-1).

Is there a function that applies N-D filters? and if not, how can I use the convolution function?

thanks!

1

1 Answers

1
votes

scipy.ndimage.generic_filter(input, function, size, footprint) can be helpful.

Here is the document scipy.ndimage.generic_filter and usage tutorial.

You can use footprint or size to design the filter range. For example, size = 3 or footprint = [[1, 1, 1], [1, 1, 1], [1, 1, 1]] sets the 3*3*3 array with current point as the core. function takes it as 1-D array and returns a scalar that you want. You can define function(array) as your customized filter. The generic filter will traverse input with the filter and deal with the boundaries with mode = 'reflect by default.