我修改了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
然后,我使用Google帖子中的修改代码来恢复补丁:
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的图像,wh看起来像原始图像。但是,当使用 inds = img_recover!= img_org 和 num = np.sum(inds)的代码测试恢复的图像时,我发现 num < / em>远大于0。实际上,img_org的形状为399 * 196 * 299,即23382996体素和 num = 1317528 。
我找不到原因。有帮助吗?预先感谢!