Pixel neighbors in 2d array (image) using Python

后端 未结 7 899
灰色年华
灰色年华 2020-12-01 08:40

I have a numpy array like this:

x = np.array([[1,2,3],[4,5,6],[7,8,9]])

I need to create a function let\'s call it \"neighbors\" with the f

相关标签:
7条回答
  • 2020-12-01 09:05

    I don't know about any library functions for this, but you can easily write something like this yourself using the great slicing functionality of numpy:

    import numpy as np
    def neighbors(im, i, j, d=1):
        n = im[i-d:i+d+1, j-d:j+d+1].flatten()
        # remove the element (i,j)
        n = np.hstack((b[:len(b)//2],b[len(b)//2+1:] ))
        return n
    

    Of course you should do some range checks to avoid out-of-bounds access.

    0 讨论(0)
提交回复
热议问题