您可以使用numpy.lib.stride_tricks.as_strided
获取图像的窗口视图:
import numpy as np
from numpy.lib.stride_tricks import as_strided
rows, cols = 500, 500
win_rows, win_cols = 5, 5
img = np.random.rand(rows, cols)
win_img = as_strided(img, shape=(rows-win_rows+1, cols-win_cols+1,
win_rows, win_cols),
strides=img.strides*2)
现在win_img[i, j]
是(win_rows, win_cols)
左上角位置的数组[i, j]
:
>>> img[100:105, 100:105]
array([[ 0.34150754, 0.17888323, 0.67222354, 0.9020784 , 0.48826682],
[ 0.68451774, 0.14887515, 0.44892615, 0.33352743, 0.22090103],
[ 0.41114758, 0.82608407, 0.77190533, 0.42830363, 0.57300759],
[ 0.68435626, 0.94874394, 0.55238567, 0.40367885, 0.42955156],
[ 0.59359203, 0.62237553, 0.58428725, 0.58608119, 0.29157555]])
>>> win_img[100,100]
array([[ 0.34150754, 0.17888323, 0.67222354, 0.9020784 , 0.48826682],
[ 0.68451774, 0.14887515, 0.44892615, 0.33352743, 0.22090103],
[ 0.41114758, 0.82608407, 0.77190533, 0.42830363, 0.57300759],
[ 0.68435626, 0.94874394, 0.55238567, 0.40367885, 0.42955156],
[ 0.59359203, 0.62237553, 0.58428725, 0.58608119, 0.29157555]])
但是,您必须小心,不要将图像的窗口视图转换为它的窗口副本:在我的示例中,这将需要 25 倍的存储空间。我相信 numpy 1.7 可以让您选择多个轴,因此您可以简单地执行以下操作:
>>> np.var(win_img, axis=(-1, -2))
我坚持使用 numpy 1.6.2,所以我无法测试它。如果我没记错我的数学,另一个选项可能会因窗口不太大而失败:
>>> win_mean = np.sum(np.sum(win_img, axis=-1), axis=-1)/win_rows/win_cols
>>> win_sqr_mean = np.sum(np.sum(win_img**2, axis=-1), axis=-1)/win_rows/win_cols
>>> win_var = win_sqr_mean - win_mean**2
现在win_var
是一个形状数组
>>> win_var.shape
(496, 496)
并win_var[i, j]
保持(5, 5)
窗口左上角的方差为[i, j]
。