您可以拆分每個2x2
子陣列,然後重新塑形,使每個加窗塊成爲2D
陣列中的一行。 從每一行中,使用boolean indexing
提取出對應於f==1
職位的元素。 然後,看看是否所有提取的元素沿着每一行是相同的,給我們一個面具。重塑後,使用此掩碼與f
乘以最終的二進制輸出。
因此,假設f
作爲濾波器陣列和A
作爲數據陣列,量化執行遵循這樣的步驟是這樣的 -
# Setup size parameters
M = A.shape[0]
Mh = M/2
N = A.shape[1]/2
# Reshape input array to 4D such that the last two axes represent the
# windowed block at each iteration of the intended operation
A4D = A.reshape(-1,2,N,2).swapaxes(1,2)
# Determine the binary array whether all elements mapped against 1
# in the filter array are the same elements or not
S = (np.diff(A4D.reshape(-1,4)[:,f.ravel()==1],1)==0).all(1)
# Finally multiply the binary array with f to get desired binary output
out = (S.reshape(Mh,N)[:,None,:,None]*f[:,None,:]).reshape(M,-1)
樣品運行 -
1)輸入:
In [58]: A
Out[58]:
array([[1, 1, 1, 1, 2, 1],
[1, 1, 3, 1, 2, 2],
[1, 3, 3, 3, 2, 3],
[3, 3, 3, 3, 3, 1]])
In [59]: f
Out[59]:
array([[0, 1],
[1, 1]])
2)中間體輸出:
In [60]: A4D
Out[60]:
array([[[[1, 1],
[1, 1]],
[[1, 1],
[3, 1]],
[[2, 1],
[2, 2]]],
[[[1, 3],
[3, 3]],
[[3, 3],
[3, 3]],
[[2, 3],
[3, 1]]]])
In [61]: S
Out[61]: array([ True, False, False, True, True, False], dtype=bool)
3)最終輸出:
In [62]: out
Out[62]:
array([[0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0],
[1, 1, 1, 1, 0, 0]])
相同的方法可以用做'np.kron(〜np.any(np.diff(A4D [...,F == 1]), -1),f)';-) – 2016-05-30 09:22:31
@morningsun啊是在最後一步乘法的'kron'!謝謝! – Divakar