{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Histogram of Oriented Gradients\n\nThe Histogram of Oriented Gradient (HOG) feature descriptor is popular\nfor object detection [1]_.\n\nIn the following example, we compute the [HOG descriptor](https://en.wikipedia.org/wiki/Histogram_of_oriented_gradients)_\nand display a visualisation.\n\n## Algorithm overview\n\nCompute a Histogram of Oriented Gradients (HOG) by\n\n1. (optional) global image normalisation\n2. computing the gradient image in x and y\n3. computing gradient histograms\n4. normalising across blocks\n5. flattening into a feature vector\n\nThe first stage applies an optional global image normalisation\nequalisation that is designed to reduce the influence of illumination\neffects. In practice we use gamma (power law) compression, either\ncomputing the square root or the log of each color channel.\nImage texture strength is typically proportional to the local surface\nillumination so this compression helps to reduce the effects of local\nshadowing and illumination variations.\n\nThe second stage computes first order image gradients. These capture\ncontour, silhouette and some texture information, while providing\nfurther resistance to illumination variations. The locally dominant\ncolor channel is used, which provides color invariance to a large\nextent. Variant methods may also include second order image derivatives,\nwhich act as primitive bar detectors - a useful feature for capturing,\ne.g. bar like structures in bicycles and limbs in humans.\n\nThe third stage aims to produce an encoding that is sensitive to\nlocal image content while remaining resistant to small changes in\npose or appearance. The adopted method pools gradient orientation\ninformation locally in the same way as the SIFT [2]_\nfeature. The image window is divided into small spatial regions,\ncalled \"cells\". For each cell we accumulate a local 1-D histogram\nof gradient or edge orientations over all the pixels in the\ncell. This combined cell-level 1-D histogram forms the basic\n\"orientation histogram\" representation. Each orientation histogram\ndivides the gradient angle range into a fixed number of\npredetermined bins. The gradient magnitudes of the pixels in the\ncell are used to vote into the orientation histogram.\n\nThe fourth stage computes normalisation, which takes local groups of\ncells and contrast normalizes their overall responses before passing\nto next stage. Normalisation introduces better invariance to illumination,\nshadowing, and edge contrast. It is performed by accumulating a measure\nof local histogram \"energy\" over local groups of cells that we call\n\"blocks\". The result is used to normalize each cell in the block.\nTypically each individual cell is shared between several blocks, but\nits normalisations are block dependent and thus different. The cell\nthus appears several times in the final output vector with different\nnormalisations. This may seem redundant but it improves the performance.\nWe refer to the normalized block descriptors as Histogram of Oriented\nGradient (HOG) descriptors.\n\nThe final step collects the HOG descriptors from all blocks of a dense\noverlapping grid of blocks covering the detection window into a combined\nfeature vector for use in the window classifier.\n\n## References\n\n.. [1] Dalal, N. and Triggs, B., \"Histograms of Oriented Gradients for\n       Human Detection,\" IEEE Computer Society Conference on Computer\n       Vision and Pattern Recognition, 2005, San Diego, CA, USA.\n\n.. [2] David G. Lowe, \"Distinctive image features from scale-invariant\n       keypoints,\" International Journal of Computer Vision, 60, 2 (2004),\n       pp. 91-110.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n\nfrom skimage.feature import hog\nfrom skimage import data, exposure\n\n\nimage = data.astronaut()\n\nfd, hog_image = hog(\n    image,\n    orientations=8,\n    pixels_per_cell=(16, 16),\n    cells_per_block=(1, 1),\n    visualize=True,\n    channel_axis=-1,\n)\n\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)\n\nax1.axis('off')\nax1.imshow(image, cmap=plt.cm.gray)\nax1.set_title('Input image')\n\n# Rescale histogram for better display\nhog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))\n\nax2.axis('off')\nax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)\nax2.set_title('Histogram of Oriented Gradients')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}