Ejemplo n.º 1
0
        public mTextureClouds()
        {
            Pattern = new CloudsTexture();

            Texture = Pattern;
        }
Ejemplo n.º 2
0
        public void custom_feature_test_haralick()
        {
            #region doc_feature_haralick
            // Ensure results are reproducible
            Accord.Math.Random.Generator.Seed = 0;

            // The Bag-of-Visual-Words model converts images of arbitrary
            // size into fixed-length feature vectors. In this example, we
            // will be setting the codebook size to 3. This means all feature
            // vectors that will be generated will have the same length of 3.

            // By default, the BoW object will use the sparse SURF as the
            // feature extractor and K-means as the clustering algorithm.
            // In this example, we will use the Haralick feature extractor.

            // Create a new Bag-of-Visual-Words (BoW) model using Haralick features
            var bow = BagOfVisualWords.Create(new Haralick()
            {
                CellSize = 256, // divide images in cells of 256x256 pixels
                Mode     = HaralickMode.AverageWithRange,
            }, new KMeans(3));

            // Generate some training images. Haralick is best for classifying
            // textures, so we will be generating examples of wood and clouds:
            var woodenGenerator = new WoodTexture();
            var cloudsGenerator = new CloudsTexture();

            Bitmap[] images = new[]
            {
                woodenGenerator.Generate(512, 512).ToBitmap(),
                woodenGenerator.Generate(512, 512).ToBitmap(),
                woodenGenerator.Generate(512, 512).ToBitmap(),

                cloudsGenerator.Generate(512, 512).ToBitmap(),
                cloudsGenerator.Generate(512, 512).ToBitmap(),
                cloudsGenerator.Generate(512, 512).ToBitmap()
            };

            // Compute the model
            bow.Learn(images);

            bow.ParallelOptions.MaxDegreeOfParallelism = 1;

            // After this point, we will be able to translate
            // images into double[] feature vectors using
            double[][] features = bow.Transform(images);
            #endregion

            Assert.AreEqual(features.GetLength(), new[] { 6, 3 });

            string str = features.ToCSharp();

            double[][] expected = new double[][]
            {
                new double[] { 3, 0, 1 },
                new double[] { 3, 0, 1 },
                new double[] { 3, 0, 1 },
                new double[] { 3, 1, 0 },
                new double[] { 3, 1, 0 },
                new double[] { 3, 1, 0 }
            };

            for (int i = 0; i < expected.Length; i++)
            {
                for (int j = 0; j < expected[i].Length; j++)
                {
                    Assert.IsTrue(expected[i][j] == features[i][j]);
                }
            }

            #region doc_classification_feature_haralick

            // Now, the features can be used to train any classification
            // algorithm as if they were the images themselves. For example,
            // let's assume the first three images belong to a class and
            // the second three to another class. We can train an SVM using

            int[] labels = { -1, -1, -1, +1, +1, +1 };

            // Create the SMO algorithm to learn a Linear kernel SVM
            var teacher = new SequentialMinimalOptimization <Linear>()
            {
                Complexity = 100 // make a hard margin SVM
            };

            // Obtain a learned machine
            var svm = teacher.Learn(features, labels);

            // Use the machine to classify the features
            bool[] output = svm.Decide(features);

            // Compute the error between the expected and predicted labels
            double error = new ZeroOneLoss(labels).Loss(output); // should be 0
            #endregion

            Assert.AreEqual(error, 0);
        }