// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function AForge.Math.Gaussian gaus = new AForge.Math.Gaussian(sigma); // create kernel double[,] kernel = gaus.Kernel2D(size); double min = kernel[0, 0]; // integer kernel int[,] intKernel = new int[size, size]; int divisor = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { double v = kernel[i, j] / min; if (v > ushort.MaxValue) { v = ushort.MaxValue; } intKernel[i, j] = (int)v; // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }
// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function var gaus = new AForge.Math.Gaussian(this.sigma); // create kernel var kernel = gaus.Kernel2D(this.size); var min = kernel[0, 0]; // integer kernel var intKernel = new int[this.size, this.size]; var divisor = 0; for (var i = 0; i < this.size; i++) { for (var j = 0; j < this.size; j++) { var v = kernel[i, j] / min; if (v > ushort.MaxValue) { v = ushort.MaxValue; } intKernel[i, j] = (int)v; // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }
// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function AForge.Math.Gaussian gaus = new AForge.Math.Gaussian(sigma); // create kernel double[,] kernel = gaus.Kernel2D(size); double min = kernel[0, 0]; // integer kernel int[,] intKernel = new int[size, size]; int sum = 0; int divisor = 0; // calculate integer kernel for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { double v = kernel[i, j] / min; if (v > ushort.MaxValue) { v = ushort.MaxValue; } intKernel[i, j] = (int)v; // collect sum sum += intKernel[i, j]; } } // recalc kernel int c = size >> 1; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if ((i == c) && (j == c)) { // calculate central value intKernel[i, j] = 2 * sum - intKernel[i, j]; } else { // invert value intKernel[i, j] = -intKernel[i, j]; } // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }
// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function var gaus = new AForge.Math.Gaussian(this.sigma); // create kernel var kernel = gaus.Kernel2D(this.size); var min = kernel[0, 0]; // integer kernel var intKernel = new int[this.size, this.size]; var sum = 0; var divisor = 0; // calculate integer kernel for (var i = 0; i < this.size; i++) { for (var j = 0; j < this.size; j++) { var v = kernel[i, j] / min; if (v > ushort.MaxValue) { v = ushort.MaxValue; } intKernel[i, j] = (int)v; // collect sum sum += intKernel[i, j]; } } // recalc kernel var c = this.size >> 1; for (var i = 0; i < this.size; i++) { for (var j = 0; j < this.size; j++) { if ((i == c) && (j == c)) { // calculate central value intKernel[i, j] = 2 * sum - intKernel[i, j]; } else { // invert value intKernel[i, j] = -intKernel[i, j]; } // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }
/// <summary> /// Smooths an image with Gaussian kernel. /// </summary> /// <param name="img">Input image.</param> /// <param name="kernelSize">Kernel size.</param> /// <param name="sigma">Sigma (standard deviation)</param> /// <returns>Smoothed image.</returns> public static TColor[,] SmoothGaussian <TColor>(this TColor[,] img, int kernelSize, double sigma) where TColor : struct, IColor <float> { var gaussianKernel = new AForge.Math.Gaussian(sigma); //TODO: it would be nice if I could get separated kernel var kernel = gaussianKernel.Kernel2D(kernelSize); //normalize kernel double factor = 1d / kernel.Sum().Sum(); kernel.ApplyInPlace(x => x * factor); return(img.Convolve(kernel.ToSingle())); }
// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function AForge.Math.Gaussian gaus = new AForge.Math.Gaussian( sigma ); // create kernel double[,] kernel = gaus.Kernel2D( size ); double min = kernel[0, 0]; // integer kernel int[,] intKernel = new int[size, size]; int divisor = 0; for ( int i = 0; i < size; i++ ) { for ( int j = 0; j < size; j++ ) { double v = kernel[i, j] / min; if ( v > ushort.MaxValue ) { v = ushort.MaxValue; } intKernel[i, j] = (int) v; // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }
// Private members #region Private Members // Create Gaussian filter private void CreateFilter( ) { // create Gaussian function AForge.Math.Gaussian gaus = new AForge.Math.Gaussian( sigma ); // create kernel double[,] kernel = gaus.Kernel2D( size ); double min = kernel[0, 0]; // integer kernel int[,] intKernel = new int[size, size]; int sum = 0; int divisor = 0; // calculate integer kernel for ( int i = 0; i < size; i++ ) { for ( int j = 0; j < size; j++ ) { double v = kernel[i, j] / min; if ( v > ushort.MaxValue ) { v = ushort.MaxValue; } intKernel[i, j] = (int) v; // collect sum sum += intKernel[i, j]; } } // recalc kernel int c = size >> 1; for ( int i = 0; i < size; i++ ) { for ( int j = 0; j < size; j++ ) { if ( ( i == c ) && ( j == c ) ) { // calculate central value intKernel[i, j] = 2 * sum - intKernel[i, j]; } else { // invert value intKernel[i, j] = -intKernel[i, j]; } // collect divisor divisor += intKernel[i, j]; } } // update filter this.Kernel = intKernel; this.Divisor = divisor; }