public mTextureWood(double RingParameter) { Rings = RingParameter; Pattern = new WoodTexture(Rings); Texture = Pattern; }
public void apply_test() { #region doc_apply // Ensure results can be reproduced: Accord.Math.Random.Generator.Seed = 0; // Let's generate a square image containing a wooden texture: Bitmap wood = new WoodTexture().Generate(512, 512).ToBitmap(); // Show the image or save it to disk so we can visualize it later: // ImageBox.Show(wood); // visualizes it // wood.Save("wood.png"); // saves to disk // Create a new Haralick extractor: Haralick haralick = new Haralick() { Mode = HaralickMode.AverageWithRange, CellSize = 0 // use the entire image }; // Extract the descriptors from the texture image List <double[]> descriptors = haralick.ProcessImage(wood); // If desired, we can obtain the GLCM for the image: GrayLevelCooccurrenceMatrix glcm = haralick.Matrix; // Since we specified CellSize = 0 when we created the Haralick object // above, the Haralick extractor should have been computed considering // the entire image and therefore it will return just one descriptor: double[] descriptor = descriptors[0]; // should be similar to // { // 0.000452798780435224, 0.000176452252541844, 2185.7835571369, 1055.78890910489, // 1819136356.0869, 31272466.1144943, 162.811942113822, 0.0294747289650559, // 0.0600645989906271, 0.014234218481406, 325.617817549069, 0.0288571168872522, // 124520.583178736, 1051.39892825528, 6.04466262360143, 0.0166538281740083, // 9.9387182389777, 0.188948901162149, 4.21006097507467E-05, 1.50197212765552E-05, // 4.51859838204172, 0.250994550822876, -0.127589342042208, 0.0356360581349288, // 0.858214424241827, 0.0572031826003976 // } #endregion string str = descriptor.ToCSharp(); double[] expected = new double[] { 0.000452798780435224, 0.000176452252541844, 2185.7835571369, 1055.78890910489, 1819136356.0869, 31272466.1144943, 162.811942113822, 0.0294747289650559, 0.0600645989906271, 0.014234218481406, 325.617817549069, 0.0288571168872522, 124520.583178736, 1051.39892825528, 6.04466262360143, 0.0166538281740083, 9.9387182389777, 0.188948901162149, 4.21006097507467E-05, 1.50197212765552E-05, 4.51859838204172, 0.250994550822876, -0.127589342042208, 0.0356360581349288, 0.858214424241827, 0.0572031826003976 }; Assert.IsTrue(expected.IsEqual(descriptor, rtol: 1e-6)); Assert.AreEqual(1, descriptors.Count); Assert.AreEqual(new HashSet <CooccurrenceDegree> { CooccurrenceDegree.Degree0, CooccurrenceDegree.Degree45, CooccurrenceDegree.Degree90, CooccurrenceDegree.Degree135 }, haralick.Degrees); }
private static ObjectId AddMaterial(string nameWood) { // Database db = Application.DocumentManager.MdiActiveDocument.Database; TransactionManager tm = db.TransactionManager; Transaction trans = tm.StartTransaction(); Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ObjectId materialId = ObjectId.Null; try { DBDictionary materialDict; ObjectId materialDictId = db.MaterialDictionaryId; materialDict = trans.GetObject(materialDictId, OpenMode.ForWrite) as DBDictionary; Material material = new Material(); material.Name = nameWood; WoodTexture woodTexture = new WoodTexture(); MaterialColor matColor, woodColor1, woodColor2; EntityColor entColor = new EntityColor(128, 0, 128); matColor = new MaterialColor(Method.Override, 1.0, entColor); EntityColor entColor2 = new EntityColor(128, 128, 0); woodColor1 = new MaterialColor(Method.Override, 1.0, entColor2); EntityColor entColor3 = new EntityColor(0, 128, 128); woodColor2 = new MaterialColor(Method.Override, 1.0, entColor3); woodTexture.Color1 = woodColor1; woodTexture.Color2 = woodColor2; MaterialMap materialMap = new MaterialMap(Source.Procedural, woodTexture, 0, null); material.Diffuse = new MaterialDiffuseComponent(matColor, materialMap); MaterialColor inheritColor = new MaterialColor(Method.Inherit, 1.0, entColor); material.Ambient = matColor; material.Specular = new MaterialSpecularComponent(matColor, new MaterialMap(), 0.5); material.Mode = Mode.Realistic; materialDict.SetAt(nameWood, material); materialId = material.ObjectId; trans.Commit(); } catch (System.Exception e) { ed.WriteMessage(e.Message); trans.Abort(); } return(materialId); }
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); }