我想从二进制图像(1和0)中提取局部傅立叶模式,所以如果图像是,那么,(1000,1000),我想进行傅里叶变换窗户(30,30)。如何在不制作数据副本的情况下制作此窗口?
答案 0 :(得分:1)
这是as_strided
:
>>> import numpy as np
>>> from numpy.lib.stride_tricks import as_strided
>>> a = np.arange(4*4).reshape(4,4)
>>> ws = (2,2) # shape of the elements on which you want to perform the operation (e.g. Fourier Transform)
>>> a_view = as_strided(a, shape=(a.shape[0] - ws[0] + 1, a.shape[1] - ws[1] + 1) + ws, strides=a.strides*2)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> a_view[0,0]
array([[0, 1],
[4, 5]])
>>> a_view[0,1] # move one window to the right
array([[1, 2],
[5, 6]])
>>> a_view[1,1] # move one window down from previous
array([[ 5, 6],
[ 9, 10]])
这个blog entry显示相同,但如果你想要另一个例子,那就很好学习。
使用该视图,您可以轻松地operate on the last two axes数组(默认情况下,大多数以2维操作的numpy和scipy函数在最后两个轴上工作):
>>> np.fft.fft2(a_view)
>>> a_view.sum(axis=(-2,-1)) # more visually understandable example (added in numpy version 1.7.0)
array([[10, 14, 18],
[26, 30, 34],
[42, 46, 50]])