Get this book -> Problems on Array: For Interviews and Competitive Programming
Reading time: 35 minutes | Coding time: 10 minutes
What is Histogram of Oreinted Gradients (HOG)?
Navneet Dalal and Bill Triggs introduced Histogram of Oriented Gradients(HOG) features in 2005. Histogram of Oriented Gradients (HOG) is a feature descriptor used in image processing, mainly for object detection. A feature descriptor is a representation of an image or an image patch that simplifies the image by extracting useful information from it.
The principle behind the histogram of oriented gradients descriptor is that local object appearance and shape within an image can be described by the distribution of intensity gradients or edge directions. The x and y derivatives of an image (Gradients) are useful because the magnitude of gradients is large around edges and corners due to abrupt change in intensity and we know that edges and corners pack in a lot more information about object shape than flat regions. So, the histograms of directions of gradients are used as features in this descriptor.
Workflow of object detection using HOG
Now that we know basic priciple of Histogram of Oriented Gradients we will be moving into how we calculate the histograms and how these feature vectors, that are obtained from the HOG descriptor, are used by the classifier such a SVM to detect the concerned object.
Steps for Object Detection with HOG
How Histogram of Oreinted Gradients(HOG) Works?
Pre-processing
Preprocessing of image involves normalising the image but it is entirely optional. It is used to improve performance of the HOG descriptor. Since, here we are building a simple descriptor we don't use any normalisation in preprocessing.
Computing Gradient
The first actual step in the HOG descriptor is to compute the image gradient in both the x and y direction.
Let us take an example. Say the pixel Q has values surrounding it as shown below:
We can calculate the Gradient magnitude for Q in x and y direction as follow:
We can get the magnitude of the gradient as:
And the direcction of the gradient as :
Compute Histogram of Gradients in 8×8 cells
- The image is divided into 8×8 cell blocks and a histogram of gradients is calculated for each 8×8 cell block.
- The histogram is essentially a vector of 9 buckets ( numbers ) corresponding to angles from 0 to 180 degree (20 degree increments).
- The values of these 64 cells (8X8) are binned and cumulatively added into these 9 buckets.
- This essentially reduces 64 values into 9 values.
A great illustration of this is shown on learnopencv. The following figure shows how it is done. The blue pixel encircled has an angle of 80 degrees and magnitude of 2. So it adds 2 to the 5th bin. The gradient at the pixel encircled using red has an angle of 10 degrees and magnitude of 4. Since 10 degrees is half way between 0 and 20, the vote by the pixel splits evenly into the two bins.
Illustration of splitting of gradient magitude according to gradient direction (Image source: https://www.learnopencv.com/histogram-of-oriented-gradients)
Block Normalization
After the creation of histogram of oriented gradients we need to something else too. Gradient is sensitive to overall lighting. If we say divide/multiply pixel values by some constant in order to make it lighter/ darker the gradient magnitude will change and so will histogram values. We want that histogram values be independent of lighting. Normalization is done on the histogram vector v within a block. One of the following norms could be used:
- L1 norm
- L2 norm
- L2-Hys(Lowe-style clipped L2 norm)
Now, we could simply normalize the 9×1 histogram vector but it is better to normalize a bigger sized block of 16×16. A 16×16 block has 4 histograms (8×8 cell results to one histogram) which can be concatenated to form a 36 x 1 element vector and normalized. The 16×16 window then moves by 8 pixels and a normalized 36×1 vector is calculated over this window and the process is repeated for the image.
Calculate HOG Descriptor vector
To calculate the final feature vector for the entire image patch, the 36×1 vectors are concatenated into one giant vector.
So, say if there was an input picture of size 64×64 then the 16×16 block has 7 positions horizontally and 7 position vertically.
In one 16×16 block we have 4 histograms which after normalization concatinate to form a 36×1 vector.
(Video) C34 | HOG Feature Vector Calculation | Computer Vision | Object Detection | EvODNThis block moves 7 positions horizontally and vertically totalling it to 7×7 = 49 positions.
So when we concatenate them all into one gaint vector we obtain a 36×49 = 1764 dimensional vector.
This vector is now used to train classifiers such as SVM and then do object detection.
Visualization of HOG features
Here is a snippet to visualise HOG features of an Image provided in Scikit-Image's docs to visualize HOG features.
import matplotlib.pyplot as pltfrom skimage.feature import hogfrom skimage import data, exposureimage = data.astronaut()fd, hog_image = hog(image, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualize=True, multichannel=True)fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True)ax1.axis('off')ax1.imshow(image, cmap=plt.cm.gray)ax1.set_title('Input image')# Rescale histogram for better displayhog_image_rescaled = exposure.rescale_intensity(hog_image, 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()
Output as follow:
Visualization of HOG features of image of the astronaut Eileen Collins.
Implementation
In this tutorial we will be performing a simple Face Detection using HOG features.
We need to first train the classifier in order to do face detection so first we will need to have training set for the classifier.
Training Set
- Positive training samples
Labeled Faces in the Wild dataset provided by Scikit-Learn consists of variety of faces which is perfect for our positive set.
from sklearn.datasets import fetch_lfw_peoplefaces = fetch_lfw_people()positive_patches = faces.images
- Negative training samples
For Negative set we need images without face on them. Scikit-Image offers images which can be used in this case. To incerease the size of negative set we extract patches of image at different scale using PatchExtractor from Scikit-Learn.
from skimage import data, transformfrom sklearn.feature_extraction.image import PatchExtractorimgs_to_use = ['camera', 'text', 'coins', 'moon', 'page', 'clock', 'immunohistochemistry', 'chelsea', 'coffee', 'hubble_deep_field']images = [color.rgb2gray(getattr(data, name)()) for name in imgs_to_use] def extract_patches(img, N, scale=1.0, patch_size=positive_patches[0].shape): extracted_patch_size = tuple((scale * np.array(patch_size)).astype(int)) extractor = PatchExtractor(patch_size=extracted_patch_size, max_patches=N, random_state=0) patches = extractor.transform(img[np.newaxis]) if scale != 1: patches = np.array([transform.resize(patch, patch_size) for patch in patches]) return patchesnegative_patches = np.vstack([extract_patches(im, 1000, scale) for im in images for scale in [0.5, 1.0, 2.0]])
Extract HOG Features
Scikit-Image's feature module offers a function skimage.feature.hog which extracts Histogram of Oriented Gradients (HOG) features for a given image. we combine the positive and negative set and compute the HOG features
from skimage import feature from itertools import chainX_train = np.array([feature.hog(im) for im in chain(positive_patches, negative_patches)])y_train = np.zeros(X_train.shape[0])y_train[:positive_patches.shape[0]] = 1
Training a SVM classifier
We will use Scikit-Learn's LinearSVC with a grid search over a few choices of the C parameter:
from sklearn.svm import LinearSVCfrom sklearn.model_selection import GridSearchCV grid = GridSearchCV(LinearSVC(dual=False), {'C': [1.0, 2.0, 4.0, 8.0]},cv=3)grid.fit(X_train, y_train)grid.best_score_
We will take the best estimator and then build a model.
model = grid.best_estimator_model.fit(X_train, y_train)
Testing on a new image
Now that we have built the Model we can test it on a new image to see how it detects the faces.
from skimage import ioimg = io.imread('testpic.jpg',as_gray=True)img = skimage.transform.rescale(img, 0.5)indices, patches = zip(*sliding_window(img))patches_hog = np.array([feature.hog(patch) for patch in patches])labels = model.predict(patches_hog)
We are detecting the face by using a sliding window which goes over the image patches. Then we find the HOG feature of these patches. Finally, we run it through the classification model that we build and predict the face in the image. The image below is one of the test images. We can see that the classifier detected patches and most of them overlap the face in the image.
To see the full code for this post check out this repository
FAQs
What detection is Histogram of Oriented Gradients suitable for? ›
Histogram of Oriented Gradients, also known as HOG, is a feature descriptor like the Canny Edge Detector, SIFT (Scale Invariant and Feature Transform) . It is used in computer vision and image processing for the purpose of object detection.
How do you use a HOG for object detection? ›In this tutorial, we will use Histogram of Oriented Gradient (HOG) feature descriptor based linear SVM to create a person detector. We will first create a person classifier and then use this classifier with a sliding window to identify and localize people in an image.
What is the disadvantage of Histogram of Oriented Gradients? ›The disadvantage is that the final descriptor vector grows larger, thus taking more time to extract and to train using a given classifier.
Is Histogram of Oriented Gradients an algorithm? ›Histogram of Oriented Gradients(HOG), one of the well-known image processing algorithms, is a feature descriptor that is used for extracting essential features and shapes of a particular object within an image such as edges and textures.
What is HOG feature object detection? ›The histogram of oriented gradients (HOG) is a feature descriptor used in computer vision and image processing for the purpose of object detection. The technique counts occurrences of gradient orientation in localized portions of an image.
Why do we use HOG feature? ›The HOG features are widely use for object detection. HOG decomposes an image into small squared cells, computes an histogram of oriented gradients in each cell, normalizes the result using a block-wise pattern, and return a descriptor for each cell.
What is the best algorithm for object detection? ›Most Popular Object Detection Algorithms. Popular algorithms used to perform object detection include convolutional neural networks (R-CNN, Region-Based Convolutional Neural Networks), Fast R-CNN, and YOLO (You Only Look Once).
What is the disadvantage of HOG algorithm? ›Especially, HOG shows good performance for human detection [11-13]. However, it has a disadvantage that is very sensitive to image rotation. Therefore, HOG is not good choice for classification of textures or objects which can often be detected as rotated image.
Which framework is best for object detection? ›The YOLO framework (You Only Look Once) on the other hand, deals with object detection in a different way. It takes the entire image in a single instance and predicts the bounding box coordinates and class probabilities for these boxes.
When should you not use a histogram? ›Avoid using a histogram if:
You want to compare the specific values of individual data points.
What are three limitations of a histogram? ›
Disadvantages of Histogram
Here are the cons/drawback of histogram: Not allow you to read exact values because data is grouped into categories. It uses only with continuous data. In Histogram, it is not easy to compare two data sets.
Histograms can sometimes be misleading because of the way they conflate data into larger bins. For example, the graph showing year of composition shows five tragedies written between 459 and 450 BCE.
What is the accuracy of HOG? ›The HOG with a pre-processing framework achieves a higher accuracy rate of 92.5% and the non-processing method accuracy rate of 88.13%, improving the accuracy rate of 4.37%.
What is HOG image classification? ›The Histogram of Oriented Gradients (HOG) is a method used in computer vision and image processing to describe the features of a given piece of data. It considers the number of times the gradient orientation in a localized part of an image occurs. Support vector machine (SVM) is also known as the discriminative method.
What is LBP in image processing? ›Local Binary Pattern (LBP) is an effective texture descriptor for images which thresholds the neighboring pixels based on the value of the current pixel [12]. LBP descriptors efficiently capture the local spatial patterns and the gray scale contrast in an image.
What is HOG method face recognition? ›What is HOG and how it works ? HOG is a feature descriptor used to extract the features pixel by pixel with the help of gradients. This is primarily used for face detection, recognition and object detection. HOG works on grey scale images.
How do you use a HOG with SVM? ›The HOG person detector uses a sliding detection window which is moved around the image. At each position of the detector window, a HOG descriptor is computed for the detection window. This descriptor is then shown to the trained SVM, which classifies it as either “person” or “not a person”.
How do you implement HOG in Python? ›...
HOG
- Horizontal and vertical gradients.
- Gradient magnituge.
- Gradient direction.
- Histogram for a given cell.
HOG is a simple and powerful feature descriptor. It is not only used for face detection but also it is widely used for object detection like cars, pets, and fruits. HOG is robust for object detection because object shape is characterized using the local intensity gradient distribution and edge direction.
What is the application of HOG? ›HOG, or Histogram of Oriented Gradients, is a feature descriptor that is often used to extract features from image data. It is widely used in computer vision tasks for object detection.
Is HOG A CNN? ›
Great success has been achieved recently on general object recognition by means of deep neural networks. Thus we are inspired to inspect the effectiveness of deep neural network on face recognition. This paper presents a deep neural network architecture referred as HOG-CNN for face recognition.
Which is the fastest object detection algorithm 2022? ›- Histogram of Oriented Gradients (HOG) ...
- Fast R-CNN. ...
- Faster R-CNN. ...
- Region-based Convolutional Neural Networks (R-CNN) ...
- Region-based Fully Convolutional Network (R-FCN) ...
- Single Shot Detector (SSD) ...
- YOLO (You Only Look Once) ...
- RetinaNet.
Object detection in videos can also be difficult because of the fast speed required of object detection algorithms to accurately classify and localise important objects in motion to meet real-time video processing. Another significant problem facing object detection is the limited amount of annotated data.
Which algorithm is best for anomaly detection? ›Local outlier factor (LOF)
Local outlier factor is probably the most common technique for anomaly detection. This algorithm is based on the concept of the local density. It compares the local density of an object with that of its neighbouring data points.
- ImageNet.
- COCO (Microsoft Common Objects in Context)
- PASCAL VOC.
- BDD100K (UCBerkeley "Deep Drive")
- Visual Genome.
- nuScenes.
- DOTA v2. ...
- KITTI Vision Benchmark Suite.
C++ is considered to be the fastest programming language, which is highly important for faster execution of heavy AI algorithms. A popular machine learning library TensorFlow is written in low-level C/C++ and is used for real-time image recognition systems. Advantages: A collection of AI libraries and tools.
What are the different methods for object detection? ›- 1| Fast R-CNN.
- 2| Faster R-CNN.
- 3| Histogram of Oriented Gradients (HOG)
- 4| Region-based Convolutional Neural Networks (R-CNN)
- 5| Region-based Fully Convolutional Network (R-FCN)
- 6| Single Shot Detector (SSD)
- 7| Spatial Pyramid Pooling (SPP-net)
- 8| YOLO (You Only Look Once)
The disadvantages of a histogram are that they are extremely focused on the number of "bins," or lines, and are heavily affected by the maximum and minimum of the variable. Altering the max and min can alter the look of a graph dramatically, which can be misleading.
What is the drawback of histogram? ›It doesn't allow to detect relevant values.
In general, when a variable contains some frequent values, we need to be aware of it. However, histograms don't allow to do that, because they are based on intervals, and intervals “hide” individual values.
The histogram is a popular graphing tool. It is used to summarize discrete or continuous data that are measured on an interval scale. It is often used to illustrate the major features of the distribution of the data in a convenient form.
What are the advantages and disadvantages of using histograms? ›
The advantage of a frequency histogram over a frequency distribution is that it allows you to visually compare data. The frequency histogram has the disadvantage of being more complex, requiring more time and effort to produce than the frequency distribution.
What are the advantages and disadvantages histogram? ›The advantage of a frequency histogram is, that it is visually strong. Histograms are usually preferable to stem and leaf diagrams in large data sets. The disadvantage of a stem leaf diagram is not visual. The disadvantage of histogram frequency is cannot read exact values because data is grouped into categories.
What 4 things does a histogram reveal about the process? ›It shows the center, the spread, the skewness of the data, the presence of outliers, and the presence of multiple modes in the data.
What are common histogram mistakes? ›Common mistakes
Try several bin size, it can lead to very different conclusions. Don't use weird color sheme. It does not give any more insight. Don't confound it with a barplot.
The main advantages of a histogram are its simplicity and versatility. It can be used in many different situations to offer an insightful look at frequency distribution. For example, it can be used in sales and marketing to develop the most effective pricing plans and marketing campaigns.
What are alternatives to histogram? ›An alternative to both histograms and boxplots is to use density plots.
How much yield do you get from a hog? ›On average about 57% of a hog make it from the pen to the pan. A 250 lb. hog will yield approximately 144 lbs of retail cuts. Around 28% of a hog's live weight is inedible product re- moved during the slaughter and dressing procedure bringing our 250 lb.
How do you evaluate hogs? ›A heavily muscled hog will have a plump, firm, deep, thick meaty ham and long rump. When viewed from the rear, the lower part of the ham should be the widest part of the hog. The ham should extend well down onto the hock. The hog should be muscular over the shoulders with a wide chest and the loin wide along the back.
How do I make sure a successful hog production? ›- High health status. ...
- Strict biosecurity. ...
- Gilt acclimation. ...
- Vaccination. ...
- All-in, all-out pig flow. ...
- Barns designed for comfort. ...
- Producer engagement. ...
- Veterinary oversight.
Convolutional Neural Networks (CNNs) is the most popular neural network model being used for image classification problem.
Which model should I use for image classification? ›
VGG16 is a pre-trained CNN model which is used for image classification. It is trained on a large and varied dataset and fine-tuned to fit image classification datasets with ease. Now, import a VGG16 model. You must initialize the model and add input and output layers.
How many images do you need for image classification? ›Computer Vision: For image classification using deep learning, a rule of thumb is 1,000 images per class, where this number can go down significantly if one uses pre-trained models [6].
How does LBPH algorithm work? ›Given the above-mentioned parameters, LBPH works as follows; A data set is created by taking images with a camera or taking images that are saved, and then provisioning a unique identifier or name of the person in the image and then adding the images to a database.
How can you determine LBP code of an image? ›For calculating the LBP, the LBP code for each pixel is calculated and the histogram of LBP codes is constructed as the LBP feature. To calculate the lbp code, for each pixel p, the 8 neighbours of the center pixel are compared with the pixel p and the neighbours x are assigned a value 1 if x ≥ p. Fig.
Is LBP a deep learning algorithm? ›Local binary pattern network: A deep learning approach for face recognition.
What does a histogram help detect? ›It can help detect any unusual observations (outliers) or any gaps in the data. A histogram divides up the range of possible values in a data set into classes or groups.
What is a histogram test used for? ›Histograms are particularly helpful to analyze the frequency distribution of sample data. In a statistical experiment, frequency distribution is the number of observations that belong to a particular category (or “bin” in histogram terminology).
Is histogram used for detecting outliers? ›Histogram: A histogram is the best way to check univariate data — data containing a single variable — for outliers. A histogram divides the range of values into various groups (or buckets), and then shows the frequency — how many times the data falls into each group — through a bar graph.
Why is histogram The best way to display data? ›In general, a histogram can be used whenever there's a need to display a comparison of the distribution of certain numerical data in various ranges of intervals. Histogram examples can help an audience see and understand quickly and easily essential meanings and patterns related to a large amount of data.
What are the four uses of a histogram? ›Histograms are not only useful in determining the minimum data point, maximum data point, and the median. But it is also used to find out the standard median of the data. The range of the chart from left to right, that is also called the class width of the chart, can be found out by using a histogram.
Why is hog better for face detection? ›
HOG is a simple and powerful feature descriptor. It is not only used for face detection but also it is widely used for object detection like cars, pets, and fruits. HOG is robust for object detection because object shape is characterized using the local intensity gradient distribution and edge direction.
What are three types of hog production? ›Farrow-to-finish operations raise hogs from birth to slaughter weight, about 240-270 pounds. Feeder pig producers raise pigs from birth to about 10-60 pounds, then generally sell them for finishing. Feeder pig finishers buy feeder pigs and grow them to slaughter weight.
What are 5 by products of hogs? ›Byproducts (edible offal (including variety meats), inedible offal, hides and skins, blood, fats, and tallow) include all parts of a live animal that are not part of the dressed carcass and constitute about 30 percent of the liveweight of hogs and about 44 percent of the live- weight of cattle.
How do you analyze data from a histogram? ›Click Data > Data Analysis > Histogram > OK. Under Input, select the input range (your data), then select the bin range. Under Output options, choose an output location. To show the data in descending order of frequency, click Pareto (sorted histogram).
Which method is most preferred for outlier detection? ›Scatter plots and box plots are the most preferred visualization tools to detect outliers. Scatter plots — Scatter plots can be used to explicitly detect when a dataset or particular feature contains outliers.
What do outliers look like on a histogram? ›A histogram with an overlaid box plot are shown below. The outlier is identified as the largest value in the data set, 1441, and appears as the circle to the right of the box plot. Outliers should be investigated carefully.