To reduce noise in a raster file, I aim to eliminate isolated pixels, those surrounded by NoData values in all eight neighboring positions.
I have this code, but it is not removing those isolated pixels, it removes some others, and I do not understand why:
import rasterio
from rasterio.enums import Resampling
from rasterio.fill import fillnodata
# Open the TIFF file
with rasterio.open('input.tif') as src:
# Read the raster data
data = src.read(1)
nodata_value = src.nodata
# Set the NoData pixels surrounded by 8 NoData pixels to the NoData value
filled_data = fillnodata(data, mask=data == nodata_value, max_search_distance=1, smoothing_iterations=0)
# Create a new TIFF file with the filled data
with rasterio.open('output.tif', 'w', **src.profile) as dst:
dst.write(filled_data, 1)