[英]Extracting indices of 2D equal size patches from 2D numpy array
I have a numpy array as follows.我有一个 numpy 数组,如下所示。
a = np.random.rand(5,6)
a
Out[52]:
array([[0.08649968, 0.24360955, 0.27972609, 0.21566217, 0.00194021,
0.69750779],
[0.09327379, 0.7579194 , 0.34634515, 0.78285156, 0.50981823,
0.17256468],
[0.7386456 , 0.78608358, 0.80615647, 0.72471626, 0.14825363,
0.62044455],
[0.32171325, 0.10889609, 0.56453828, 0.41675939, 0.09400235,
0.32373844],
[0.52850344, 0.0783796 , 0.74144658, 0.2363739 , 0.24535204,
0.9930051 ]])
Then I used the following function to obtain non overlapped patches from the original array a
.然后我使用以下 function 从原始数组
a
中获取非重叠补丁。 I used the code from the previously asked question .我使用了之前提出的问题中的代码。
def select_random_windows(arr, number_of_windows, window_size):
# Get sliding windows
w = view_as_windows(arr,window_size, 3)
# Store shape info
m,n = w.shape[:2]
# Get random row, col indices for indexing into windows array
lidx = np.random.choice(m*n,number_of_windows,replace=False)
r,c = np.unravel_index(lidx,(m,n))
# If duplicate windows are allowed, use replace=True or np.random.randint
# Finally index into windows and return output
return w[r,c]
Then I called select_random_windows
to obtain the following two non overlapped patches, each of size 3x3
.然后我调用
select_random_windows
来获得以下两个不重叠的补丁,每个补丁的大小为3x3
。
select_random_windows(a, number_of_windows=2, window_size=(3,3))
Out[54]:
array([[[0.08649968, 0.24360955, 0.27972609],
[0.09327379, 0.7579194 , 0.34634515],
[0.7386456 , 0.78608358, 0.80615647]],
[[0.21566217, 0.00194021, 0.69750779],
[0.78285156, 0.50981823, 0.17256468],
[0.72471626, 0.14825363, 0.62044455]]])
Now, how can I get the index of each of the two patches with respect to the main array a
.现在,我怎样才能获得两个补丁中每个补丁相对于主数组
a
的索引。 For instance, the first patch should have index of (1x1)
and second patch should have index of (1x4)
.例如,第一个补丁的索引应为
(1x1)
,第二个补丁的索引应为(1x4)
。 Is there any way I can extract these center indices of patches with respect to original array a
.有什么方法可以提取这些补丁相对于原始数组
a
中心索引。
You can just use np.where(X == [value1, value2])
to get the index of values您可以只使用
np.where(X == [value1, value2])
来获取值的索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.