如何在Python中从补丁恢复3D图像?

2024-09-28 01:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个三维图像,形状是DxHxW。我成功地将图像提取成补丁pdxphxpw(重叠的补丁)。对于每个补丁,我都会做一些处理。现在,我想从处理过的补丁生成图像,这样新的图像必须与原始图像的形状相同。你能帮我做这件事吗。在

enter image description here

这是我提取补丁的代码

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

^{pr2}$

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

Tags: 代码图像inputnpextractpatch形状print
1条回答
网友
1楼 · 发布于 2024-09-28 01:29:40

但是,这将反过来,因为您的面片重叠,只有当它们的值在重叠的地方一致时,才会定义良好

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

更新:这里是一个更安全的版本,平均重叠像素:

^{pr2}$

相关问题 更多 >

    热门问题