Faster way to extract patches from images?

Avi*_*pta 5 python performance numpy image-processing

I am trying to extract patches of fixed size centered at some given position (x,y). The code is given below-

for i,j in zip(indices[0],indices[1]):
    patches.append(
        x[None,
          i-int(np.floor(patch_size/2.0)):i+int(np.floor(patch_size/2.0))+1,
          j-int(np.floor(patch_size/2.0)):j+int(np.floor(patch_size/2.0))+1])
Run Code Online (Sandbox Code Playgroud)

Variable indices is the locations (indices.shape=(2,770)). x is the original image.

But this code is taking 25s seconds time. Can anyone tell me how to make this work faster? or any other alternatives if you can suggest it would be of great help.

Div*_*kar 4

假设您单独处理近边界索引,否则您将拥有不同形状的补丁,让我们建议自己使用一种向量化方法,并broadcasting结合有关 的一些知识linear-indexing。下面发布的是一个遵循这一理念的实现,为我们提供了一系列3D此类补丁 -

m,n = x.shape
K = int(np.floor(patch_size/2.0))
R = np.arange(-K,K+1)                  
out = np.take(x,R[:,None]*n + R + (indices[0]*n+indices[1])[:,None,None])
Run Code Online (Sandbox Code Playgroud)

让我们在最小输入情况下运行一个示例,其中输入图像x(8,10)索引使得所需的补丁不会超出输入图像的边界。然后,运行原始方法和建议方法进行验证。开始了 -

1] 输入:

In [105]: # Inputs
     ...: x = np.random.randint(0,99,(8,10))
     ...: indices = np.array([[4,2,3],[6,3,7]])
     ...: 
Run Code Online (Sandbox Code Playgroud)

3]原始方法与输出:

In [106]: # Posted code in the question ...

In [107]: patches[0]
Out[107]: 
array([[[92, 21, 84],
        [10, 52, 36],
        [ 5, 62, 61]]])

In [108]: patches[1]
Out[108]: 
array([[[71, 76, 75],
        [80, 32, 55],
        [77, 62, 42]]])

In [109]: patches[2]
Out[109]: 
array([[[16, 88, 31],
        [21, 84, 51],
        [52, 36,  3]]])
Run Code Online (Sandbox Code Playgroud)

3]建议的输出方法:

In [110]:  # Posted code in the solution earlier ...

In [111]: out
Out[111]: 
array([[[92, 21, 84],
        [10, 52, 36],
        [ 5, 62, 61]],

       [[71, 76, 75],
        [80, 32, 55],
        [77, 62, 42]],

       [[16, 88, 31],
        [21, 84, 51],
        [52, 36,  3]]])
Run Code Online (Sandbox Code Playgroud)