我有一个二维Numpy数组(Uint16),如何将某个屏障(例如255)以上的所有值截断到该屏障?其他值必须保持不变。使用嵌套循环似乎既低效又笨拙。
发布于 2011-08-14 03:38:34
import numpy as np
my_array = np.array([[100, 200], [300, 400]],np.uint16)
my_array[my_array > 255] = 255输出将是
array([[100, 200],
[255, 255]], dtype=uint16)发布于 2011-08-14 03:51:56
实际上,有一个特定的方法,'clip':
import numpy as np
my_array = np.array([[100, 200], [300, 400]],np.uint16)
my_array.clip(0,255) # clip(min, max)输出:
array([[100, 200],
[255, 255]], dtype=uint16)https://stackoverflow.com/questions/7052776
复制相似问题