UPD:我做了一个可复制的小例子(见文章末尾)。在
我想把重叠的图像分割成另一个模块,然后把它们分割成另一个模块。在
我试图使用numpyviews(as_strided)函数来创建源数组的只读视图,但它似乎不会在图像的边界上生成补丁,如果它不能被分片精确覆盖的话。这可以通过填充图像到正确的大小来解决,但是使用填充的代码运行的时间与简单的平铺循环(0.015s->;0.5s)相当。在
为了收集回补丁,我使用loop填充空图像,其中有1/2的初始重叠(原因是我的图像处理算法会损坏补丁边界)。在我的系统上,4960x3500和补丁大小为1000的情况下,使用loop处理补丁的时间是0.5s。在
有没有什么方法可以让它更快(使as_strided在我的例子中适用于拆分),或者甚至可以重新组合图像作为补丁的视图?在
代码:import time
import numpy as np
from numpy.lib.stride_tricks import as_strided
from scipy.misc import imread, imsave, imresize
# the code produces read-only view of an image in 0.015s
# function taken from
# https://stackoverflow.com/questions/45960192/
def window_nd(a, window, steps = None, axis = None, outlist = False):
"""
Create a windowed view over `n`-dimensional input that uses an
`m`-dimensional window, with `m <= n`
Parameters
-------------
a : Array-like
The array to create the view on
window : tuple or int
If int, the size of the window in `axis`, or in all dimensions if
`axis == None`
If tuple, the shape of the desired window. `window.size` must be:
equal to `len(axis)` if `axis != None`, else
equal to