Tho*_*mas 16 python numpy image-processing integer-overflow numpy-ndarray
我想用datatyp uint8添加numpy数组.我知道这些数组中的值可能足够大,可以发生溢出.所以我得到类似的东西:
a = np.array([100, 200, 250], dtype=np.uint8)
b = np.array([50, 50, 50], dtype=np.uint8)
a += b
Run Code Online (Sandbox Code Playgroud)
现在,是的[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)
Run Code Online (Sandbox Code Playgroud)
问题是,我的数组非常大,因此创建具有更大数据类型的第三个数组可能是内存问题.是否有一种快速且更有效的内存方式来实现所描述的结果?
小智 9
这是一个方法:
>>> 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)
Run Code Online (Sandbox Code Playgroud)
你可以通过创建第三个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
Run Code Online (Sandbox Code Playgroud)
但是,正如@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
Run Code Online (Sandbox Code Playgroud)
此方法用于交换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)
Run Code Online (Sandbox Code Playgroud)
如果阵列非常大,这种方法的内存效率最高.同样,它在处理时间上很昂贵,因为它用较慢的2dim-array索引替换了超快整数加法.
解释它的工作原理
c上面阵列的构造使用了一个numpy广播技巧.添加形状(N,)和形状数组的数组(1,N)都是(N,N)类似的,因此结果是所有可能总和的NxN数组.然后,我们剪辑它.我们得到一个2dim数组,满足:c[i,j]=min(i+j,255)每个i,j.
然后剩下的就是使用花式索引来获取正确的值.使用您提供的输入,我们访问:
c[( [100, 200, 250] , [50, 50, 50] )]
Run Code Online (Sandbox Code Playgroud)
第一个索引数组指第一个暗淡,第二个指向第二个暗淡.因此,结果是一个与索引数组((N,))相同形状的数组,由值组成[ c[100,50] , c[200,50] , c[250,50] ].
怎么样做
>>> a + np.minimum(255 - a, b)
array([150, 250, 255], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)
一般来说,获取数据类型的最大值
np.iinfo(np.uint8).max
Run Code Online (Sandbox Code Playgroud)