【问题标题】:Show points in a 3x3 box around points in an image array在图像数组中的点周围显示 3x3 框中的点
【发布时间】:2020-12-07 14:32:54
【问题描述】:

我有一个表示图像的数组,从该图像中我有另一个数组(称为点),它表示该图像的特定像素的位置。我想要做的是创建一个新的图像数组,其中每个像素的值都为零,而不是点数组中指定的像素周围的 3x3 像素框。

到目前为止,要获得另一个仅指定了我使用的单个像素的数组:

new_im_array[points] = im_array[points]

两个图像数组都是 500x500,点数组是 2xn,其中 n 是指定的像素数,包含的两个列表是 x 和 y 坐标。

我尝试了各种方法,但我对 python 还很陌生,我认为它变得比它应该考虑的操作应该有多简单要复杂。

提前感谢您的帮助。

【问题讨论】:

  • 这并不像你想象的那么简单。你能用scipy吗? scipy.ndimage.binary_dilation 就是为此而生的
  • 我可以用scipy,我去看看binary_dilation函数,谢谢

标签: python arrays python-3.x numpy


【解决方案1】:

你可以使用scipy.ndimage.binary_dilation:

from scipy.ndimage import binary_dilation

struct = np.ones((3,3)).astype(bool)
mask = np.zeros_like(im_array).astype(bool)
mask[points] = True
dilated_mask = binary_dilation(mask, structure = struct)
new_im_array = im_array * dilated_mask

如果您只想要像素周围的像素,但不包括它们,您也可以在创建后执行struct[1, 1] = False

【讨论】:

  • 效果很好,感谢您的帮助。
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
  • 2011-08-20
  • 2011-08-21
  • 1970-01-01
  • 2011-06-08
相关资源
最近更新 更多