public Bilateral(double sigmaD, double sigmaR) { // compute the necessary kernel radius from the maximum of both sigma values double sigmaMax = Math.Max(sigmaD, sigmaR); int kernelRadius = (int)Math.Ceiling(2 * sigmaMax); this.SigmaD = sigmaD; this.SigmaR = sigmaR; this.TwoSigmaRSquared = 2 * this.SigmaR * this.SigmaR; this.Size = kernelRadius * 2 + 1; int center = (this.Size - 1) / 2; Gaussian gauss = new Gaussian(sigmaD); this.KernelD = gauss.Kernel2D(this.Size); for (int x = -center; x < -center + this.Size; x++) { for (int y = -center; y < -center + this.Size; y++) { this.KernelD[x + center, y + center] = gauss.Function2D(x, y); } } // precomute all possible similarity values for performance resaons this.GaussSimilarity = new double[256]; for (int i = 0; i < 256; i++) { this.GaussSimilarity[i] = Math.Exp(-((i) / this.TwoSigmaRSquared)); } }