SampleApp.MainForm.btnBagOfWords_Click C# (CSharp) Method

btnBagOfWords_Click() private method

This methods computes the Bag-of-Visual-Words with the training images.
private btnBagOfWords_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private void btnBagOfWords_Click(object sender, EventArgs e)
        {
            int numberOfWords = (int)numWords.Value;


            // Create a Binary-Split clustering algorithm
            BinarySplit binarySplit = new BinarySplit(numberOfWords);

            Stopwatch sw1 = Stopwatch.StartNew();

            IBagOfWords<Bitmap> bow;

            if (rbSurf.Checked)
            {
                // Create bag-of-words (BoW) with the given algorithm
                var surfBow = new BagOfVisualWords(binarySplit);

                // Compute the BoW codebook using training images only
                surfBow.Compute(originalTrainImages.Values.ToArray());

                bow = surfBow;
            }

            else
            {

                // Alternative creation using the FREAK detector

                // Create a Binary-Split clustering algorithm
                var kmodes = new KModes<byte>(numberOfWords, new Hamming());
                var detector = new FastRetinaKeypointDetector();

                // Create bag-of-words (BoW) with the given algorithm
                var freakBow = new BagOfVisualWords<FastRetinaKeypoint, byte[]>(detector, kmodes);

                // Compute the BoW codebook using training images only
                freakBow.Compute(originalTrainImages.Values.ToArray());

                bow = freakBow;
            }

            sw1.Stop();

            Stopwatch sw2 = Stopwatch.StartNew();

            // Extract features for all images
            foreach (ListViewItem item in listView1.Items)
            {
                // Get item image
                Bitmap image = originalImages[item.ImageKey] as Bitmap;

                // Process image
                double[] featureVector = bow.GetFeatureVector(image);
                string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture);

                if (item.SubItems.Count == 2)
                    item.SubItems[1].Text = featureString;
                else item.SubItems.Add(featureString);

                int classLabel = (item.Tag as Tuple<double[], int>).Item2;
                item.Tag = Tuple.Create(featureVector, classLabel);
            }

            sw2.Stop();

            lbStatus.Text = "BoW constructed in " + sw1.Elapsed + "s. Features extracted in " + sw2.Elapsed + "s.";
            btnSampleRunAnalysis.Enabled = true;
        }
MainForm