# Feature Extractors Part 2 - SIFT and HOG

In the first part, we have looked at the Sobel filter which extracts approximations of pixel intensity gradients in images and the Harris filter to detect corners. Today, we will use two feature extractor which have been used successfully for object recognition: the Scale Invariant Feature Transform (SIFT) and Histogram of Oriented Gradients (HOG) in OpenCV. Both of them start with basic edge detection, similar to what we’ve seen in part 1. The code is available here. To learn more about how they work see this section.

## SIFT

Scale Invariant Feature Transform is a feature extractor and object recognition pipeline which is built around using keypoints as features to detect objects regardless of scale.

To start, we load the image and convert it into greyscale.

Next we create a SIFT object.

Here, we select the parameter values as in the paper. The first parameter, nfeatures determines how many of the best features to keep. A value of $0$ keeps all keypoints.

The second parameter specifies the structure of the pyramid which is used. The pyramid consists of multiple downsamples (scales or octaves) and multiple Gaussian blurrings between each downsample. We can specify the latter here. In the paper, nOctaveLayers is $3$. In OpenCV, we cannot select the number of octaves to use. They are calculated automatically from the image resolution.

The third parameter is used to filter out features in regions of low contrast. In these regions it is harder to distinguish an object from background. The greater this threshold, the fewer features will remain. In OpenCV this threshold is divided by nOctaveLayers when filtering is applied. Hence, we use $0.09$ to work with the value from the paper, which is $0.03$.

The next parameter, edgeThreshold is used to filter out edge-like features. These are features which are used to determine the boundaries between objects which is different from the contrastThreshold. The larger this threshold, the more features are retained. To understand the difference to the contrastThreshold, consider the image produced with the Sobel filter in part 1. Here, you can distinguish the plane from the tarmac by using the object boundaries only, not the contrast (since both plane and tarmac are black here by design). So both the edgeThreshold as well as the contrastThreshold could be considered as high.

The final parameter, sigma, is the $\sigma$ used in the Gaussian blurring step which is applied at the first downsample in each octave. We use the default value $1.6$.

Now that we have our sift object we can use it to identify and visualize the keypoints in the image.

In figure 1, you can see the resulting keypoints as circles with the radius marked. The size of the circles indicate the magnitude while the line indicates the orientation of the keypoint. Interestingly, there’s an obviously not helpful keypoint of large magnitude above the plane.

Here is the full code:

## HOG

Histogram of Oriented Gradients is a feature extraction pipeline which was first used to recognize pedestrians.

In OpenCV, the HOGDescriptor() function can be used to compute HOG features. However, the downside of the OpenCV implementation is that there is no simple way to visualize the features. That’s why we use scikit-image instead, which is a collection of image processing algorithms for Python specifically.

First, we load the image and normalize the pixel values so that they fall into the range $[0,1]$ as this is required for the skimage operations later. For this example, we use an image of size $64x128$ as in the original implementation for pedestrian recognition.

Next, we calculate the pixel intensity gradients for each pixel to find the magnitude and orientation for each gradient. We use the Sobel filter for this.

In figure 2, you can see the gradient magnitudes overlaid onto the image.

Once we have the gradients for each pixel we calculate the histograms for the grid cells on the image.

The first part here changes the colour channel sequence from BGR as used in OpenCV to RGB as used in skimage and matplotlib. The third line calculates the HOG features, which are accessible as the first returned value fd. By selecting the visualization option in the second argument, the function also returns the image hogimage. For the visualization of the steps involved in HOG, we overlay the grid and get the result (figure 3).

And that is it. We now have the HOG features which can be used as input for a classifier for example.

Here is the full code:

Tags:

Updated: