跟进这个问题(和 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)