Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

python - How exactly does the "reflect" mode for scipys ndimage filters work?

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.]]

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]]

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.

enter image description here

Result is:

enter image description here

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. ]]

Doing it by hand:

enter image description here

And I get:

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...