0

跟进这个问题(和 jorgeca 的回答): 将图像切片成重叠补丁并将补丁合并到图像的快速方法我想为补丁化数组的索引添加一个偏移量,即:

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)
4

1 回答 1

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    
于 2013-12-17T14:58:47.687 回答