Click or drag to resize
Accord.NET (logo)

HistogramsOfOrientedGradients Class

Histograms of Oriented Gradients (HOG) descriptor extractor.
Inheritance Hierarchy
SystemObject
  Accord.ImagingBaseFeatureExtractorFeatureDescriptor
    Accord.ImagingHistogramsOfOrientedGradients

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

The HistogramsOfOrientedGradients type exposes the following members.

Constructors
  NameDescription
Public methodHistogramsOfOrientedGradients
Initializes a new instance of the HistogramsOfOrientedGradients class.
Public methodHistogramsOfOrientedGradients(Int32, Int32, Int32)
Initializes a new instance of the HistogramsOfOrientedGradients class.
Top
Properties
  NameDescription
Public propertyBinWidth
Gets the width of the histogram bin. This property is computed as (2.0 * System.Math.PI) / numberOfBins.
Public propertyBlockSize
Gets the size of a block, in pixels. Default is 3.
Public propertyCellSize
Gets the size of a cell, in pixels. Default is 6.
Public propertyDirection
Gets the matrix of orientations generated in the last call to Transform(Bitmap).
Public propertyHistograms
Gets the histogram computed at each cell.
Public propertyMagnitude
Gets the matrix of magnitudes generated in the last call to Transform(Bitmap).
Public propertyNormalize
Gets or sets whether to normalize final histogram feature vectors. Default is true.
Public propertyNumberOfBins
Gets the number of histogram bins. Default is 9.
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

References:

Examples

The first example shows how to extract HOG descriptors from a standard test image:

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

The second example shows how to use HOG descriptors as part of a BagOfVisualWords (BoW) pipeline for 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 Local Binary Pattern (LBP) 
// feature extractor and the Binary-Split clustering algorithm.
// However, this is just an example: the best features and the
// best clustering algorithm might need to be found through 
// experimentation. Please also try with KMeans first to obtain
// a baseline value.

// Create a new Bag-of-Visual-Words (BoW) model using LBP features
var bow = BagOfVisualWords.Create(new LocalBinaryPattern(), new BinarySplit(3));

// Since we are using generics, we can setup properties 
// of the binary split clustering algorithm directly:
bow.Clustering.ComputeCovariances = false;
bow.Clustering.ComputeProportions = false;
bow.Clustering.ComputeError = false;

// Get some training images
Bitmap[] images = GetImages();

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

// 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<Gaussian>()
{
    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