问题描述
我无法准确理解反射模式如何处理我的数组.我有这个非常简单的数组:
I'm failing to understand exactly how the reflect mode handles my arrays. I have this very simple array:
import numpy as np
from scipy.ndimage.filters import uniform_filter
from scipy.ndimage.filters import median_filter
vector = np.array([[1.0,1.0,1.0,1.0,1.0],[2.0,2.0,2.0,2.0,2.0],[4.0,4.0,4.0,4.0,4.0],[5.0,5.0,5.0,5.0,5.0]])
print(vector)
[[ 1.1.1.1.1.][ 2. 2. 2. 2. 2. ][ 4. 4. 4. 4. 4. ][ 5. 5. 5. 5. 5.]]
[[ 1. 1. 1. 1. 1.] [ 2. 2. 2. 2. 2.] [ 4. 4. 4. 4. 4.] [ 5. 5. 5. 5. 5.]]
应用窗口大小为 3 的统一(均值)过滤器,我得到以下结果:
Applying a uniform (mean) filter with a window size of 3 I get the following:
filtered = uniform_filter(vector, 3, mode='reflect')
print(filtered)
[[ 1.33333333 1.33333333 1.33333333 1.33333333 1.33333333][ 2.33333333 2.33333333 2.33333333 2.33333333 2.33333333][ 3.66666667 3.66666667 3.66666667 3.66666667 3.66666667][ 4.66666667 4.66666667 4.66666667 4.66666667 4.66666667]]
[[ 1.33333333 1.33333333 1.33333333 1.33333333 1.33333333] [ 2.33333333 2.33333333 2.33333333 2.33333333 2.33333333] [ 3.66666667 3.66666667 3.66666667 3.66666667 3.66666667] [ 4.66666667 4.66666667 4.66666667 4.66666667 4.66666667]]
如果我尝试手动复制练习,我可以得到这个结果.原始矩阵为绿色,窗口为橙色,结果为黄色.白色是反射"的观察结果.
If I try to replicate the exercise by hand I can get to this result. Original matrix in green, window in orange and result in yellow. White are "reflected" observations.
结果是:
但是当我尝试将窗口大小设为 4 或 5 时,我无法复制结果.
But when I try a window size of 4 or 5 I fail to be able to replicate the results.
filtered = uniform_filter(vector, 4, mode='reflect')
print(filtered)
[[ 1.5 1.5 1.5 1.5 1.5][ 2. 2. 2. 2. 2. ][ 3. 3. 3. 3. 3. ][ 4. 4. 4. 4. 4. ]]
[[ 1.5 1.5 1.5 1.5 1.5] [ 2. 2. 2. 2. 2. ] [ 3. 3. 3. 3. 3. ] [ 4. 4. 4. 4. 4. ]]
手工制作:
我得到:
如果窗口大小为偶数,如何处理?但无论如何,如果我尝试复制大小为 5 的窗口和模式反射的结果,我也不能.即使我认为这种行为类似于大小 3.
How is the window handled if its size is even? But anyway, If I try to replicate the results of a window of size 5 and mode reflect I cant either. Even though I would think the behavior is analogous to that of size 3.
推荐答案
假设一个轴的数据为1 2 3 4 5 6 7 8
.下表显示了如何为每种模式扩展数据(假设 cval=0
):
Suppose the data in one axis is 1 2 3 4 5 6 7 8
. The following table shows how the data is extended for each mode (assuming cval=0
):
mode | Ext | Input | Ext
-----------+---------+------------------------+---------
'mirror' | 4 3 2 | 1 2 3 4 5 6 7 8 | 7 6 5
'reflect' | 3 2 1 | 1 2 3 4 5 6 7 8 | 8 7 6
'nearest' | 1 1 1 | 1 2 3 4 5 6 7 8 | 8 8 8
'constant' | 0 0 0 | 1 2 3 4 5 6 7 8 | 0 0 0
'wrap' | 6 7 8 | 1 2 3 4 5 6 7 8 | 1 2 3
对于一个均匀的窗口大小n
,考虑n+1
大小的窗口,然后不包括下边缘和右边缘.(可以使用 origin
参数更改窗口的位置.)
For an even window size n
, consider the window of size n+1
, and then don't include the lower and right edges. (The position of the window can be changed by using the origin
argument.)
这篇关于“反射"究竟是如何产生的?scipys ndimage 过滤器的模式有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!