Beispiel #1
0
 private void addAllImagesToDBToolStripMenuItem_Click(object sender, EventArgs e)
 {
     FBD = new FolderBrowserDialog();
     if (FBD.ShowDialog() == DialogResult.OK)
     {
         DirectoryInfo myFolder = new DirectoryInfo(FBD.SelectedPath);
         foreach (string filename in Directory.GetFiles(FBD.SelectedPath))
         {
             bitmap = new Bitmap(filename);
             hog    = new HistogramsOfOrientedGradients();
             hog.ProcessImage(bitmap);
             human = new Human();
             if (filename.Contains("image_human"))
             {
                 human.IsHuman = 1;
             }
             else
             {
                 human.IsHuman = 0;
             }
             line       = AuxiliaryFunctions.ToOneLine(hog.Histograms);
             byteArray  = AuxiliaryFunctions.DoubleArrayToByte(line);
             human.HOG  = byteArray;
             humanModel = new HumanModel();
             humanModel.Insert(human);
         }
     }
 }
Beispiel #2
0
        /// <summary>
        /// Extracts the hog accord.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns></returns>
        public IList <double> ExtractHogAccord(Bitmap image)
        {
            var hog = new HistogramsOfOrientedGradients();

            hog.ProcessImage(image);

            var lined = hog.Histograms;

            return(ToOneLine(lined));
        }
        public void MagnitudeDirectionTest()
        {
            byte[,] gradient = new byte[255, 255];

            for (int i = 0; i < 255; i++)
            {
                for (int j = 0; j < 255; j++)
                {
                    gradient[i, j] = (byte)j;
                }
            }

            UnmanagedImage output;

            new MatrixToImage().Convert(gradient, out output);

            //ImageBox.Show(output);
            var hog    = new HistogramsOfOrientedGradients();
            var result = hog.ProcessImage(output);

            float[,] actualDir = hog.Direction;
            Assert.AreEqual(255, actualDir.GetLength(0));
            Assert.AreEqual(255, actualDir.GetLength(1));

            for (int i = 0; i < 255; i++)
            {
                for (int j = 0; j < 255; j++)
                {
                    Assert.AreEqual(0, actualDir[i, j]);
                }
            }

            float[,] actualMag = hog.Magnitude;
            Assert.AreEqual(255, actualMag.GetLength(0));
            Assert.AreEqual(255, actualMag.GetLength(1));

            for (int i = 0; i < 255; i++)
            {
                for (int j = 0; j < 255; j++)
                {
                    if (i == 0 || j == 0 || i == 254 || j == 254)
                    {
                        Assert.AreEqual(0, actualMag[i, j]);
                    }
                    else
                    {
                        Assert.AreEqual(1, actualMag[i, j]);
                    }
                }
            }
        }
Beispiel #4
0
        private void addAllImagesToDBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            humanList = new List <Human>();
            if (FBD.ShowDialog() == DialogResult.OK)
            {
                DirectoryInfo myFolder = new DirectoryInfo(FBD.SelectedPath);
                fileCount = Directory.GetFiles(FBD.SelectedPath).Count();
                InitializeProgressBar();
                foreach (string filename in Directory.GetFiles(FBD.SelectedPath))
                {
                    bitmap = new Bitmap(filename);
                    hog    = new HistogramsOfOrientedGradients();
                    hog.ProcessImage(bitmap);
                    human = new Human();
                    if (filename.Contains("image_human"))
                    {
                        human.IsHuman = 1;
                    }
                    else
                    {
                        human.IsHuman = -1;
                    }
                    line      = AuxiliaryFunctions.ToOneLine(hog.Histograms);
                    byteArray = AuxiliaryFunctions.DoubleArrayToByte(line);
                    human.HOG = byteArray;

                    //XML file
                    humanList.Add(human);


                    index++;

                    if (index % (fileCount / 100) == 0)
                    {
                        progressValue++;
                        label1.Text = (progressValue + " %").ToString();
                        pb.Value    = progressValue <= 100?progressValue:100;
                        Thread.Sleep(10);
                    }
                }
                if (!File.Exists("Database.xml"))
                {
                    AuxiliaryFunctions.Serialize(humanList, "Database.xml");
                }
                else
                {
                    humanList.AddRange(AuxiliaryFunctions.Deserialize <List <Human> >("Database.xml"));
                    AuxiliaryFunctions.Serialize(humanList, "Database.xml");
                }
            }
        }
        public void CloneTest()
        {
            var images = GetImages();

            var hog    = new HistogramsOfOrientedGradients();
            var clone1 = (HistogramsOfOrientedGradients)hog.Clone();

            List <double[]> features1 = hog.ProcessImage(images[0]);

            Assert.AreEqual(features1.Count, 2352);

            List <double[]> features2 = clone1.ProcessImage(images[0]);

            Assert.AreEqual(features2.Count, 2352);
            Assert.IsTrue(features1.ToArray().IsEqual(features2.ToArray()));

            var             clone2    = (HistogramsOfOrientedGradients)hog.Clone();
            List <double[]> features3 = clone2.ProcessImage(images[0]);

            Assert.AreEqual(features3.Count, 2352);
            Assert.IsTrue(features1.ToArray().IsEqual(features3.ToArray()));
        }
        public static void OnePassOfWindow(System.Drawing.Image src, int width, int height)
        {
            Bitmap    bmpImage = new Bitmap(src);
            Rectangle cropRect = new Rectangle();
            Bitmap    newImage = new Bitmap(64, 128);

            for (int i = 0; i < src.Height - height; i += height / 4)
            {
                for (int j = 0; j < src.Width - width; j += width / 4)
                {
                    cropRect = new Rectangle(j, i, width, height);
                    newImage = bmpImage.Clone(cropRect, bmpImage.PixelFormat);
                    newImage = (Bitmap)ImageFunctions.ScaleImage(newImage, 64, 128);

                    HistogramsOfOrientedGradients hog = new HistogramsOfOrientedGradients();
                    hog.ProcessImage(newImage);
                    double[, ][] hogHistogram = hog.Histograms;

                    CompareHOG(hogHistogram);
                }
            }
        }
        public void MagnitudeDirectionTest()
        {
            byte[,] gradient = new byte[255, 255];

            for (int i = 0; i < 255; i++)
                for (int j = 0; j < 255; j++)
                    gradient[i, j] = (byte)j;

            UnmanagedImage output;
            new MatrixToImage().Convert(gradient, out output);

            //ImageBox.Show(output);
            HistogramsOfOrientedGradients hog = new HistogramsOfOrientedGradients();
            var result = hog.ProcessImage(output);

            float[,] actualDir = hog.Direction;
            Assert.AreEqual(255, actualDir.GetLength(0));
            Assert.AreEqual(255, actualDir.GetLength(1));

            for (int i = 0; i < 255; i++)
                for (int j = 0; j < 255; j++)
                    Assert.AreEqual(0, actualDir[i, j]);

            float[,] actualMag = hog.Magnitude;
            Assert.AreEqual(255, actualMag.GetLength(0));
            Assert.AreEqual(255, actualMag.GetLength(1));

            for (int i = 0; i < 255; i++)
            {
                for (int j = 0; j < 255; j++)
                {
                    if (i == 0 || j == 0 || i == 254 || j == 254)
                        Assert.AreEqual(0, actualMag[i, j]);
                    else
                        Assert.AreEqual(1, actualMag[i, j]);
                }
            }
        }
Beispiel #8
0
        public static void OnePassOfWindow(System.Drawing.Image src, int width, int height, int step)
        {
            Bitmap bmpImage = new Bitmap(src);

            cropRect = new Rectangle();
            Bitmap newImage = new Bitmap(64, 128);

            for (int i = 0; i < src.Height - height; i += step)
            {
                for (int j = 0; j < src.Width - width; j += step)
                {
                    cropRect = new Rectangle(j, i, width, height);
                    newImage = bmpImage.Clone(cropRect, bmpImage.PixelFormat);
                    newImage = (Bitmap)ImageFunctions.ScaleImage(newImage, 64, 128);

                    HistogramsOfOrientedGradients hog = new HistogramsOfOrientedGradients();
                    hog.ProcessImage(newImage);
                    double[, ][] hogHistogram = hog.Histograms;
                    bool         t            = CompareHOG(hogHistogram);
                    if (t)
                    {
                        rectangleList.Add(cropRect);
                        newImage.Save(@"Output\image_" + Counter + ".png");
                    }
                    Counter += 1;

                    if (Counter % (ScanForm.countIter / 100) == 0)
                    {
                        progressValue++;
                        if (progressValue <= 100)
                        {
                            ScanForm.pb.Value = progressValue;
                        }
                        System.Threading.Thread.Sleep(10);
                    }
                }
            }
        }
        public void doc_test()
        {
            string localPath = TestContext.CurrentContext.TestDirectory;

            #region doc_apply
            // Let's load an example image, such as Lena,
            // from a standard dataset of example images:
            var    images = new TestImages(path: localPath);
            Bitmap lena   = images["lena.bmp"];

            // Create a new Histogram of Oriented Gradients with the default parameter values:
            var hog = new HistogramsOfOrientedGradients(numberOfBins: 9, blockSize: 3, cellSize: 6);

            // Use it to extract descriptors from the Lena image:
            List <double[]> descriptors = hog.ProcessImage(lena);

            // Now those descriptors can be used to represent the image itself, such
            // as for example, in the Bag-of-Visual-Words approach for classification.
            #endregion

            Assert.AreEqual(784, descriptors.Count);
            double sum = descriptors.Sum(x => x.Sum());
            Assert.AreEqual(3359.1014569812564, sum, 1e-3);
        }
Beispiel #10
0
        /// <summary>
        ///   This methods computes the Bag-of-Visual-Words with the training images.
        /// </summary>
        ///
        private void btnBagOfWords_Click(object sender, EventArgs e)
        {
            int numberOfWords = (int)numWords.Value;


            Stopwatch sw1 = Stopwatch.StartNew();

            IBagOfWords <Bitmap> bow;

            // Check if we will use SURF or FREAK as the feature detector
            if (rbSurf.Checked)
            {
                // We will use SURF, so we can use a standard clustering
                // algorithm that is based on Euclidean distances. A good
                // algorithm for clustering codewords is the Binary Split
                // variant of the K-Means algorithm.

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

                // Create bag-of-words (BoW) with the given algorithm
                BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit);

                // Compute the BoW codebook using training images only
                bow = surfBow.Learn(originalTrainImages.Values.ToArray());
            }
            else if (rbFreak.Checked)
            {
                // We will use the FREAK detector. The features generated by FREAK
                // are represented as bytes. While it is possible to transform those
                // to standard double vectors, we will demonstrate how to use a non-
                // Euclidean distance based algorithm to generate codewords for it.

                // Note: Using Binary-Split with normalized FREAK features would
                // possibly work better than the k-modes. This is just an example.

                // Create a k-Modes clustering algorithm
                var kmodes = new KModes <byte>(numberOfWords, new Hamming());

                // Create a FREAK detector explicitly (if no detector was passed,
                // the BagOfVisualWords would be using a SURF detector by default).
                var freak = new FastRetinaKeypointDetector();

                // Create bag-of-words (BoW) with the k-modes clustering and FREAK detector
                var freakBow = new BagOfVisualWords <FastRetinaKeypoint, byte[]>(freak, kmodes);

                // Compute the BoW codebook using training images only
                bow = freakBow.Learn(originalTrainImages.Values.ToArray());
            }
            else
            {
                // We will use HOG, so we can use a standard clustering
                // algorithm that is based on Euclidean distances. A good
                // algorithm for clustering codewords is the Binary Split
                // variant of the K-Means algorithm.

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

                // Create a HOG detector explicitly (if no detector was passed,
                // the BagOfVisualWords would be using a SURF detector by default).
                var hog = new HistogramsOfOrientedGradients();

                // Create bag-of-words (BoW) with the given algorithm
                var hogBow = BagOfVisualWords.Create(hog, binarySplit);

                // Compute the BoW codebook using training images only
                bow = hogBow.Learn(originalTrainImages.Values.ToArray());
            }

            sw1.Stop();

            // Now that we have already created and computed the BoW model, we
            // will use it to extract representations for each of the images in
            // both training and testing sets.

            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;

                // Get a feature vector representing this image
                double[] featureVector = (bow as ITransform <Bitmap, double[]>).Transform(image);

                // Represent it as a string so we can show it onscreen
                string featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture);

                // Show it in the visual grid
                if (item.SubItems.Count == 2)
                {
                    item.SubItems[1].Text = featureString;
                }
                else
                {
                    item.SubItems.Add(featureString);
                }

                // Retrieve the class labels, that we had stored in the Tag
                int classLabel = (item.Tag as Tuple <double[], int>).Item2;

                // Now, use the Tag to store the feature vector too
                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;
        }