HOG(Histograms of Oriented Gradients)
What is HOG?
HOG extracts the edges of the image by color variation and consequently identifies the object based on the format.
There are two important concepts
- Derivative
It allows us to measure the rate of changes (colors) and there are three options.- zero derivative
means there is no variation in the image. - small derivative
means there is small variation in the image. - high derivative
means there is high variation in the image
EX) edge of the image.
The zero derivative of the image is painted black instead of the original color, as shown in the images below.
So we can extract the edges of the image and consequently identify the object based on the formats.
- zero derivative
Gradient vector
Gradient vector indicates the direction in which the values increase.
For example,as the image below shows, the gradient vector points upwards because this is where there is the greatest variation in colors from yellow to gray
‘gradient vector image’We select only that part of the head, and can see several arrows pointing to where there is greater gradient vector. Also ‘Gradient Direction’(direction of arrow) matrix and ‘Gradient Magnitude’(size of arrow) can be made like image below.
Role of Gradient Direction & Gradient Magnitude
Gradient Direction & Gradient Magnitude are used to construct the Histogram of Gradients as you can see in the image below.
- The range of the interval is [0,20,40,… ,100], and the total number of bins is 8
- If Direction is 80 and Magnitude is 2, Fill 2 in the interval corresponding to 80 of the range of bin intervals
- If Direction is 10 and Magnitude is 4, since 10 corresponds to the range between 0 and 20 of the range of bin intervals, take 2(= $10\over 20$ = $(direction)\over(20-0)$) for each Magnitude.
After linking all values,generate the final histogram, as shown in the image below to find out if an image is a particular object that I want to detect.
Result
A HOG like the image below is created by the final histogram.
Example Code
There are two ways to implement Hog: using only dlib and using CNN.
Only dlib
1 2 3 4 5 6 7 8 9 10 11 12 13
import dlib import cv2 image = cv2.imread('people2.jpg') face_detector_hog = dlib.get_frontal_face_detector() # 1 is similar to the scale factor which is the higher value, the smaller the box detections = face_detector_hog(image, 1) for face in detections: print(face) l, t, r, b = face.left(), face.top(), face.right(), face.bottom() cv2.rectangle(image, (l, t), (r, b), (0, 255, 0), 2) cv2.imshow(image)
CNN with dlib
1 2 3 4 5 6 7 8 9 10
image = cv2.imread('people2.jpg') cnn_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat') detections = cnn_detector(image, 1) for face in detections: # the higher c value, the higher quality of detection l, t, r, b, c = face.rect.left(), face.rect.top(), face.rect.right(), face.rect.bottom(), face.confidence cv2.rectangle(image, (l, t), (r, b), (0, 255, 0), 2) cv2.imshow(image)