Find a 3x3 sliding window over an image

前端 未结 3 1916
星月不相逢
星月不相逢 2020-12-09 13:56

I have an image.

I want to obtain a 3x3 window (neighbouring pixels) for every pixel in the image.

I have this Python code:

for x in range(2,         


        
3条回答
  •  被撕碎了的回忆
    2020-12-09 14:31

    Try the following code as matlab function im2col(...)

    import numpy as np
    
    def im2col(Im, block, style='sliding'):
        """block = (patchsize, patchsize)
            first do sliding
        """
        bx, by = block
        Imx, Imy = Im.shape
        Imcol = []
        for j in range(0, Imy):
            for i in range(0, Imx):
                if (i+bx <= Imx) and (j+by <= Imy):
                    Imcol.append(Im[i:i+bx, j:j+by].T.reshape(bx*by))
                else:
                    break
        return np.asarray(Imcol).T
    
    if __name__ == '__main__':
        Im = np.reshape(range(6*6), (6,6))
        patchsize = 3
        print Im
        out =  im2col(Im, (patchsize, patchsize))
        print out
        print out.shape
        print len(out)
    

提交回复
热议问题