我有一个形状为 DxHxW 的 3D 图像。我成功地将图像提取到补丁 pdxphxpw(重叠补丁)中。对于每个补丁,我都做了一些处理。现在,我想从处理过的补丁中生成图像,这样新图像的形状必须与原始图像相同。你能帮我做吗?
这是我提取补丁的代码
def patch_extract_3D(input,patch_shape,xstep=1,ystep=1,zstep=1):
patches_3D = np.lib.stride_tricks.as_strided(input, ((input.shape[0] - patch_shape[0] + 1) / xstep, (input.shape[1] - patch_shape[1] + 1) / ystep,
(input.shape[2] - patch_shape[2] + 1) / zstep, patch_shape[0], patch_shape[1], patch_shape[2]),
(input.strides[0] * xstep, input.strides[1] * ystep,input.strides[2] * zstep, input.strides[0], input.strides[1],input.strides[2]))
patches_3D= patches_3D.reshape(patches_3D.shape[0]*patches_3D.shape[1]*patches_3D.shape[2], patch_shape[0],patch_shape[1],patch_shape[2])
return patches_3D
这是处理补丁(只是简单的乘以 2
for i in range(patches_3D.shape[0]):
patches_3D[i]=patches_3D[i];
patches_3D[i]=patches_3D[i]*2;
现在,我需要的是 patches_3D,我想将它重新整形为原始图像。谢谢
这是示例代码
patch_shape=[2, 2, 2]
input=np.arange(4*4*6).reshape(4,4,6)
patches_3D=patch_extract_3D(input,patch_shape)
print patches_3D.shape
for i in range(patches_3D.shape[0]):
patches_3D[i]=patches_3D[i]*2
print patches_3D.shape
请您参考如下方法:
然而,这会反过来,因为你的补丁重叠,只有当它们的值在它们重叠的地方一致时,这才会被明确定义
def stuff_patches_3D(out_shape,patches,xstep=12,ystep=12,zstep=12):
out = np.zeros(out_shape, patches.dtype)
patch_shape = patches.shape[-3:]
patches_6D = np.lib.stride_tricks.as_strided(out, ((out.shape[0] - patch_shape[0] + 1) // xstep, (out.shape[1] - patch_shape[1] + 1) // ystep,
(out.shape[2] - patch_shape[2] + 1) // zstep, patch_shape[0], patch_shape[1], patch_shape[2]),
(out.strides[0] * xstep, out.strides[1] * ystep,out.strides[2] * zstep, out.strides[0], out.strides[1],out.strides[2]))
patches_6D[...] = patches.reshape(patches_6D.shape)
return out
更新:这是一个更安全的版本,它平均重叠像素:
def stuff_patches_3D(out_shape,patches,xstep=12,ystep=12,zstep=12):
out = np.zeros(out_shape, patches.dtype)
denom = np.zeros(out_shape, patches.dtype)
patch_shape = patches.shape[-3:]
patches_6D = np.lib.stride_tricks.as_strided(out, ((out.shape[0] - patch_shape[0] + 1) // xstep, (out.shape[1] - patch_shape[1] + 1) // ystep,
(out.shape[2] - patch_shape[2] + 1) // zstep, patch_shape[0], patch_shape[1], patch_shape[2]),
(out.strides[0] * xstep, out.strides[1] * ystep,out.strides[2] * zstep, out.strides[0], out.strides[1],out.strides[2]))
denom_6D = np.lib.stride_tricks.as_strided(denom, ((denom.shape[0] - patch_shape[0] + 1) // xstep, (denom.shape[1] - patch_shape[1] + 1) // ystep,
(denom.shape[2] - patch_shape[2] + 1) // zstep, patch_shape[0], patch_shape[1], patch_shape[2]),
(denom.strides[0] * xstep, denom.strides[1] * ystep,denom.strides[2] * zstep, denom.strides[0], denom.strides[1],denom.strides[2]))
np.add.at(patches_6D, tuple(x.ravel() for x in np.indices(patches_6D.shape)), patches.ravel())
np.add.at(denom_6D, tuple(x.ravel() for x in np.indices(patches_6D.shape)), 1)
return out/denom
