我有两个相同形状的二维numpy数组,一个包含数据,一个类型为'ubyte',每个像素存储位标志。我想访问数据数组中的每个像素,在位标志数组中有一个特定的标志。
我可以在任意一个数组中的每个像素上迭代,并使用多个索引来获得位标志和像素的值。
it = np.nditer(array, flags=['multi_index'])
while not it.finished:
if bitflags[it.multi_index] & FLAG:
do_something(array[it.multi_index])
it.iternext()由于大多数像素没有设置相应的位标志,所以我更希望找到具有给定位标志的所有像素(例如,使用numpy.where(bitflags & FLAG)并仅在这些像素上迭代--类似于
pixels = np.where(bitflags & FLAG)
for pixel in array[pixels]:
do_something()是否仍然可以获得原始数组的索引,以便在do_something()中使用它们?
发布于 2014-10-31 11:50:08
不完全确定我是否正确地理解了你的问题,但你是否在寻找:
pixels, = np.where(bitflags & FLAG)
for i, pixel in zip(pixels, array[pixels]):
do_something(i, pixel)https://stackoverflow.com/questions/26673570
复制相似问题