图像处理练习和 np.array 索引



>我试图理解在这个网站上找到的练习: http://scipy-lectures.org/intro/summary-exercises/answers_image_processing.html

终点是从显微镜下观察的沙子图片(sand_op(中去除小于10像素的颗粒:

sand_labels, sand_nb = ndimage.label(sand_op)
sand_areas = np.array(ndimage.sum(sand_op, sand_labels, np.arange(sand_labels.max()+1)))
mask = sand_areas > 100
remove_small_sand = mask[sand_labels.ravel()].reshape(sand_labels.shape)
plt.imshow(remove_small_sand)

我的问题是关于这部分:

mask[sand_labels.ravel()]

其中掩码是一个充满布尔值的形状 (155,( 的 np.array 其中sand_labels是形状的 np.array (883, 1024(

我不明白我们如何用扁平化的形状 (904192,( 的 np.array 来索引 np.array 掩码,也就是说要大得多!为什么我们没有像"..轴 0 的越界"?

提前感谢!

格温诺莱

我无权确认这个问题,但你可以根据需要多次索引数组,只要你的索引没有超出数组的范围。下面是一个示例来澄清它:

mask=np.array([True, False])
#mask[0]=True
#mask[1]=False
print(mask[[0,0,0,0,1]])
#[ True  True  True  True False]
#this will throw error because index 2 does not exist in mask and NOT because it calls elements too many times.    
print(mask[[0,0,2]])
#IndexError: index 2 is out of bounds for axis 0 with size 2

最新更新