18

我想用 datatyp uint8 添加 numpy 数组。我知道这些数组中的值可能大到足以发生溢出。所以我得到类似的东西:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
a += b

现在,a 是[150 250 44]。但是,我希望对于 uint8 来说太大的值而不是溢出,而不是 uint8 允许的最大值。所以我想要的结果是[150 250 255]

我可以使用以下代码得到这个结果:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
c = np.zeros((1,3), dtype=np.uint16)
c += a
c += b
c[c>255] = 255
a = np.array(c, dtype=np.uint8)

问题是,我的数组非常大,因此创建具有更大数据类型的第三个数组可能是内存问题。是否有一种快速且内存效率更高的方法来实现所描述的结果?

4

7 回答 7

8

您可以通过创建第三个 dtype uint8 数组和一个 bool 数组(它们一起比一个 uint16 数组更节省内存)来实现这一点。

np.putmask对于避免临时数组很有用。

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
c = 255 - b  # a temp uint8 array here
np.putmask(a, c < a, c)  # a temp bool array here
a += b

但是,正如@moarningsun 正确指出的那样, bool 数组占用的内存量与 uint8 数组相同,因此这不一定有用。可以通过避免在任何给定时间拥有多个临时数组来解决此问题:

a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
b = 255 - b  # old b is gone shortly after new array is created
np.putmask(a, b < a, b)  # a temp bool array here, then it's gone
a += 255 - b  # a temp array here, then it's gone

这种方法用内存消耗换取 CPU。


另一种方法是预先计算所有可能的结果,即 O(1) 额外内存(即与数组的大小无关):

c = np.clip(np.arange(256) + np.arange(256)[..., np.newaxis], 0, 255).astype(np.uint8)
c
=> array([[  0,   1,   2, ..., 253, 254, 255],
          [  1,   2,   3, ..., 254, 255, 255],
          [  2,   3,   4, ..., 255, 255, 255],
          ..., 
          [253, 254, 255, ..., 255, 255, 255],
          [254, 255, 255, ..., 255, 255, 255],
          [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

c[a,b]
=> array([150, 250, 255], dtype=uint8)

如果您的数组非常大,这种方法是最节省内存的。同样,它的处理时间很昂贵,因为它用较慢的 2dim 数组索引代替了超快速的整数加法。

解释它是如何工作的

上面数组的构造c使用了一个 numpy 广播技巧。将形状(N,)数组和(1,N)广播形状数组都添加为(N,N)-like,因此结果是所有可能和的 NxN 数组。然后,我们剪辑它。我们得到一个满足:c[i,j]=min(i+j,255)对于每个 i,j 的 2dim 数组。

然后剩下的就是使用花哨的索引来抓取正确的值。使用您提供的输入,我们可以访问:

c[( [100, 200, 250] , [50, 50, 50] )]

第一个索引数组指的是第一个暗淡,第二个指的是第二个暗淡。因此,结果是一个与索引数组 ( (N,)) 形状相同的数组,由 values 组成[ c[100,50] , c[200,50] , c[250,50] ]

于 2015-04-13T17:25:34.277 回答
4

怎么做

>>> a + np.minimum(255 - a, b)
array([150, 250, 255], dtype=uint8)

一般来说,你的数据类型的最大值是

np.iinfo(np.uint8).max
于 2015-04-13T17:23:46.833 回答
4

这是一种方法:

>>> a = np.array([100, 200, 250], dtype=np.uint8)
>>> b = np.array([50, 50, 50], dtype=np.uint8)
>>> a+=b; a[a<b]=255
>>> a
array([150, 250, 255], dtype=uint8)
于 2015-04-13T18:32:37.960 回答
2

您可以使用 Numba 真正做到这一点,例如:

import numba

@numba.jit('void(u1[:],u1[:])', locals={'temp': numba.uint16})
def add_uint8_inplace_clip(a, b):
    for i in range(a.shape[0]):
        temp = a[i] + b[i]
        a[i] = temp if temp<256 else 255

add_uint8_inplace_clip(a, b)

或者使用 Numexpr,例如:

import numexpr

numexpr.evaluate('where((a+b)>255, 255, a+b)', out=a, casting='unsafe')

Numexpr向上转换 uint8int32内部,然后将其放回uint8数组中。

于 2015-04-13T19:30:45.867 回答
2

OpenCV有这样一个函数:cv2.addWeighted

于 2019-02-08T01:38:40.197 回答
1
def non_overflowing_sum(a, b)
    c = np.uint16(a)+b
    c[np.where(c>255)] = 255
    return np.uint8( c )

它也交换内存,但我发现更优雅,临时 uint16 在返回转换后被释放

于 2018-06-02T15:11:56.687 回答
0

numpy中有一个功能:

numpy.nan_to_num(x)[source]

将 nan 替换为 0,将 inf 替换为有限数。

返回一个数组或标量,用零替换非数字 (NaN),用非常大的数字替换(正)无穷大,用非常小的(或负)数字替换负无穷大。

与 x 具有相同形状和 x 中元素的 dtype 的新数组,精度最高。

如果 x 不精确,则 NaN 被零替换,无穷大 (-infinity) 被适合输出 dtype 的最大(最小或最大负)浮点值替换。如果 x 不是不精确的,则返回 x 的副本。

我不确定它是否适用于 uint8,因为在输出中提到了浮点,但对于其他读者来说,它可能有用

于 2016-02-28T16:05:43.127 回答