Using GDAL/Python is another way to do this procedure. For testing it, I prepared a raster (with aleatory values of 0 and 1) and 29 rows by 29 columns. This was used for running the following code in the Python Console of QGIS (and this raster as active layer):
from osgeo import gdal
import struct
import numpy as np
layer = iface.activeLayer()
provider = layer.dataProvider()
fmttypes = {'Byte':'B', 'UInt16':'H', 'Int16':'h', 'UInt32':'I', 'Int32':'i', 'Float32':'f', 'Float64':'d'}
path= provider.dataSourceUri()
dataset = gdal.Open(path)
band = dataset.GetRasterBand(1)
BandType = gdal.GetDataTypeName(band.DataType)
array = []
for i in range(band.YSize - 2):
for j in range(band.XSize - 2):
scanline = band.ReadRaster(i, j, 3, 3, 3, 3, band.DataType)
values = struct.unpack(fmttypes[BandType] * 9, scanline)
tmp = np.sum(values) - values[4]
array.append(tmp)
dataset = None
The algorithm starts at the position (x_off,y_off) = (0,0) and explores all cells considering blocks of 3x3. The element value[4] = value[2][2] is always eliminated of each sum. The neighborhood analysis is stored in the list array and could be used for creating a new raster. The complete serie obtained here (array) for this raster is exposed to continuation and it was verified that the expected values were produced.
[6.0, 4.0, 3.0, 5.0, 4.0, 6.0, 3.0, 3.0, 4.0, 2.0, 3.0, 2.0, 3.0, 4.0, 6.0, 5.0, 5.0, 3.0, 3.0, 2.0, 3.0, 4.0, 2.0, 4.0, 1.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0, 3.0, 5.0, 4.0, 3.0, 4.0, 2.0, 4.0, 1.0, 4.0, 3.0, 4.0, 4.0, 5.0, 4.0, 5.0, 4.0, 3.0, 4.0, 4.0, 4.0, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 5.0, 6.0, 5.0, 4.0, 4.0, 4.0, 3.0, 4.0, 3.0, 4.0, 4.0, 5.0, 5.0, 6.0, 5.0, 4.0, 2.0, 5.0, 4.0, 5.0, 4.0, 4.0, 2.0, 3.0, 3.0, 4.0, 6.0, 6.0, 5.0, 5.0, 5.0, 4.0, 3.0, 5.0, 4.0, 4.0, 3.0, 3.0, 5.0, 6.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 6.0, 3.0, 4.0, 3.0, 4.0, 4.0, 3.0, 3.0, 4.0, 4.0, 6.0, 6.0, 5.0, 3.0, 4.0, 6.0, 6.0, 5.0]
A new evaluation, with a slight modification of the above code, could be executed with 5x5 blocks if the requeriment of sum >= 5 is not accomplished for 3x3 blocks .