[英]Extracting indices of 2D equal size patches from 2D numpy array
我有一個 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 ]])
然后我使用以下 function 從原始數組a
中獲取非重疊補丁。 我使用了之前提出的問題中的代碼。
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]
然后我調用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]]])
現在,我怎樣才能獲得兩個補丁中每個補丁相對於主數組a
的索引。 例如,第一個補丁的索引應為(1x1)
,第二個補丁的索引應為(1x4)
。 有什么方法可以提取這些補丁相對於原始數組a
中心索引。
您可以只使用np.where(X == [value1, value2])
來獲取值的索引
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.