How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function

后端 未结 6 1302
我在风中等你
我在风中等你 2020-11-27 02:55

I\'m looking for a good approach for efficiently dividing an image into small regions, processing each region separately, and then re-assembling the results from each proces

6条回答
  •  一向
    一向 (楼主)
    2020-11-27 03:12

    Bit late to the game, but this would do overlapping blocks. I haven't done it here, but you could easily adapt for step sizes for shifting the window, I think:

    from numpy.lib.stride_tricks import as_strided
    def rolling_block(A, block=(3, 3)):
        shape = (A.shape[0] - block[0] + 1, A.shape[1] - block[1] + 1) + block
        strides = (A.strides[0], A.strides[1]) + A.strides
        return as_strided(A, shape=shape, strides=strides)
    

提交回复
热议问题