Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lec 4 Hog

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Histogram oriented gradients

• https://learnopencv.com/histogram-of-oriented-gradients
What is a Feature Descriptor?

A feature descriptor is a representation of an image or an image patch that simplifies the image by extracting useful information
and throwing away extraneous information.
Keep in mind that HOG descriptor can be calculated for other sizes, but in lecture I am sticking to numbers presented in the
original paper so you can easily understand the concept with one concrete example.
How to calculate Histogram of Oriented Gradients ?

Step 1 : Preprocessing
Step 2 : Calculate the Gradient Images
Step 3 : Calculate Histogram of Gradients in 8×8 cells
Step 4 : 16×16 Block Normalization
Step 5 : Calculate the Histogram of Oriented Gradients feature vector
Step 1 : Preprocessing

•HOG feature descriptor used for pedestrian detection is calculated on a 64×128 patch of an image.
•the patches need to have an aspect ratio of 1:2. For example, they can be 100×200, 128×256, or 1000×2000 but not
101×205.
Step 2 : Calculate the Gradient Images
Step 2 : Calculate the Gradient Images
Step 2 : Calculate the Gradient Images
Step 2 : Calculate the Gradient Images
Step 2 : Calculate the Gradient Images

•X direction = | 40 - 70 | = 30
•Y direction = | 20 - 70 | = 50
• 2 2 2 2
M = sqrt(Gx + Gy )=sqrt( (30) + (50) )~= 58
• -1
Sita = tan ’=(Gy/Gx)=50/30 ~ =30
Step 2 : Calculate the Gradient Images

• Magnitude Sita
Step 2 : Calculate the Gradient Images
Step 3 : Calculate Histogram of Gradients in 8×8 cells

• In this step, the image is divided into 8×8 cells and a histogram of gradients is calculated for each 8×8 cells.
• An 8×8 image patch contains 8x8x3 = 192 pixel values. The gradient of this patch contains 2 values ( magnitude and
direction ) per pixel which adds up to 8x8x2 = 128 numbers.

• Calculating a histogram over a patch makes this representation more robust to noise. Individual gradients may have
noise, but a histogram over 8×8 patch makes the representation much less sensitive to noise.
Step 3 : Calculate Histogram of Gradients in 8×8 cells
Step 3 : Calculate Histogram of Gradients in 8×8 cells
Step 3 : Calculate Histogram of Gradients in 8×8
cells
Step 4 : 16×16 Block Normalization

Gradients of an image are sensitive to overall lighting.


If you make the image darker by dividing all pixel values by 2, the gradient magnitude will change by half, and
therefore the histogram values will change by half

Let’s say we have an RGB color vector [ 128, 64, 32 ]. The length of this vector is sqrt{128^2 + 64^2 + 32^2} =
146.64.
This is also called the L2 norm of the vector.
Dividing each element of this vector by 146.64 gives us a normalized vector [0.87, 0.43, 0.22].

Now consider another vector in which the elements are twice the value of the first vector 2 x [ 128, 64, 32 ] = [ 256,
128, 64 ].
You can work it out yourself to see that normalizing [ 256, 128, 64 ] will result in [0.87, 0.43, 0.22], which is the same
as the normalized version of the original RGB vector. You can see that normalizing a vector removes the scale

A 16×16 block has 4 histograms which can be concatenated to form a 36 x 1 element vector and it can be
normalized just the way a 3×1 vector is normalized. The window is then moved by 8 pixels ( see animation ) and a
normalized 36×1 vector is calculated over this window and the process is repeated.
Step 5 : Calculate the Histogram of Oriented Gradients feature vector

1. How many positions of the 16×16 blocks do we have ? There are 7 horizontal and 15
vertical positions making a total of 7 x 15 = 105 positions.
2. Each 16×16 block is represented by a 36×1 vector. So when we concatenate them all into
one giant vector we obtain a 36×105 =  3780 dimensional vector.
HOG
import cv2
import os
os.chdir(r"C: \Users \amb \Downloads")
filename=‘pedestrians_1.jpg'
def main() :
hog = cV2. HOGDescriptor()
hog.
setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
image= cV2. imread(filename)
(bounding_ boxes, weights) = hog.
detectMultiScale(image,winstride=(4,4),padding=(8, 8),
scale=1.05)
HOG

for (x, y, W, h) in bounding_ boxes:


cv2.rectangle (image,(x, y) ,
(x + W, y + h),(0, 0, 255),4)
size = len(filename)
new_filename = filename[:size - 4]
new_filename = new_filename + Hog.jpg
cv2. imwrite(new_filename, image)
cv2.imshow("Image", image)
cv2.waitKey(0)

You might also like