SampleApp.MainForm.runMeanShift C# (CSharp) Method

runMeanShift() private method

Runs the Mean-Shift algorithm.
private runMeanShift ( ) : void
return void
        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.Learn(pixels).Decide(pixels);


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

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

            pictureBox.Image = result;
        }
MainForm