interesting problem, i wrote a program that will recursively loop through the image and smooth out the pixels by averaging them. It looks for absolute values over a certain size, and if detected will average those 2 values rebuilding the matrix. let me know what you think:
from statistics import mean
myimage = [
[0,0,0,0,.1],
[0,.1,3,1,.1],
[1,.1,4,.2,.1],
[0,1,0,0,.1],
[.1,.9,0,0,.1]
]
def smooth(matrix, delta):
noise=0
reduction=matrix.copy()
for i,row in enumerate(matrix):
for j,pixel in enumerate(row):
if j<len(row)-1:
if (abs(row[j]-row[j+1]))>=delta:
noise=1
av = mean([row[j],row[j+1]])
mv,iv=max((v,i) for i,v in enumerate((row[j],row[j+1])))
if iv==0:
reduction[i][j]=av
else:
reduction[i][j+1]=av
if i<len(matrix)-1:
if abs(row[j]-matrix[i+1][j])>=delta:
noise=1
av = mean([row[j],matrix[i+1][j]])
mv,iv=max((v,i) for i,v in enumerate((row[j],matrix[i+1][j])))
if iv==0:
reduction[i][j]=av
else:
reduction[i+1][j]=av
if noise==1:
return smooth(reduction, delta)
else:
return reduction
x=smooth(myimage, 0.5)
for line in x:
print(line)
#[0, 0, 0, 0, 0.1]
#[0, 0.1, 0.4, 0.25, 0.1]
#[0.25, 0.1, 0.3625, 0.2, 0.1]
#[0, 0.275, 0, 0, 0.1]
#[0.1, 0.29375, 0, 0, 0.1]