【发布时间】:2017-02-15 05:22:01
【问题描述】:
我有一个对应于 RGB 图像的 3 维 Numpy 数组。我需要从中创建一个二维 Numpy 数组,这样如果 R、G 或 B 通道中的任何像素为 1,则二维数组中的相应像素为 255。
我知道如何在 Numpy 数组上使用列表推导之类的东西,但结果与原始数组的形状相同。我需要新的形状是二维的。
【问题讨论】:
标签: python-3.x numpy
我有一个对应于 RGB 图像的 3 维 Numpy 数组。我需要从中创建一个二维 Numpy 数组,这样如果 R、G 或 B 通道中的任何像素为 1,则二维数组中的相应像素为 255。
我知道如何在 Numpy 数组上使用列表推导之类的东西,但结果与原始数组的形状相同。我需要新的形状是二维的。
【问题讨论】:
标签: python-3.x numpy
好的,假设您希望输出像素为 0,而不应为 255,并且您的输入为 MxNx3。
RGB = RGB == 1 # you can skip this if your original (RGB) contains only 0's and 1's anyway
out = np.where(np.logical_or.reduce(RGB, axis=-1), 255, 0)
【讨论】:
一种方法是在第三个暗淡处使用any(),然后乘以255,这样布尔值就会自动放大为int类型,就像这样-
(img==1).any(axis=2)*255
示例运行 -
In [19]: img
Out[19]:
array([[[1, 8, 1],
[2, 4, 7]],
[[4, 0, 6],
[4, 3, 1]]])
In [20]: (img==1).any(axis=2)*255
Out[20]:
array([[255, 0],
[ 0, 255]])
运行时测试-
In [45]: img = np.random.randint(0,5,(1024,1024,3))
# @Paul Panzer's soln
In [46]: %timeit np.where(np.logical_or.reduce(img==1, axis=-1), 255, 0)
10 loops, best of 3: 22.3 ms per loop
# @nanoix9's soln
In [47]: %timeit np.apply_along_axis(lambda a: 255 if 1 in a else 0, 0, img)
10 loops, best of 3: 40.1 ms per loop
# Posted soln here
In [48]: %timeit (img==1).any(axis=2)*255
10 loops, best of 3: 19.1 ms per loop
此外,我们可以转换为np.uint8,然后将其与255 相乘以进一步提升性能 -
In [49]: %timeit (img==1).any(axis=2).astype(np.uint8)*255
100 loops, best of 3: 18.5 ms per loop
还有更多,如果我们沿第三个暗角处理单个切片 -
In [68]: %timeit ((img[...,0]==1) | (img[...,1]==1) | (img[...,2]==1))*255
100 loops, best of 3: 7.3 ms per loop
In [69]: %timeit ((img[...,0]==1) | (img[...,1]==1) | (img[...,2]==1)).astype(np.uint8)*255
100 loops, best of 3: 5.96 ms per loop
【讨论】:
使用apply_along_axis。例如
In [28]: import numpy as np
In [29]: np.random.seed(10)
In [30]: img = np.random.randint(2, size=12).reshape(3, 2, 2)
In [31]: img
Out[31]:
array([[[1, 1],
[0, 1]],
[[0, 1],
[1, 0]],
[[1, 1],
[0, 1]]])
In [32]: np.apply_along_axis(lambda a: 255 if 1 in a else 0, 0, img)
Out[32]:
array([[255, 255],
[255, 255]])
详情请参阅doc of numpy。
【讨论】: