Search code examples
c#image-processingaccord.net

Image Texture Recognition C#


I have a project that deals with disease recognition. We are required to use c#. How should I determine if the tree is infected or not. I am using Accord. Imaging my correlation value are too high. Here is a sample image right side: infected; left side: healthy/not infected


Solution

  • Since you have mentioned you are using Accord.NET, I believe you might want to take a look at the examples for Bag-of-Visual-Words. More specifically, since you mentioned texture recognition, maybe you should consider the BoVW example that uses the Haralick feature descriptor for extracting texture features from images. This example is reproduced below:

    // 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);
    

    In order to apply this example to your problem, instead of creating the images variable using the wood and cloud texture generators, you can retrieve them from your own database of images. Later, after you have extracted a feature representation for each of the images in your dataset, you can use those representations to learn any machine learning classifier, such as a Support Vector Machine, using code similar to:

    // 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
    

    P.S.: It might be possible to obtain better performance in your classification problem if you consider creating SVMs with a ChiSquare kernel instead of Linear.