[Solved] how to change the particuler elements of an array


You can use a 2D convolution on the 1s with a 3×3 kernel of 1s to identify the centers of the 3×3 squares, then dilate them and restore the non 1 numbers

from scipy.signal import convolve2d
from scipy.ndimage import binary_dilation

# get 1s (as boolean)
m = data==1

kernel = np.ones((3, 3))

# get centers
conv = convolve2d(m, kernel, mode="same")

# dilate and restore the other numbers
out = np.where(m, binary_dilation(conv == 9, kernel).astype(int), data)

print(out)

Alternative, erosion and dilation, similarly to my previous answer:

from scipy.ndimage import binary_dilation, binary_erosion

m = data==1
kernel = np.ones((3, 3))

out = np.where(m, binary_dilation(binary_erosion(m, kernel), kernel), data)

Output:

[[4 4 4 0 1 1 1 1 1 1 0 0 0 0 0]
 [3 0 0 0 1 1 1 1 1 1 1 1 1 0 0]
 [6 0 0 0 1 1 1 1 1 1 1 1 1 0 0]
 [2 0 0 0 1 1 1 0 0 0 1 1 1 0 0]
 [2 0 0 0 1 1 1 0 0 0 0 0 0 0 0]]

1

solved how to change the particuler elements of an array