将一堆图像居中 [英] Centering a Numpy array of images

查看:90
本文介绍了将一堆图像居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些要中心化的图像阵列(减去均值并除以标准差).我可以这样简单吗?

I have some numpy arrays of images that I want to center (subtract the mean and divide by the standard deviation). Can I simply do it like this?

# x is a np array
img_mean = x.mean(axis=0)
img_std = np.std(x)
x = (x - img_mean) / img_std

推荐答案

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

I don't think this is what you are trying to do.
Let's say we have an array like this:

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

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

x.mean(axis=0) calculates the mean value for each column (axis 0):

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

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

Subtracted from our original x array, each value gets subtracted by its column's mean value:

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

如果未为x.mean指定轴,则整个数组将被获取:

If we don't specify an axis for x.mean, the whole array is being taken:

In [6]: x.mean(axis=None)
Out[6]: 12.0

这就是您一直在使用x.std()的方式,因为对于

This is what you've been doing with x.std() all the time, since for both np.std and np.mean the default axis is None.
This might be what you want:

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

这篇关于将一堆图像居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆