# Find cells with 3x3 window [closed]

I have a raster and need to only identify cells that are adjacent within a 3x3 window. I tried various tools but without any success. My current cells are 1x1 and currently using ArcGIS.

## closed as unclear what you're asking by ahmadhanb, Erik, LaughU, Hornbydd, whyzarFeb 26 at 14:02

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• we cannot have raster data with different pixel size as you have mentioned in the sample image – Gurminder Bharani Feb 25 at 20:18
• This is a more complicated task than it seems at first. Have a look at the method discussed in this similar question. It looks like the custom script that was discussed there, was never created or at least not shared. But perhaps you can take a similar approach. – csk Feb 25 at 21:00

You can use arcpy and numpy, no need for Spatial Analyst. Convert your raster to 0 and 1, with 1 as your grey areas and use this as `in_raster`:

``````import arcpy
import numpy as np

in_raster = r'C:\Test\randraster_1_0.tif'
to_find = np.ones(shape=(2,2)) #2 rows, 2 columns of ones. Change to 3,3 to find 3x3 matrix
output_raster = r'C:\Test\resultraster.tif' #Resulting raster. Will be 1 where your matrix is found and 0 everywhere else.

def im2col(A,BLKSZ):
#https://stackoverflow.com/questions/32531377/how-can-i-check-if-one-two-dimensional-numpy-array-contains-a-specific-pattern-o
M,N = A.shape
col_extent = N - BLKSZ + 1
row_extent = M - BLKSZ + 1
start_idx = np.arange(BLKSZ)[:,None]*N + np.arange(BLKSZ)
offset_idx = np.arange(row_extent)[:,None]*N + np.arange(col_extent)
return np.take (A,start_idx.ravel()[:,None] + offset_idx.ravel())

arr = arcpy.RasterToNumPyArray(in_raster)
col_match = im2col(arr,to_find.shape) == to_find.ravel()[:,None]
out_shape = np.asarray(arr.shape) - np.asarray(to_find.shape) + 1
R,C = np.where(col_match.all(0).reshape(out_shape))
resultarr = np.zeros(shape=arr.shape)

for idx in zip(R.tolist(),C.tolist()):
resultarr[idx:idx+to_find.shape,idx:idx+to_find.shape] = 1 #Im sure there is a better way of indexing...

desc = arcpy.Describe(in_raster+r'/Band_1')
arcpy.env.outputCoordinateSystem = desc.spatialReference
resultraster = arcpy.NumPyArrayToRaster(in_array=resultarr, lower_left_corner=desc.extent.lowerLeft, x_cell_size=desc.meanCellHeight, y_cell_size=desc.meanCellWidth)
resultraster.save(output_raster)
`````` 