将一堆图像居中

Chr*_*rry 1 python arrays numpy computer-vision

我有一些要居中的图像的numpy数组(减去平均值并除以标准差)。我可以这样简单吗?

# x is a np array
img_mean = x.mean(axis=0)
img_std = np.std(x)
x = (x - img_mean) / img_std
Run Code Online (Sandbox Code Playgroud)

Fin*_*ood 5

我不认为这是您要尝试做的。
假设我们有一个像这样的数组:

In [2]: x = np.arange(25).reshape((5, 5))

In [3]: x
Out[3]: 
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])
Run Code Online (Sandbox Code Playgroud)

x.mean(axis=0) 计算每列(轴0)的平均值:

In [4]: x.mean(axis=0)
Out[4]: array([ 10.,  11.,  12.,  13.,  14.])
Run Code Online (Sandbox Code Playgroud)

从原始x数组中减去每个值,再减去其列的平均值:

In [5]: x - x.mean(axis=0)
Out[5]: 
array([[-10., -10., -10., -10., -10.],
       [ -5.,  -5.,  -5.,  -5.,  -5.],
       [  0.,   0.,   0.,   0.,   0.],
       [  5.,   5.,   5.,   5.,   5.],
       [ 10.,  10.,  10.,  10.,  10.]])
Run Code Online (Sandbox Code Playgroud)

如果我们没有为指定轴x.mean,则整个数组将被使用:

In [6]: x.mean(axis=None)
Out[6]: 12.0
Run Code Online (Sandbox Code Playgroud)

这就是你一直在做与x.std()所有的时间,因为两个np.stdnp.mean默认轴None
这可能是您想要的:

In [7]: x - x.mean()
Out[7]: 
array([[-12., -11., -10.,  -9.,  -8.],
       [ -7.,  -6.,  -5.,  -4.,  -3.],
       [ -2.,  -1.,   0.,   1.,   2.],
       [  3.,   4.,   5.,   6.,   7.],
       [  8.,   9.,  10.,  11.,  12.]])

In [8]: (x - x.mean()) / x.std()
Out[8]: 
array([[-1.6641005, -1.5254255, -1.3867504, -1.2480754, -1.1094003],
       [-0.9707253, -0.8320502, -0.6933752, -0.5547002, -0.4160251],
       [-0.2773501, -0.1386750,  0.       ,  0.1386750,  0.2773501],
       [ 0.4160251,  0.5547002,  0.6933752,  0.8320502,  0.9707253],
       [ 1.1094003,  1.2480754,  1.3867504,  1.5254255,  1.6641005]])
Run Code Online (Sandbox Code Playgroud)