Inheritance: IRadiallySymmetricKernel
        public void MeanShiftConstructorTest()
        {
            Accord.Math.Tools.SetupGenerator(0);

            // Test Samples
            double[][] samples =
            {
                new double[] { 0, 1 },
                new double[] { 1, 2 }, 
                new double[] { 1, 1 },
                new double[] { 0, 7 },
                new double[] { 1, 1 },
                new double[] { 6, 2 },
                new double[] { 6, 5 },
                new double[] { 5, 1 },
                new double[] { 7, 1 },
                new double[] { 5, 1 }
            };


            var kernel = new GaussianKernel(dimension: 2);
            MeanShift meanShift = new MeanShift(2, kernel, 3);

            // Compute the model (estimate)
            int[] labels = meanShift.Compute(samples);

            int a = 0;
            int b = 1;

            if (0.2358896594197982.IsRelativelyEqual(meanShift.Clusters.Modes[1][0], 1e-10))
            {
                a = 1;
                b = 0;
            }

            for (int i = 0; i < 5; i++)
                Assert.AreEqual(a, labels[i]);

            for (int i = 5; i < samples.Length; i++)
                Assert.AreEqual(b, labels[i]);

            Assert.AreEqual(0.2358896594197982, meanShift.Clusters.Modes[a][0], 1e-10);
            Assert.AreEqual(1.0010865560750339, meanShift.Clusters.Modes[a][1], 1e-10);

            Assert.AreEqual(6.7284908155626031, meanShift.Clusters.Modes[b][0], 1e-10);
            Assert.AreEqual(1.2713970467590967, meanShift.Clusters.Modes[b][1], 1e-10);

            Assert.AreEqual(2, meanShift.Clusters.Count);
            Assert.AreEqual(2, meanShift.Clusters.Modes.Length);
        }
Esempio n. 2
0
        public void MeanShiftConstructorTest()
        {
            Accord.Math.Random.Generator.Seed = 0;

            // Test Samples
            double[][] samples =
            {
                new double[] { 0, 1 },
                new double[] { 1, 2 }, 
                new double[] { 1, 1 },
                new double[] { 0, 7 },
                new double[] { 1, 1 },
                new double[] { 6, 2 },
                new double[] { 6, 5 },
                new double[] { 5, 1 },
                new double[] { 7, 1 },
                new double[] { 5, 1 }
            };


            var kernel = new GaussianKernel(dimension: 2);
            MeanShift meanShift = new MeanShift(2, kernel, 2.0);
            meanShift.UseParallelProcessing = false;

            // Compute the model (estimate)
            int[] labels = meanShift.Compute(samples);

            int a = labels[0];
            int b = (a == 0) ? 1 : 0;

            for (int i = 0; i < 5; i++)
                Assert.AreEqual(a, labels[i]);

            for (int i = 5; i < samples.Length; i++)
                Assert.AreEqual(b, labels[i]);

            Assert.AreEqual(1.1922811512028066, meanShift.Clusters.Modes[a][0], 1e-3);
            Assert.AreEqual(1.2567196159235963, meanShift.Clusters.Modes[a][1], 1e-3);

            Assert.AreEqual(5.2696337859175868, meanShift.Clusters.Modes[b][0], 1e-3);
            Assert.AreEqual(1.4380326532534968, meanShift.Clusters.Modes[b][1], 1e-3);

            Assert.AreEqual(2, meanShift.Clusters.Count);
            Assert.AreEqual(2, meanShift.Clusters.Modes.Length);

            Assert.AreEqual(0.5, meanShift.Clusters.Proportions[0]);
            Assert.AreEqual(0.5, meanShift.Clusters.Proportions[1]);
        }
Esempio n. 3
0
        /// <summary>
        ///   Runs the Mean-Shift algorithm.
        /// </summary>
        /// 
        private void runMeanShift()
        {
            int pixelSize = 3;

            // Retrieve the kernel bandwidth
            double sigma = (double)numBandwidth.Value;

            // Load original image
            Bitmap image = Properties.Resources.leaf;

            // Create converters
            ImageToArray imageToArray = new ImageToArray(min: -1, max: +1);
            ArrayToImage arrayToImage = new ArrayToImage(image.Width, image.Height, min: -1, max: +1);

            // Transform the image into an array of pixel values
            double[][] pixels; imageToArray.Convert(image, out pixels);


            // Create a MeanShift algorithm using the given bandwidth
            // and a Gaussian density kernel as the kernel function:

            IRadiallySymmetricKernel kernel = new GaussianKernel(pixelSize);
            
            var meanShift = new MeanShift(pixelSize, kernel, sigma)
            {
                Tolerance = 0.05,
                MaxIterations = 10
            };

            
            // Compute the mean-shift algorithm until the difference 
            // in shift vectors between two iterations is below 0.05
            
            int[] idx = meanShift.Compute(pixels);


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => meanShift.Clusters.Modes[idx[i]]);

            // Show resulting image in the picture box
            Bitmap result; arrayToImage.Convert(pixels, out result);

            pictureBox.Image = result;
        }