Histogram of Oriented Gradients

Reference: Dalal, Navneet, and Bill Triggs. "Histograms of oriented gradients for human detection." Computer Vision and Pattern Recognition, 2005. CVPR 2005. IEEE Computer Society Conference on. Vol. 1. IEEE, 2005.

Histogram of Oriented Gradients is a feature descriptor mainly used for object detection. It's similar to scale-invariant features descriptors, such as SIFT or SURF. This technique counts gradient orientation ocurrences in uniformly spaced cells, using overlapping local contrast to improve accuracy. The original technique divides the image in cells and calculates the histogram of oriented gradients in each cell. The feature extractor implemented in TRIOSlib only calculates the histogram of oriented gradients in the region of interest and uses this histogram as a feature.

HOG feature extractor takes two additional and optional arguments, channels and normalize. The channels argument controls how many orientation bins the histogram will have, dividing the range of orientation in uniform intervals. The value of each orientation bin can be normalized, and this is controled by the normalize argument.

# file docs/examples/methods/hog.py
from trios.classifiers import SKClassifier
from sklearn.tree import DecisionTreeClassifier
from trios.contrib.features.hog import HoGExtractor
import trios
import numpy as np

import trios.shortcuts.persistence as p

drive_location = 'datasets/drive'
training = trios.Imageset([
    ('%s/training/images/%2d_training.tif'%(drive_location, i),
    '%s/training/1st_manual/%2d_manual1.gif'%(drive_location, i),
    '%s/training/mask/%2d_training_mask.gif'%(drive_location, i))
    for i in range(21, 41)])

testset = trios.Imageset([
    ('%s/test/images/%02d_test.tif'%(drive_location, i),
    '%s/test/1st_manual/%02d_manual1.gif'%(drive_location, i),
    '%s/test/mask/%02d_test_mask.gif'%(drive_location, i))
    for i in range(1, 21)])

if __name__ == '__main__':
    win = np.ones((9,9), np.uint8)
    hog = HoGExtractor(window=win, channels=10, normalize=True)

    op = trios.WOperator(win, SKClassifier(DecisionTreeClassifier()), hog)
    print('Training')
    op.train(training)
    print('Evaluating')
    print('Accuracy:', 1 - op.eval(testset))