【发布时间】:2013-12-17 12:12:33
【问题描述】:
跟进这个问题(以及 jorgeca 的回答): Fast Way to slice image into overlapping patches and merge patches to image 我想给补丁数组的索引添加一个偏移量,即:
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)
【问题讨论】:
-
例如说
W, H, X, Y = (10, 14, 4, 7),P.shape将是(11, 4, 4, 7),你怎么不想抵消什么? -
刚刚更正,X,Y 的大小必须是奇数。让我们考虑 W,H,X,Y = (10,14,5,7),P.shape 将是 (6,8,5,7),我想访问 P[0,0] 作为 P[2, 3]
-
那么 P.shape 是 (10, 4, 5, 7),因此 P[0,0].shape 是 (5, 7),它是左上 A 的 5x7 大小的子数组。还不清楚你想要什么
-
我不明白你为什么说P.shape是(10,4,5,7)? P.shape 应该是 (6,8,5,7)。我希望能够更改内存布局或任何其他允许我键入 P[2,3] 并获得现在的 P[0,0] 即以 A[2,3] 为中心的 5.7 子数组跨度>
-
如果你想要 6,8,你可以混合 W 和 H。在这种情况下,P[0,0] 的值是多少?
标签: python arrays numpy stride