Litwos
Litwos

Reputation: 1338

Float required in list output

I am trying to create a custom filter to run it with the generic filter from SciPy package.

scipy.ndimage.filters.generic_filter

The problem is that I don't know how to get the returned value to be a scalar, as it needs for the generic function to work. I read through these threads (bottom), but I can't find a way for my function to perform.

The code is this:

import scipy.ndimage as sc

def minimum(window):    
    list = []
    for i in range(window.shape[0]):
        window[i] -= min(window)
        list.append(window[i])        
    return list

test =  np.ones((10, 10)) * np.arange(10)

result = sc.generic_filter(test, minimum, size=3)

It gives the error:

cval, origins, extra_arguments, extra_keywords)
TypeError: a float is required

Scipy filter with multi-dimensional (or non-scalar) output

How to apply ndimage.generic_filter()

http://ilovesymposia.com/2014/06/24/a-clever-use-of-scipys-ndimage-generic_filter-for-n-dimensional-image-processing/

Upvotes: 1

Views: 263

Answers (2)

B. M.
B. M.

Reputation: 18668

If I understand, you want to substract each pixel the min of its 3-horizontal neighbourhood. It's not a good practice to do that with lists, because numpy is for efficiency( ~100 times faster ). The simplest way to do that is just :

test-sc.generic_filter(test, np.min, size=3)

Then the substraction is vectorized on the whole array. You can also do:

test-np.min([np.roll(test,1),np.roll(test,-1),test],axis=0)

10 times faster, if you accept the artefact at the border.

Upvotes: 2

agold
agold

Reputation: 6276

Using the example in Scipy filter with multi-dimensional (or non-scalar) output I converted your code to:

def minimum(window,out):
    list = []
    for i in range(window.shape[0]):
        window[i] -= min(window)
        list.append(window[i])        
    out.append(list)
    return 0

test =  np.ones((10, 10)) * np.arange(10)

result = []
sc.generic_filter(test, minimum, size=3, extra_arguments=(result,))

Now your function minimum outputs its result to the parameter out, and the return value is not used anymore. So the final result matrix contains all the results concatenated, not the output of generic_filter.

Edit 1: Using the generic_filter with a function that returns a scalar, a matrix of the same dimensions is returned. In this case however the lists are appended of each call by the filter which results in a larger matrix (100x9 in this case).

Upvotes: 1

Related Questions