首页 > 解决方案 > 使用 python 从补丁中恢复图像

问题描述

我修改了 stuff_patches_3D 的代码以从重叠的补丁中恢复 3D 肋骨图像,但发现结果并不完全正确。

首先,我使用以下代码提取补丁:

def extract_patches(arr, patch_shape=24, extraction_step=8):
    # From: scikit-learn/sklearn/feature_extraction/image.py
    """Extracts patches of any n-dimensional array in place using strides.

    Parameters
    ----------
    arr : ndarray. n-dimensional array of which patches are to be extracted
    patch_shape : integer or tuple of length arr.ndim
    extraction_step : integer or tuple of length arr.ndim

    Returns
    -------
    patches : strided ndarray

    """
    print('Extract Patch...')
    arr_ndim = arr.ndim

    if isinstance(patch_shape, numbers.Number):
        patch_shape = tuple([patch_shape] * arr_ndim)
    if isinstance(extraction_step, numbers.Number):
        extraction_step = tuple([extraction_step] * arr_ndim)

    patch_strides = arr.strides

    slices = tuple(slice(None, None, st) for st in extraction_step)
    indexing_strides = arr[slices].strides

    patch_indices_shape = ((np.array(arr.shape) - np.array(patch_shape)) //
                           np.array(extraction_step)) + 1

    shape = tuple(list(patch_indices_shape) + list(patch_shape))
    strides = tuple(list(indexing_strides) + list(patch_strides))

    patches = as_strided(arr, shape=shape, strides=strides)

    return patches

然后,我使用谷歌帖子中的修改代码来恢复补丁:

def stuff_patches_3D(img_org, patches,xstep=12,ystep=12,zstep=12):
    out_shape = img_org.shape

    print('Recover image...')
    out = np.zeros(out_shape, patches.dtype)
    denom = np.zeros(out_shape, patches.dtype)
    patch_shape = patches.shape[-3:]

    patches_shape = ((out.shape[0]-patch_shape[0])//xstep+1, (out.shape[1]-patch_shape[1])//ystep+1,
                    (out.shape[2]-patch_shape[2])//zstep+1, patch_shape[0], patch_shape[1], patch_shape[2])
    patches_strides = (out.strides[0]*xstep, out.strides[1] * ystep,out.strides[2] * zstep, out.strides[0], out.strides[1],out.strides[2])

    patches_6D = np.lib.stride_tricks.as_strided(out, patches_shape, patches_strides)
    denom_6D = np.lib.stride_tricks.as_strided(denom, patches_shape, patches_strides)

    grid_inds = tuple(x.ravel() for x in np.indices(patches_6D.shape))
    np.add.at(patches_6D, grid_inds, patches.ravel())
    np.add.at(denom_6D, grid_inds, 1)

    # in case there are 0 elements in denom
    inds = denom != 0
    img_recover = np.zeros(out.shape, dtype=out.dtype)
    img_recover[inds] = out[inds]/denom[inds]

    return img_recover

我 plt.imshow() 恢复的 out_new 图像,看起来像原始图像。但是,当使用inds = img_recover!=img_orgnum = np.sum(inds)的代码测试恢复的图像时,我发现num远大于 0。实际上 img_org 的形状是 399*196* 299,即 23382996 个体素,并且num=1317528

我找不到原因。有什么帮助吗?提前致谢!

标签: pythonimagepatchrecover

解决方案


推荐阅读