Haralick Class
Click or drag to resize
Accord.NET (logo)

Haralick Class

Haralick textural feature extractor.
Inheritance Hierarchy
SystemObject
  Accord.ImagingBaseFeatureExtractorFeatureDescriptor
    Accord.ImagingHaralick

Namespace:  Accord.Imaging
Assembly:  Accord.Imaging (in Accord.Imaging.dll) Version: 3.8.0
Syntax
public class Haralick : BaseFeatureExtractor<FeatureDescriptor>
Request Example View Source

The Haralick type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCellSize
Gets the size of a cell, in pixels. A value of 0 means the cell will have the size of the image. Default is 0 (uses the entire image).
Public propertyDegrees
Gets the CooccurrenceDegrees which should be computed by this Haralick textural feature extractor. Default is NormalizedAverage.
Public propertyDescriptors
Gets the set of local binary patterns computed for each cell in the last call to ProcessImage(Bitmap).
Public propertyFeatures
Gets or sets the number of features to extract using the HaralickDescriptor. By default, only the first 13 original Haralick's features will be used.
Public propertyMatrix
Gets the Gray-level Co-occurrence Matrix (GLCM) generated during the last call to Transform(UnmanagedImage).
Public propertyMode
Gets or sets the mode of operation of this Haralick's textural feature extractor.
Public propertyNormalize
Gets or sets whether to normalize final histogram feature vectors. Default is false.
Public propertyNumberOfInputs
Returns -1.
(Inherited from BaseFeatureExtractorTFeature.)
Public propertyNumberOfOutputs
Gets the dimensionality of the features generated by this extractor.
(Inherited from BaseFeatureExtractorTFeature.)
Public propertySupportedFormats
Gets the list of image pixel formats that are supported by this extractor. The extractor will check whether the pixel format of any provided images are in this list to determine whether the image can be processed or not.
(Inherited from BaseFeatureExtractorTFeature.)
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
(Inherited from BaseFeatureExtractorTFeature.)
Protected methodClone(ISetPixelFormat)
Creates a new object that is a copy of the current instance.
(Overrides BaseFeatureExtractorTFeatureClone(ISetPixelFormat).)
Public methodDispose
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
(Inherited from BaseFeatureExtractorTFeature.)
Protected methodDispose(Boolean)
Releases unmanaged and - optionally - managed resources.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodInnerTransform
This method should be implemented by inheriting classes to implement the actual feature extraction, transforming the input image into a list of features.
(Overrides BaseFeatureExtractorTFeatureInnerTransform(UnmanagedImage).)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodProcessImage(Bitmap) Obsolete.
Obsolete. Please use the Transform(Bitmap) method instead.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodProcessImage(BitmapData) Obsolete.
Obsolete. Please use the Transform(Bitmap) method instead.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodProcessImage(UnmanagedImage) Obsolete.
Obsolete. Please use the Transform(Bitmap) method instead.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTransform(Bitmap)
Applies the transformation to an input, producing an associated output.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodTransform(Bitmap)
Applies the transformation to an input, producing an associated output.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodTransform(UnmanagedImage)
Applies the transformation to an input, producing an associated output.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodTransform(UnmanagedImage)
Applies the transformation to an input, producing an associated output.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodTransform(Bitmap, IEnumerableTFeature)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from BaseFeatureExtractorTFeature.)
Public methodTransform(UnmanagedImage, IEnumerableTFeature)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from BaseFeatureExtractorTFeature.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

Haralick's texture features are based on measures derived from Gray-level Co-occurrence matrices (GLCM).

Whether considering the intensity or grayscale values of the image or various dimensions of color, the co-occurrence matrix can measure the texture of the image. Because co-occurrence matrices are typically large and sparse, various metrics of the matrix are often taken to get a more useful set of features. Features generated using this technique are usually called Haralick features, after R. M. Haralick, attributed to his paper Textural features for image classification (1973).

This class can extract HaralickDescriptors from different regions of an image using a pre-defined cell size. For more information about which features are computed, please see documentation for the HaralickDescriptor class.

References:

Examples

The first example shows how to extract Haralick descriptors given an image.

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

Input image:

The second example shows how to use the Haralick feature extractor as part of a Bag-of-Words model in order to perform texture image classification:

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