2013-12-17 26 views
0

跟进这个问题(和jorgeca的答案): Fast Way to slice image into overlapping patches and merge patches to image我想偏移添加到patchified数组的索引,即:偏移添加到numpy的阵列的指数

A = np.arange(W*H).reshape(H,W) 
P = patchify(A,(X,Y)) 

假设X,Y是奇数,P的大小将等于W-X + 1,H-Y + 1,因此以P [0,0]为中心的像素实际上对应于A [(Y-1)/ 2,( X-1)/ 2]。

有没有什么办法可以抵消(不复制任何数据)P的指标有完美的对应关系?

以供参考,在这里是对现有patchify功能:

def patchify(img, patch_shape): 
    img = np.ascontiguousarray(img) # won't make a copy if not needed 
    X, Y = img.shape 
    x, y = patch_shape 
    shape = ((X-x+1), (Y-y+1), x, y) # number of patches, patch_shape 
    # The right strides can be thought by: 
    # 1) Thinking of `img` as a chunk of memory in C order 
    # 2) Asking how many items through that chunk of memory are needed when indices 
    # i,j,k,l are incremented by one 
    strides = img.itemsize*np.array([Y, 1, Y, 1]) 
    return np.lib.stride_tricks.as_strided(img, shape=shape, strides=strides) 
+1

例如,说'W,H,X,Y =(10,14,4,7)','P.shape'将'(11,4,4 ,7)',你怎么能抵消和什么? – alko

+0

刚刚纠正,X,Y的大小必须是奇数。假设W,H,X,Y =(10,14,5,7),P.shape将是(6,8,5,7),我想访问P [0,0]为P [2, 3] – memecs

+0

则P.shape为(10,4,5,7),因此P [0,0] .shape为(5,7),它是左上A的5x7大小的子阵列。仍然不清楚你想要什么 – alko

回答

0

是下面的表达式

P = np.roll(np.roll(P, X/2, 0), Y/2, 1) 

你需要什么?

演示:

>>> W, H, X, Y = 10, 14, 5, 7 
>>> A = np.arange(W*H).reshape(W,H) 
>>> P = patchify(A,(X,Y)) 
>>> P = np.roll(np.roll(P, X/2, 0), Y/2, 1) 
>>> all(map(all, P[X/2, Y/2] == A[:X, :Y])) 
True