Home
Course Guidelines
About the course Prerequite Material References
Python
Jupyter Notebooks Python overview
Exercises
Before the semester start: Installation and exercise setup Week 1: Introduction to Python and libraries Week 2: Vector representations Week 3: Linear Algebra Week 4: Linear Transformations Week 5: Models and least squares Week 6: Assignment 1 - Gaze Estimation Week 7: Model selection and descriptive statistics Week 8: Filtering Week 9: Classification Week 10: Evaluation Week 11: Dimensionality reduction Week 12: Clustering and refresh on gradients Week 13: Neural Networks Week 14: Convolutional Neural Networks (CNN's)
Tutorials
Week 1: Data analysis, manipulation and plotting Week 2: Linear algebra Week 3: Transformations tutorial Week 4: Projection and Least Squares tutorial Week 7: Cross-validation and descriptive statistics tutorial Week 8: Filtering tutorial Week 11: Gradient Descent / Ascent
In-class Exercises
In-class 1 In-class 2 In-class 10 In-class 3 In-class 4 In-class 8
Explorer

Document

  • Overview
  • 1. Classification and decision boundaries
  • 2. Introduction to Logistic Regression
  • 3. Histogram of Oriented Gradients (HOG)

Content

  • Overview of Histogram of Oriented G…
    • Task 1 Calculate the HOG descriptor
    • Task 2 Visualizing and understanding the HOG descriptor
    • Task 3 Comparing Hog Features
    • Task 4 Interpretation/ Understanding questions

Gradient image features: Histogram of Oriented Gradients (HoG) introduction

This exercise is about the extraction of gradient features from images. It offers an introduction and application of HoG features on a sample image.

The HOG features will be used in the exercises next week for classification.

List of individual tasks
  • Task 1: Calculate the HOG descriptor
  • Task 2: Visualizing and understanding the HOG de…
  • Task 3: Comparing Hog Features
  • Task 4: Interpretation/ Understanding questions

The cell below loads a sample image.

## load input image import matplotlib.pyplot as plt import numpy as np from skimage.feature import hog from skimage import data, exposure fn = "./data/people01.jpg" image = plt.imread(fn) print(image.shape) fig, ax1 = plt.subplots(figsize=(12, 8), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image')
## load input image
import matplotlib.pyplot as plt
import numpy as np
from skimage.feature import hog
from skimage import data, exposure


fn =  "./data/people01.jpg"
image = plt.imread(fn)
print(image.shape)

fig, ax1 = plt.subplots(figsize=(12, 8), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')
(480, 852, 3)

In the following section:

  • you are introduced to the HOG descriptor
  • shown how to calculate a HoG featues for an image including a visualization of the HoG descriptor.

Overview of Histogram of Oriented Gradients (HOG) Algorithm

The Histogram of Oriented Gradients (HOG) is a feature descriptor used in computer vision to detect objects.

The function skimage.feature.hog computes the HOG features. It also returns the feature descriptor vector (fd ), in which its size is equal to the number of: Bins $\times$ Block Columns $\times$ Block Rows $\times$ Cells in the Block.

The process involves the following stages:

1. Preprocessing / Smoothing:
Initially, the image undergoes a global intensity normalization — a procedure that diminishes the effects of lighting. A common technique involves gamma (power-law) compression, which can be achieved by calculating the square root or logarithm of each color channel. This stage helps in reducing the impact of local shadowing and highlights variations since the image texture strength is generally related to the local surface illumination.

2. Calculation of Image Gradients:
This stage focuses on computing image gradients. The standard procedure is to either convert the image to grayscale or use the most prominent color channel.

Info

This step was implemented by us in the first part of the exercise.

3. Creation of Cells and Calculation of normalized Histograms:
The objective here is to create an encoding of local gradients. This involves partitioning the image window into smaller spatial cells. Each cell creates a histogram of image gradient orientations and performs various steps to normalize intensities and histograms.

4. Compilation of Feature Vector:
The HOG feature is constructed by concatenating the histograms for each cell into one final HOG feature descriptor (vector).

Task 1: Calculate the HOG descriptor
  1. Run the cell below to calculate and plot the HOG descriptor.
# load new image image1 = data.astronaut()[:270,100:320] print(image1.shape) feature_vector=True, bins = 8 pixels_per_cell = 16 cells_per_block = 4 (fd1, hog_feat1) = hog( image1, orientations=bins, pixels_per_cell=(pixels_per_cell, pixels_per_cell), cells_per_block=(cells_per_block, cells_per_block), block_norm="L2", visualize=True, feature_vector=True, channel_axis=-1 ) hog_feat1 = hog_feat1.astype("uint8") # after implementation of the HoG function, run the following plot fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image1, cmap=plt.cm.gray) ax1.set_title('Input image') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_feat1, in_range=(0, 10)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') plt.show()
# load new image
image1 = data.astronaut()[:270,100:320]
print(image1.shape)

feature_vector=True,
bins = 8 
pixels_per_cell = 16
cells_per_block = 4 

(fd1, hog_feat1) = hog(
    image1,
    orientations=bins,
    pixels_per_cell=(pixels_per_cell, pixels_per_cell),
    cells_per_block=(cells_per_block, cells_per_block),
    block_norm="L2",
    visualize=True,
    feature_vector=True,
    channel_axis=-1
)
hog_feat1 = hog_feat1.astype("uint8")


# after implementation of the HoG function, run the following plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image1, cmap=plt.cm.gray)
ax1.set_title('Input image')

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_feat1, in_range=(0, 10))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
plt.show()
(270, 220, 3)
Task 2: Visualizing and understanding the HOG descriptor

Change the parameters of the HOG descriptor and visualize how the feature descriptor changes.

  1. Change the parameters for the HOG descriptor:
  • the number of bins
  • the number of pixels per cell
  • the number of cells per block

and visually observe how the dimensionality and the features change.

  1. The following cell loads an image of a cat. Use the HOG descriptor on this image.
image2 = data.cat()[:270,:440:2] print(image2.shape) fd2,hog_image2 = image # replace this with hog feature results. fd is the feature vector and hog_image is the visualization of the calculated gradients. # after implementation of the HoG function, run the following plot fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image2, cmap=plt.cm.gray) ax1.set_title('Input image') # Rescale histogram for better display hog_image_rescaled2 = exposure.rescale_intensity(hog_feat2, in_range=(0, 10)) ax2.axis('off') ax2.imshow(hog_image_rescaled2, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients')
image2 = data.cat()[:270,:440:2]
print(image2.shape)

fd2,hog_image2 = image # replace this with hog feature results. fd is the feature vector and hog_image is the visualization of the calculated gradients.

# after implementation of the HoG function, run the following plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image2, cmap=plt.cm.gray)
ax1.set_title('Input image')

# Rescale histogram for better display
hog_image_rescaled2 = exposure.rescale_intensity(hog_feat2, in_range=(0, 10))

ax2.axis('off')
ax2.imshow(hog_image_rescaled2, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')
(270, 220, 3)
Task 3: Comparing Hog Features

This task will compare two HoG features of different images.

  1. For different settings visually compare the hog features
  2. Calculate the normalized (use np.linalg.norm to normalize vectors) scalar product between the two feature vectors fd1 and fd2 .
    • What is the scalar product between two feature vectors? (compare it to the value of 1 )
    • Repeat the calculation for multiple HoG filter settings, notice a difference?
Hint

Set feature_vector =True in hog

  1. Based on the cosine similarity (normalized inner product), explain how HoG features can be used to classify different images of cat/humans.
calculate the similarity here..
calculate the similarity here..
Feacture vector similarity: 0.430
Task 4: Interpretation/ Understanding questions

Select several images from the dataset (peopleXY.jpg ) and visually compare the HOG features of people and non-people images.

  1. How could HoG features be used to classify different objects in a larger image with multiple people?

  2. Explain how the HOG features use local information to create an understanding of the content of the full image?

The code in the cell below visualizes the HoG features:

# load new image fn = "./data/people01.jpg" image = plt.imread(fn) print(image.shape) feature_vector=True, bins = 8 pixels_per_cell = 16 cells_per_block = 4 # Compute HOG. (fd, hog_feat) = hog( image, orientations=bins, pixels_per_cell=(pixels_per_cell, pixels_per_cell), cells_per_block=(cells_per_block, cells_per_block), block_norm="L2", visualize=True, feature_vector=False, channel_axis=-1 ) hog_feat = hog_feat.astype("uint8") print(f' HoG feature vector shape: {fd.shape}') print(f' HoG feature vector shape after vectorization: {fd.reshape(-1).shape}') fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True) ax1.axis('off') ax1.imshow(image, cmap=plt.cm.gray) ax1.set_title('Input image') # Rescale histogram for better display hog_image_rescaled = exposure.rescale_intensity(hog_feat, in_range=(0, 10)) ax2.axis('off') ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray) ax2.set_title('Histogram of Oriented Gradients') plt.show()
# load new image
fn =  "./data/people01.jpg"
image = plt.imread(fn)
print(image.shape)

feature_vector=True,
bins = 8 
pixels_per_cell = 16
cells_per_block = 4 

# Compute HOG.
(fd, hog_feat) = hog(
    image,
    orientations=bins,
    pixels_per_cell=(pixels_per_cell, pixels_per_cell),
    cells_per_block=(cells_per_block, cells_per_block),
    block_norm="L2",
    visualize=True,
    feature_vector=False,
    channel_axis=-1
)
hog_feat = hog_feat.astype("uint8")


print(f' HoG feature vector shape: {fd.shape}')
print(f' HoG feature vector shape after vectorization: {fd.reshape(-1).shape}')

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4), sharex=True, sharey=True)

ax1.axis('off')
ax1.imshow(image, cmap=plt.cm.gray)
ax1.set_title('Input image')

# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_feat, in_range=(0, 10))

ax2.axis('off')
ax2.imshow(hog_image_rescaled, cmap=plt.cm.gray)
ax2.set_title('Histogram of Oriented Gradients')

plt.show()
(480, 852, 3)
 HoG feature vector shape: (27, 50, 4, 4, 8)
 HoG feature vector shape after vectorization: (172800,)
# write your reflections here
# write your reflections here