本文介绍了如何使用scipy.ndimage.filters.gereric_filter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scipy.ndimage.filters.generic_filter从邻域计算加权和.邻居在某个时候会是可变的,但目前3x3是我正在努力的目标.到目前为止,这是我的位置:

I'm trying to use scipy.ndimage.filters.generic_filter to calculate a weighted sum from a neighborhood. The neighborhood will be variable at some point but for now 3x3 is what I'm working towards.So far this is where I am:

    def Func(a):
         a = np.reshape((3,3))
         weights = np.array([[0.5,.05,0.5],[0.5,1,0.5],[0.5,0.5,0.5]])
         a = np.multiply(a,weights)
         a = np.sum(a)
         return a

ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)

我从ndimage收到一条错误消息,提示"TypeError:需要一个浮点数",但我不知道它所指的是什么参数,它看起来与我见过的其他示例基本相同.

I get an error from ndimage saying 'TypeError: a float is required' but I don't know what argument it's referring to and it looks basically the same as other examples I've seen.

推荐答案

这对我有用.代码有几个小问题:

This worked for me. There were a couple little problems with the code:

import scipy.ndimage.filters
import numpy as np

Array = rand( 100,100 )

def Func(a):
    a = a.reshape((3,3))
    weights = np.array([[0.5,.05,0.5],[0.5,1,0.5],[0.5,0.5,0.5]])
    a = np.multiply(a,weights)
    a = np.sum(a)
    return a

out = scipy.ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)

您输入的a = np.reshape( (3,3) )不正确.那是你想要的吗?

You had a = np.reshape( (3,3) ) which isn't correct. Is that what you want?

[更新]

根据我们的讨论对此进行一些整理:

To clean this up a little based on our discussion:

import scipy.ndimage.filters
import numpy as np

Array = rand( 100,100 )

def Func(a):
    return np.sum( a * r_[0.5,.05,0.5, 0.5,1,0.5, 0.5,0.5,0.5] )

out = scipy.ndimage.filters.generic_filter(Array,Func,footprint=np.ones((3,3)),mode='constant',cval=0.0,origin=0.0)

这篇关于如何使用scipy.ndimage.filters.gereric_filter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:48