Pixel neighbors in 2d array (image) using Python

后端 未结 7 974
灰色年华
灰色年华 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 08:55

    By using max and min, you handle pixels at the upper and lower bounds:

    im[max(i-1,0):min(i+2,i_end), max(j-1,0):min(j+2,j_end)].flatten()
    

提交回复
热议问题