from matrix import * import random import math import cmath # complex numbers ## utilities def add_gauss(mat,sigma=10): ''' Generates Gaussian noise with mean 0 and SD sigma. Adds indep. noise to pixel, keeping values in 0..255''' n,m=mat.dim() new=Matrix(n,m) for i in range (n): for j in range (m): noise=round(random.gauss(0,sigma)) if noise==0: new[i,j]=mat[i,j] elif noise>0: new[i,j]=min(mat[i,j]+noise,255) elif noise<0: new[i,j]=max(mat[i,j]+noise,0) return new def add_SP(mat,p=0.01): ''' Generates salt and pepper noise: Each pixel is "hit" indep. with prob. p. If hit, it has fifty fifty chance of becoming white or black. ''' n,m=mat.dim() new=Matrix(n,m) for i in range (n): for j in range (m): rand=random.randint(0,20000) if rand