【问题标题】:How to recover 3D image from its patches in Python?如何从 Python 中的补丁中恢复 3D 图像?
【发布时间】:2017-07-15 00:24:52
【问题描述】:

我有一个形状为 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;

现在,我需要的是 patch_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

【问题讨论】:

  • patches_3Dpdxphxpw2 的元素乘法仍将保持为 pdxphxpw。所以,我不确定如何从那里获得DxHxW 的原始图像形状。
  • 我只是多重强度,而不是形状
  • 不要以为你明白我的意思。 patches_3D 和原图形状不同。如果没有某种减少,你不能从patches_3D 回到原来的那个。此外,在您的示例中,patches_3D.shape 是 (0,2,2,2)。如果您在发布之前测试样本,那将是有意义的。
  • 对不起,上述测试形状2中的步长必须是1。patch_3D的形状是(45, 2, 2, 2)

标签: python python-2.7 numpy image-processing


【解决方案1】:

然而,这将做相反的事情,因为你的补丁重叠,只有当它们的值在它们重叠的地方一致时,它才会被明确定义

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

【讨论】:

  • 感谢您的解决方案。我只是询问返回的变量。为什么现在出了 patch_6D?如果返回的变量不存在,为什么需要patches_6D[...] = patches.reshape(patches_6D.shape)
  • out 和 patch_6d 引用相同的内存,但 out 具有您想要的形状。但是,请务必小心使用此解决方案,因为它会覆盖与后面的补丁重叠的第一个补丁。
  • @user3051460 我添加了一个更安全的版本。我认为如果你使用它会更好。
  • 谢谢,虽然比较慢
  • 谢谢。想请教一下参数。为什么我们需要在(input.shape[0] - patch_shape[0] + 1) / xstep 中加 1。我问这个是因为有时如果我们没有设置正确的xstep,补丁就不能靠近边界。我猜
猜你喜欢
  • 2017-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 2019-06-17
  • 2020-08-22
  • 2014-07-04
  • 2021-06-03
相关资源
最近更新 更多