예제 #1
0
        public static void Solve()
        {
            var       N   = Scanner.Scan <int>();
            const int max = (int)1e5 + 1;
            const int p   = 998244353;

            var G = new ModuloInteger[max];

            G[0] = 1;
            for (var i = 1; i < max; i++)
            {
                G[i] = G[i - 1] * (2 * i - 1);
            }

            var freq = new int[max];

            for (var i = 0; i < 2 * N; i++)
            {
                var H = Scanner.Scan <int>();
                freq[H]++;
            }

            var queue = new Queue <ModuloInteger[]>();

            queue.Enqueue(new ModuloInteger[] { 1 });
            foreach (var n in freq)
            {
                if (n < 2)
                {
                    continue;
                }
                var list = new ModuloInteger[n / 2 + 1];
                for (var i = 0; i * 2 <= n; i++)
                {
                    list[i] = Enumeration.CombinationCount(n, i * 2, p) * G[i];
                    if (i % 2 == 1)
                    {
                        list[i] = -list[i];
                    }
                }
                queue.Enqueue(list);
            }

            while (queue.Count > 1)
            {
                var list1 = queue.Dequeue();
                var list2 = queue.Dequeue();
                queue.Enqueue(Convolution.Execute(list1, list2).ToArray());
            }

            ModuloInteger answer = 0;
            var           last   = queue.Dequeue();

            for (var i = 0; i < last.Length; i++)
            {
                answer += last[i] * G[N - i];
            }

            Console.WriteLine(answer);
        }
예제 #2
0
        public IEnumerable <Point> Filtration(int M, double fo, IEnumerable <Point> points, IWindow window,
                                              FilterType filterType)
        {
            var fp = 1 / (points.ElementAt(1).X - points.ElementAt(0).X);

            if (filterType == FilterType.HighPassFilter)
            {
                fo = fp / 2 - fo;
            }

            var response = new List <Point>();
            var K        = fp / fo;

            for (var i = 0; i < M; i++)
            {
                response.Add(new Point(i,
                                       ImpulseResponse.Response(i, K, M) * window.Window(i, M) * filterTypes[filterType](i)));
            }

            var ret       = Convolution.Convolute(points, response);
            var amplitude = ret.Select(p => p.Y).Max() * 2;

            amplitude = points.Select(p => p.Y).Max() / amplitude;

            foreach (var point in ret)
            {
                point.Y *= amplitude;
            }

            return(ret);
        }
예제 #3
0
        /// <summary>
        /// Processes the image.
        /// </summary>
        /// <param name="factory">The the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class containing
        /// the image to process.</param>
        /// <returns>
        /// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory" /> class.
        /// </returns>
        public Image ProcessImage(ImageFactory factory)
        {
            Bitmap newImage = null;
            Bitmap image    = (Bitmap)factory.Image;

            try
            {
                newImage = new Bitmap(image);
                GaussianLayer gaussianLayer = (GaussianLayer)this.DynamicParameter;

                Convolution convolution = new Convolution(gaussianLayer.Sigma)
                {
                    Threshold = gaussianLayer.Threshold
                };
                double[,] kernel = convolution.CreateGuassianBlurFilter(gaussianLayer.Size);
                newImage         = convolution.ProcessKernel(newImage, kernel);

                image.Dispose();
                image = newImage;
            }
            catch
            {
                if (newImage != null)
                {
                    newImage.Dispose();
                }
            }

            return(image);
        }
예제 #4
0
        /// <summary>
        /// foamliu, 2009/03/03, 边缘锐化.
        /// 从原图像中减去拉普拉斯算子处理后的结果.
        /// 我做的效果是图像锐化的同时产生了噪音. 与这个结果类似:
        ///
        /// http://www.dfanning.com/ip_tips/sharpen.html
        ///
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Bitmap SharpenEdges(Bitmap bmp)
        {
            int width, height;

            int[][][] mat, filtered = new int[3][][];
            Bitmap    newBmp;

            ImageConvert.Bitmap2MatColor(bmp, out mat, out width, out height);

            Convolution conv = new Convolution();

            conv.Calculate(mat[0], ConvKernel.Laplacian_4, out filtered[0]);
            conv.Calculate(mat[1], ConvKernel.Laplacian_4, out filtered[1]);
            conv.Calculate(mat[2], ConvKernel.Laplacian_4, out filtered[2]);

            NcvMatrix.MatSubtract(mat[0], filtered[0]);
            NcvMatrix.MatSubtract(mat[1], filtered[1]);
            NcvMatrix.MatSubtract(mat[2], filtered[2]);

            GrayScaleImageLib.Normalize(mat[0]);
            GrayScaleImageLib.Normalize(mat[1]);
            GrayScaleImageLib.Normalize(mat[2]);

            ImageConvert.Mat2BitmapColor(mat, width, height, out newBmp);

            return(newBmp);
        }
        public static List <Complex> Transform(List <double> points)
        {
            List <Complex> transformed = new List <Complex>();
            List <double>  xh          = Convolution.ComputeSignal(points, H).Take(points.Count).ToList();
            List <double>  xg          = Convolution.ComputeSignal(points, G).Take(points.Count).ToList();
            List <double>  xhHalf      = new List <double>();
            List <double>  xgHalf      = new List <double>();

            for (int i = 0; i < xh.Count; i++)
            {
                if (i % 2 == 0)
                {
                    xhHalf.Add(xh[i]);
                }
                else
                {
                    xgHalf.Add(xg[i]);
                }
            }
            for (int i = 0; i < xgHalf.Count; i++)
            {
                transformed.Add(new Complex(xhHalf[i], xgHalf[i]));
            }
            return(transformed);
        }
예제 #6
0
        public void TestBrickwall()
        {
            var saw = Utils.Saw(1024, 1.0f);

            var sine1 = Utils.Sinewave(1024, 0, 1 / 1024.0f * 2.0f * (double)Math.PI, 1.0f);
            var sine2 = Utils.Sinewave(1024, 0, 2 / 1024.0f * 2.0f * (double)Math.PI, 1.0f);
            //var sine1 = Utils.Sinewave(1024, 0, 1 / 1024.0f * 2.0f * (double)Math.PI, 1.0f);
            //var sine1 = Utils.Sinewave(1024, 0, 1 / 1024.0f * 2.0f * (double)Math.PI, 1.0f);

            for (int i = 0; i < saw.Length; i++)
                saw[i] = sine1[i] + sine2[i] + 0.02f;

            var kernel = Utils.SincFilter(1/2024.0f, 256, "Blackman");

            var s = new Stopwatch();
            s.Restart();
            var filtered = Convolution.ConvSimpleCircular(saw, kernel);
            s.Stop();

            //MessageBox.Show("Millisec: " + s.ElapsedMilliseconds);

            Line l = new Line(Utils.Linspace(-1, 1, saw.Length), saw); l.LinePen = Pens.Blue;
            Line l2 = new Line(Utils.Linspace(-1, 1, filtered.Length), filtered); l2.LinePen = Pens.Red;
            Line l3 = new Line(Utils.Linspace(-1, 1, saw.Length), sine1); l3.LinePen = Pens.Green;
            Line l4 = new Line(Utils.Linspace(-1, 1, saw.Length), sine2); l4.LinePen = Pens.Gray;
            var list = new List<Line>() { l, l2, l3, l4 };
            Plot.Plot.ShowPlot(list);
        }
예제 #7
0
        public static Bitmap Gaussian(Bitmap bmp, double sigma)
        {
            int width, height;

            int[][][] img, newImg;
            Bitmap    newBmp;

            ImageConvert.Bitmap2MatColor(bmp, out img, out width, out height);

            newImg = new int[3][][];

            int        n      = 4;
            ConvKernel kernel = Util.GetGaussianKernal(sigma, n);

            Convolution conv = new Convolution();

            // RGB
            conv.Calculate(img[0], kernel, out newImg[0]);
            conv.Calculate(img[1], kernel, out newImg[1]);
            conv.Calculate(img[2], kernel, out newImg[2]);

            ImageConvert.Mat2BitmapColor(newImg, width, height, out newBmp);

            return(newBmp);
        }
예제 #8
0
        private void CameraOne_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap    bitmap1 = (Bitmap)(eventArgs.Frame.DeepClone());
            Grayscale filter  = new Grayscale(0.2, 0.7, 0.07);

            if (checkBox1.Checked)
            {
                bitmap1 = filter.Apply(bitmap1);
            }
            int x = 0;

            Invoke(new MethodInvoker(delegate
            {
                x = trackBar1.Value;
            }));
            RotateNearestNeighbor filterrot = new RotateNearestNeighbor(x, true);

            bitmap1 = filterrot.Apply(bitmap1);
            ResizeBicubic filterResizeBicubic = new ResizeBicubic(320, 240);

            bitmap1 = filterResizeBicubic.Apply(bitmap1);
            if (button1WasClicked)
            {
                Convolution conv = new Convolution(new[, ]
                {
                    { int.Parse(textBox1.Text), int.Parse(textBox2.Text), int.Parse(textBox3.Text) },
                    { int.Parse(textBox4.Text), int.Parse(textBox5.Text), int.Parse(textBox6.Text) },
                    { int.Parse(textBox7.Text), int.Parse(textBox8.Text), int.Parse(textBox9.Text) }
                });
                bitmap1 = conv.Apply(bitmap1);
            }
            pictureBox1.Image = bitmap1;
        }
예제 #9
0
        public void ExecuteTest()
        {
            float max_err = 0;

            foreach (int batch in new int[] { 1, 2 })
            {
                foreach (int inchannels in new int[] { 1, 2, 3, 4, 5, 10, 15, 20 })
                {
                    foreach (int outchannels in new int[] { 7, 13 })
                    {
                        foreach (int kheight in new int[] { 1, 3, 5 })
                        {
                            foreach (int kwidth in new int[] { 1, 3, 5 })
                            {
                                foreach (int stride in new int[] { 1, 2, 3 })
                                {
                                    foreach (int inwidth in new int[] { 8, 9, 13, 17 })
                                    {
                                        foreach (int inheight in new int[] { 8, 9, 19, 23 })
                                        {
                                            int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1;

                                            float[] xval = (new float[inwidth * inheight * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                                            float[] wval = (new float[kwidth * kheight * inchannels * outchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

                                            Map2D    x = new Map2D(inchannels, inwidth, inheight, batch, xval);
                                            Filter2D w = new Filter2D(inchannels, outchannels, kwidth, kheight, wval);

                                            Map2D y = Reference(x, w, kwidth, kheight, stride);

                                            OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map2D(inchannels, inwidth, inheight, batch), xval);
                                            OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel2D(inchannels, outchannels, kwidth, kheight), wval);

                                            OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map2D(outchannels, outwidth, outheight, batch));

                                            Convolution ope = new Convolution(inwidth, inheight, inchannels, outchannels, kwidth, kheight, stride, batch);

                                            ope.Execute(x_tensor, w_tensor, y_tensor);

                                            float[] y_expect = y.ToArray();
                                            float[] y_actual = y_tensor.State;

                                            CollectionAssert.AreEqual(xval, x_tensor.State);
                                            CollectionAssert.AreEqual(wval, w_tensor.State);

                                            AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {inchannels},{outchannels},{kwidth},{kheight},{stride},{inwidth},{inheight},{batch}");

                                            Console.WriteLine($"pass: {inchannels},{outchannels},{kwidth},{kheight},{stride},{inwidth},{inheight},{batch}");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Console.WriteLine($"maxerr:{max_err}");
        }
예제 #10
0
        public static Bitmap Convolution(Bitmap Imagem, int[,] kernel, int divisor)
        {
            Convolution filter = new Convolution(kernel, divisor);

            Imagem = Imagem.Clone(new Rectangle(0, 0, Imagem.Width, Imagem.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Imagem = filter.Apply(Imagem);
            return(Imagem);
        }
예제 #11
0
        private LayerBase CreateLayer()
        {
            var layer = new Convolution(new Shape(5, 5, 3), 3, 10, 1, null);

            layer.Init();
            layer.Kernels.FillWithRand();
            return(layer);
        }
예제 #12
0
        public void SimpleLongTest([Range(1, 80)] int n, [Range(1, 80)] int m)
        {
            var random = new Random(19937);
            var a      = new long[n].Select(_ => random.Next() % (long)1e6 - (long)5e5).ToArray();
            var b      = new long[m].Select(_ => random.Next() % (long)1e6 - (long)5e5).ToArray();

            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));
        }
예제 #13
0
        public void ConvolutionDoesNotChangeSize()
        {
            Size originalSize = _sourceImage.Size;

            Bitmap newImage   = Convolution.ConvolutionFilter(_convolutionParams);
            Size   actualSize = newImage.Size;

            Assert.AreEqual(originalSize, actualSize);
        }
예제 #14
0
        public void ConvoluteTest()
        {
            var convolution = Convolution.Convolute(first, second).ToList();

            for (int i = 0; i < 6; i++)
            {
                Assert.AreEqual(convolution[i].Y, result[i].Y);
            }
        }
예제 #15
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var A = Scanner.ScanEnumerable <long>().Select(x => new MInt(x)).ToArray();
            var B = Scanner.ScanEnumerable <long>().Select(x => new MInt(x)).ToArray();
            var c = Convolution.Execute(A, B);

            Console.WriteLine(string.Join(" ", c));
        }
예제 #16
0
        public void Test1()
        {
            Convolution cv = new Convolution(2, 2);
            var         m  = Matrix <double> .Build.Dense(5, 5, 0.5);

            var r = cv.apply(m);

            Assert.Pass();
        }
예제 #17
0
        public void MiddleTest()
        {
            ModuloInteger.Modulo = 998244353;
            var random = new Random(19937);
            var a      = new ModuloInteger[1234].Select(_ => (ModuloInteger)random.Next()).ToArray();
            var b      = new ModuloInteger[2345].Select(_ => (ModuloInteger)random.Next()).ToArray();

            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));
        }
예제 #18
0
        public void Modulo641Test()
        {
            const int mod = 641;

            ModuloInteger.Modulo = mod;
            var a = new ModuloInteger[64].Select(_ => (ModuloInteger)Utilities.RandomInteger(0, mod - 1)).ToArray();
            var b = new ModuloInteger[65].Select(_ => (ModuloInteger)Utilities.RandomInteger(0, mod - 1)).ToArray();

            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));
        }
예제 #19
0
        public void LongBoundMinMaxValueTest([Range(0, 1000)] int n)
        {
            var a = new[] { long.MinValue + n };
            var b = new[] { 1L };

            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));

            a = new[] { long.MaxValue - n };
            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));
        }
예제 #20
0
        public void TSSetUp()
        {
            l1 = Utility.Generate <Link>(() => new Link(), 1176).ToArray();
            l2 = Utility.Generate <Neuron>(() => new Neuron(new Sigmoid()), 1600).ToArray();
            Func <double> wg = () => Weight++;

            Bias = new Bias(1);
            Cma  = new Convolution(5, 16, 6, 4, GetSchema());
            Cma.Connect(l1, l2, Bias);
        }
예제 #21
0
 public static Bitmap Emboss(Bitmap input)
 {
     //emboss efect kernel
     int[,] kernel = {
     { -2, -1,  0 },
     { -1,  1,  1 },
     {  0,  1,  2 } };
     Convolution convultionFilters = new Convolution(kernel);
     return convultionFilters.Apply(input);
 }
예제 #22
0
 public ConvolutionFilter()
 {
     convolution = new Convolution(new int[, ] {
         { 1, 2, 3, 2, 1 },
         { 2, 4, 5, 4, 2 },
         { 3, 5, 6, 5, 3 },
         { 2, 4, 5, 4, 2 },
         { 1, 2, 3, 2, 1 }
     });
 }
        public void When_calling_ConvolvedTracings_Then_the_correct_instance_Should_be_returned()
        {
            // Arrange
            var subject = new Convolution <double>(_shape1, _shape2, _convolvedTracings);

            var actual = subject.ConvolvedTracings;

            // Assert
            actual.Should().BeSameAs(_convolvedTracings);
        }
예제 #24
0
 public void Convolute()
 {
     if (SelectedSignal1 != null && SelectedSignal1.HasData() && SelectedSignal2 != null && SelectedSignal2.HasData())
     {
         SampledSignal signal = new SampledSignal();
         signal.PointsY = Convolution.ComputeSignal(SelectedSignal1.PointsY, SelectedSignal2.PointsY);
         signal.Name    = $"({SelectedSignal1.Name})*({SelectedSignal2.Name})";
         SignalCreator.AddSignal(signal);
     }
 }
예제 #25
0
 public void FilterMethod()
 {
     if (SelectedSignalFilter1 != null && SelectedSignalFilter1.HasData() && SelectedSignalFilter2 != null && SelectedSignalFilter2.HasData())
     {
         SampledSignal signal = new SampledSignal();
         signal.PointsY = Convolution.ComputeSignal(SelectedSignalFilter1.PointsY, SelectedSignalFilter2.PointsY).Skip((SelectedSignalFilter2.PointsY.Count - 1) / 2).Take(SelectedSignalFilter1.PointsY.Count).ToList();
         signal.Name    = $"({SelectedSignalFilter1.Name})*({SelectedSignalFilter2.Name})";
         SignalCreator.AddSignal(signal);
     }
 }
예제 #26
0
        public void Proceed()
        {
            if (IsAllChecked())
            {
                FinalSignal = Convolution.Convolute(FirstSignal, SecondSignal);

                BindCharts();

                OutputSignal.Text = OuputString;
            }
        }
예제 #27
0
        public void SimpleModuloIntegerTest([Values(998244353, 924844033)] int mod,
                                            [Range(1, 20)] int n, [Range(1, 20)] int m)
        {
            var random = new Random(19937);

            ModuloInteger.Modulo = mod;
            var a = new ModuloInteger[n].Select(_ => (ModuloInteger)random.Next()).ToArray();
            var b = new ModuloInteger[m].Select(_ => (ModuloInteger)random.Next()).ToArray();

            Assert.That(Convolution.Execute(a, b), Is.EqualTo(ConvolutionNaive(a, b)));
        }
예제 #28
0
        public MainWindow()
        {
            InitializeComponent();

            string path = @"E:\Studing\Магістратура\НМШІ\Курсова\letters\letters2\02_52.png";
            //string path = @"C:\Users\Vitalii Rozbyiholova\Downloads\8-pink-butterfly-png-image-butterflies.png";
            Bitmap            imageBitmap       = new Bitmap(path);
            ConvolutionParams convolutionParams = new ConvolutionParams(imageBitmap, Convolution.Matrix.Edge3x3);
            Bitmap            outBitmap         = Convolution.ConvolutionFilter(convolutionParams);

            OutImage.Source = BitmapToImageSource(outBitmap);
        }
예제 #29
0
        //ApplySmooth(ref b, 1);
        public static void ApplySmooth(ref Bitmap bmp, int weight)
        {
            ConvolutionMatrix m = new ConvolutionMatrix();

            m.Apply(1);
            m.Pixel  = weight;
            m.Factor = weight + 8;

            Convolution C = new Convolution();

            C.Matrix = m;
            C.Convolution3x3(ref bmp);
        }
예제 #30
0
        //ApplyMeanRemoval(ref b, 9);
        public static void ApplyMeanRemoval(ref Bitmap bmp, int weight)
        {
            ConvolutionMatrix m = new ConvolutionMatrix();

            m.Apply(-1);
            m.Pixel  = weight;
            m.Factor = weight - 8;

            Convolution C = new Convolution();

            C.Matrix = m;
            C.Convolution3x3(ref bmp);
        }
예제 #31
0
		// ====================================================================
		// Description:	Convolution - Prewitt Y
		// Return		void
		void Convolution_PrewittY ( object sender, EventArgs ev )
		// ====================================================================
		{
			try
			{
				if ( m_list.Count > 0 )
				{
					ImageView fm = (ImageView)this.ActiveMdiChild;
			
					if ( fm.m_Img.PixelFormat != PixelFormat.Format24bppRgb )
					{
						MessageBoxButtons buttons = MessageBoxButtons.OK;
						MessageBox.Show(this, "Image Depth != 24bpp", null, buttons,
							MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
							MessageBoxOptions.RightAlign);
						return;
					}
					Convolution dlg = new Convolution();
					dlg.MdiParent = this;

					dlg.OnInitForm( fm, (int)Kernel.Prewitt_Y );
					dlg.Show();
					dlg.Location = new Point (this.Width-dlg.Width-PAD, 0 );
				}
			}
			catch(Exception e)
			{
				DisplayError("Convolution_PrewittY() failed " + e.ToString() );
			}
		}
예제 #32
0
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        /// 
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width = sourceData.Width;
            int height = sourceData.Height;
            PixelFormat format = sourceData.PixelFormat;
            int pixelSize = System.Drawing.Bitmap.GetPixelFormatSize(format) / 8;

            sourceData.Clone();

            UnmanagedImage temp = UnmanagedImage.Create(width, height, format);

            int lineWidth = width * pixelSize;

            int srcStride = temp.Stride;
            int srcOffset = srcStride - lineWidth;
            int dstStride = destinationData.Stride;
            int dstOffset = dstStride - lineWidth;

            byte* srcStart = (byte*)temp.ImageData.ToPointer();
            byte* dstStart = (byte*)destinationData.ImageData.ToPointer();


            // first
            Convolution c = new Convolution(masks[0]);
            c.Apply(sourceData, destinationData);

            // others
            for (int i = 1; i < masks.Length; i++)
            {
                c.Kernel = masks[i];
                c.Apply(sourceData, temp);

                byte* src = srcStart;
                byte* dst = dstStart;

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < lineWidth; x++, src++, dst++)
                    {
                        if (*src > *dst)
                            *dst = *src;
                    }

                    dst += dstOffset;
                    src += srcOffset;
                }
            }
        }
예제 #33
0
        /// <summary>
        ///   Process the filter on the specified image.
        /// </summary>
        /// 
        /// <param name="sourceData">Source image data.</param>
        /// <param name="destinationData">Destination image data.</param>
        /// 
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int width = sourceData.Width;
            int height = sourceData.Height;

            int pixelSize = System.Drawing.Image.GetPixelFormatSize(sourceData.PixelFormat) / 8;

            int srcStride = sourceData.Stride;
            int dstStride = destinationData.Stride;

            int srcOffset = srcStride - width * pixelSize;
            int dstOffset = dstStride - width * pixelSize;

            byte* src = (byte*)sourceData.ImageData.ToPointer();
            byte* dst = (byte*)destinationData.ImageData.ToPointer();

            // TODO: Move or cache the creation of those filters
            int[,] kernel = Accord.Math.Matrix.Create(radius * 2 + 1, radius * 2 + 1, 1);
            Convolution conv = new Convolution(kernel);
            FastVariance fv = new FastVariance(radius);

            // Mean filter
            UnmanagedImage mean = conv.Apply(sourceData);

            // Variance filter
            UnmanagedImage var = fv.Apply(sourceData);

            // do the processing job
            if (sourceData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                byte* srcVar = (byte*)var.ImageData.ToPointer();
                byte* srcMean = (byte*)mean.ImageData.ToPointer();

                // Store maximum value from variance.
                int maxV = Max(width, height, srcVar, srcOffset);

                // Store minimum value from image.
                int minG = Min(width, height, src, srcOffset);

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++, src++, srcMean++, srcVar++, dst++)
                    {
                        double mP = *srcMean;
                        double vP = *srcVar;

                        double threshold = (mP + k * ((Math.Sqrt(vP) / (double)maxV - 1.0) * (mP - (double)minG)));

                        *dst = (byte)(*src > threshold ? 255 : 0);
                    }

                    src += srcOffset;
                    srcMean += srcOffset;
                    srcVar += srcOffset;
                    dst += dstOffset;
                }
            }
        }