如何制作 nxn 过滤器以在图像数组中执行自定义操作?
How to make a nxn filter to perform custom opertions in image array?
我正在处理各种图像,我想实现一种方法,但我不知道从哪里开始。所以我期待一些建议。
作为一种方法,我的想法是获取一个图像阵列,然后在 select 感兴趣区域之后,我们说 selectedROI 是 10x10 像素区域作为阵列。那么我如何制作一个大小为 2x5 的过滤器,在制作 2x5 过滤器之后如何在整个 selectedROI 中移动过滤器并比较每个 2x5 过滤器的最小值、最大值和平均值。
我已成功实现 select 计算 ROI 并将 selectedROI 打印为数组。
用简单的话来说,我想做的就是 select 一组像素,然后将这组像素与整个 selectedROI 中的另一组像素进行比较。怎么可能。
听起来你有一个形状为 (10, 10) 的像素数组,并且想要在数组中滑动一个形状为 (2, 5) 的 window,在每个位置执行一个操作。
NumPy 有操作来帮助解决这个问题(见这里:
https://numpy.org/devdocs/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html), however the documentation says sliding_window_view is generic and thus nonoptimal for your case. They suggest using scipy.ndimage operations instead, which I have shown examples of below. https://docs.scipy.org/doc/scipy/reference/reference/generated/scipy.ndimage.generic_filter.html
首先,我导入了包并定义了一个 10x10 数组和 2x5 过滤器大小。
>>> import numpy as np
>>> from scipy.ndimage import generic_filter
>>> selectedROI = np.array(range(100)).reshape((10, 10))
>>> selectedROI
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
>>> filter_size = (2, 5)
任何函数都可以通过 generic_filter(此处使用 np.average)以滑动 window 方式应用。选择变量 a、b、c、d 是因为 generic_filter 为输入数组中的所有位置生成值,包括 window 未完全落入输入数组的位置。有关切片中“或None”的解释,请参见此处:
numpy negative indexing a[:-0]
>>> a = filter_size[0]//2
>>> b = (1 - filter_size[0]%2) - (filter_size[0]//2)
>>> c = filter_size[1]//2
>>> d = (1 - filter_size[1]%2) - (filter_size[1]//2)
>>> generic_filter(selectedROI, np.average, filter_size)[a:b or None, c:d or None]
array([[ 7, 8, 9, 10, 11, 12],
[17, 18, 19, 20, 21, 22],
[27, 28, 29, 30, 31, 32],
[37, 38, 39, 40, 41, 42],
[47, 48, 49, 50, 51, 52],
[57, 58, 59, 60, 61, 62],
[67, 68, 69, 70, 71, 72],
[77, 78, 79, 80, 81, 82],
[87, 88, 89, 90, 91, 92]])
您可以为您的用例使用其他函数,例如 np.min 和 np.max,或者编写您自己的自定义函数,如下所示:How do I use scipy.ndimage.filters.gereric_filter?
>>> generic_filter(selectedROI, np.min, filter_size)[a:b or None, c:d or None]
array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55],
[60, 61, 62, 63, 64, 65],
[70, 71, 72, 73, 74, 75],
[80, 81, 82, 83, 84, 85]])
>>> generic_filter(selectedROI, np.max, filter_size)[a:b or None, c:d or None]
array([[14, 15, 16, 17, 18, 19],
[24, 25, 26, 27, 28, 29],
[34, 35, 36, 37, 38, 39],
[44, 45, 46, 47, 48, 49],
[54, 55, 56, 57, 58, 59],
[64, 65, 66, 67, 68, 69],
[74, 75, 76, 77, 78, 79],
[84, 85, 86, 87, 88, 89],
[94, 95, 96, 97, 98, 99]])
为清楚起见,这些示例使用了 2D 数组,但操作应扩展到 3D 像素数组。
我正在处理各种图像,我想实现一种方法,但我不知道从哪里开始。所以我期待一些建议。
作为一种方法,我的想法是获取一个图像阵列,然后在 select 感兴趣区域之后,我们说 selectedROI 是 10x10 像素区域作为阵列。那么我如何制作一个大小为 2x5 的过滤器,在制作 2x5 过滤器之后如何在整个 selectedROI 中移动过滤器并比较每个 2x5 过滤器的最小值、最大值和平均值。
我已成功实现 select 计算 ROI 并将 selectedROI 打印为数组。 用简单的话来说,我想做的就是 select 一组像素,然后将这组像素与整个 selectedROI 中的另一组像素进行比较。怎么可能。
听起来你有一个形状为 (10, 10) 的像素数组,并且想要在数组中滑动一个形状为 (2, 5) 的 window,在每个位置执行一个操作。
NumPy 有操作来帮助解决这个问题(见这里: https://numpy.org/devdocs/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html), however the documentation says sliding_window_view is generic and thus nonoptimal for your case. They suggest using scipy.ndimage operations instead, which I have shown examples of below. https://docs.scipy.org/doc/scipy/reference/reference/generated/scipy.ndimage.generic_filter.html
首先,我导入了包并定义了一个 10x10 数组和 2x5 过滤器大小。
>>> import numpy as np
>>> from scipy.ndimage import generic_filter
>>> selectedROI = np.array(range(100)).reshape((10, 10))
>>> selectedROI
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
>>> filter_size = (2, 5)
任何函数都可以通过 generic_filter(此处使用 np.average)以滑动 window 方式应用。选择变量 a、b、c、d 是因为 generic_filter 为输入数组中的所有位置生成值,包括 window 未完全落入输入数组的位置。有关切片中“或None”的解释,请参见此处: numpy negative indexing a[:-0]
>>> a = filter_size[0]//2
>>> b = (1 - filter_size[0]%2) - (filter_size[0]//2)
>>> c = filter_size[1]//2
>>> d = (1 - filter_size[1]%2) - (filter_size[1]//2)
>>> generic_filter(selectedROI, np.average, filter_size)[a:b or None, c:d or None]
array([[ 7, 8, 9, 10, 11, 12],
[17, 18, 19, 20, 21, 22],
[27, 28, 29, 30, 31, 32],
[37, 38, 39, 40, 41, 42],
[47, 48, 49, 50, 51, 52],
[57, 58, 59, 60, 61, 62],
[67, 68, 69, 70, 71, 72],
[77, 78, 79, 80, 81, 82],
[87, 88, 89, 90, 91, 92]])
您可以为您的用例使用其他函数,例如 np.min 和 np.max,或者编写您自己的自定义函数,如下所示:How do I use scipy.ndimage.filters.gereric_filter?
>>> generic_filter(selectedROI, np.min, filter_size)[a:b or None, c:d or None]
array([[ 0, 1, 2, 3, 4, 5],
[10, 11, 12, 13, 14, 15],
[20, 21, 22, 23, 24, 25],
[30, 31, 32, 33, 34, 35],
[40, 41, 42, 43, 44, 45],
[50, 51, 52, 53, 54, 55],
[60, 61, 62, 63, 64, 65],
[70, 71, 72, 73, 74, 75],
[80, 81, 82, 83, 84, 85]])
>>> generic_filter(selectedROI, np.max, filter_size)[a:b or None, c:d or None]
array([[14, 15, 16, 17, 18, 19],
[24, 25, 26, 27, 28, 29],
[34, 35, 36, 37, 38, 39],
[44, 45, 46, 47, 48, 49],
[54, 55, 56, 57, 58, 59],
[64, 65, 66, 67, 68, 69],
[74, 75, 76, 77, 78, 79],
[84, 85, 86, 87, 88, 89],
[94, 95, 96, 97, 98, 99]])
为清楚起见,这些示例使用了 2D 数组,但操作应扩展到 3D 像素数组。