尝试将尺寸为100x100的灰度图像切割成大小为39x39且重叠的贴片,步幅大小为1.这意味着向下/向下开始一个像素的下一个贴片仅与之前的贴片不同一个额外的列/或行.
代码的粗略轮廓:首先计算每个补丁的索引,以便能够从图像构造补丁的2D阵列,并能够从补丁构建图像:
patches = imgFlat[ind]
Run Code Online (Sandbox Code Playgroud)
'patches'是一个2D数组,每列包含一个矢量形式的补丁.
处理这些补丁,每个补丁单独地和之后再次合并到具有预先计算的索引的图像.
img = np.sum(patchesWithColFlat[ind],axis=2)
Run Code Online (Sandbox Code Playgroud)
由于补丁重叠,最后需要将img与预先计算的权重相乘:
imgOut = weights*imgOut
Run Code Online (Sandbox Code Playgroud)
我的代码非常慢,速度是一个关键问题,因为这应该在ca. 10 ^ 8个补丁.
函数get_indices_for_un_patchify和weights_unpatchify可以预先计算一次,因此速度只是patchify和unpatchify的问题.
谢谢你的任何tipps.
卡洛斯
import numpy as np
import scipy
import collections
import random as rand
def get_indices_for_un_patchify(sImg,sP,step):
''' creates indices for fast patchifying and unpatchifying
INPUTS:
sx image size
sp patch size
step offset between two patches (default == [1,1])
OUTPUTS:
patchInd collection with indices
patchInd.img2patch patchifying indices
patch = img(patchInd.img2patch);
patchInd.patch2img unpatchifying indices
NOTE: * for unpatchifying necessary to …Run Code Online (Sandbox Code Playgroud)