如何从python中提取的补丁列表中重建图像/数组?

时间:2018-11-19 17:06:29

标签: python arrays python-3.x image

我的图片大小为256x256。我将图像提取到大小为32x32的补丁中,补丁的中心将以20的窗口步长滑动。如果提取的补丁大小小于32x32。然后添加零填充以获取32x32。提取补丁的代码是

import numpy as np
image = np.random.randn(256,256)
patch_H, patch_W = 32, 32
step_H, step_W = 20, 20

lst_H = np.arange(0, image.shape[0] + patch_H, step_H)
lst_W = np.arange(0, image.shape[1] + patch_W, step_W)
image_patches =[]
for i in range(len(lst_H)):
  for j in range(len(lst_W)):
    h= lst_H[i]
    w= lst_W[j]
    patch = image[h : h + patch_H, w : w + patch_W]
    # Zero padding
    if (patch.shape[0]!= patch_H or patch.shape[1]!= patch_W):
      patch = np.pad(patch,[(0, patch_H- patch.shape[0]), (0, patch_W- patch.shape[1])], mode='constant')
    patch =patch[None,:,:]
    image_patches.append(patch)
image_patches = np.vstack(image_patches) # (225, 32, 32)

使用上面的代码,我获得了32x32的225个补丁。我想从image_patches生成一个与原始图像(256x256)大小相同的新图像,并将这些值在重叠的色块之间求和。

困难之处在于,我必须首先取消填充零填充的补丁。因此,如果没有展开填充,则无法在波纹管代码中分配image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]。我该如何解决这个问题?在我看来,我想我们必须保存一个字典来保存其他键,例如零标志,位置...这就是我所做的

# Recover the image
image_rec = np.zeros (image.shape)
num_patches=0
for i in range(len(lst_H)):
  for j in range(len(lst_W)):
    h= lst_H[i]
    w= lst_W[j]
    image_rec[h : h + patch_H, w : w + patch_W]+=image_patches[num_patches,:,:]
    num_patches +=1

0 个答案:

没有答案