M = A.shape[0]
Mh = M / 2
N = A.shape[1] / 2
A4D = A.reshape(-1, 2, N, 2).swapaxes(1, 2)
S = (np.diff(A4D.reshape(-1, 4)[:, (f.ravel() == 1)], 1) == 0).all(1)
out = (S.reshape(Mh, N)[:, (None), :, (None)] * f[:, (None), :]).reshape(M, -1)
Efficiently applying function over moving window numpy array