2017-02-24 84 views
0

我有一個形狀爲DxHxW的3D圖像。我成功地將圖像提取到補丁pdxphxpw(重疊補丁)。對於每個補丁,我都會做一些處理。現在,我想從處理過的補丁中生成圖像,以使新圖像必須與原始圖像具有相同的形狀。你能幫我做到嗎?如何從Python中的補丁恢復3D圖像?

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

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 
+0

'patches_3D'是'pdxphxpw'。 「2」的元素乘法仍然保持爲「pdxphxpw」。所以,我不確定如何從那裏得到'DxHxW'的原始圖像形狀。 – Divakar

+0

我只是多強度,不是形狀 – user3051460

+0

不要以爲你理解我的觀點。 'patches_3D'和原始圖像是不同的形狀。你不能從'patches_3D'回到原來的版本,而不會減少某些東西。另外,在你的樣本'patches_3D.shape'中是(0,2,2,2)。如果您在發佈之前測試樣本,這是否合理? – Divakar

回答

4

這將反過來做,但是,因爲你的補丁重疊,這樣只會被明確定義,如果他們的價值認同它們重疊

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 
+0

它工作。我會測試更多 – user3051460

+0

感謝您的解決方案。我只是詢問返回的變量。爲什麼現在patch_6D?如果返回的變量不存在,爲什麼需要'patches_6D [patch] .shape(patches_6D.shape)' – user3051460

+0

out和patches_6d引用相同的內存,但out的形狀是你想要的。但是,請小心使用此解決方案,因爲它會覆蓋第一個與後面的補丁重疊的補丁。 –