Pedestrian Detection using OpenCV and H.O.G Descriptor

Annapurna Vinod
3 min readMay 17, 2021


A Beginner’s Project to Object Detection

When it comes to learning object detection and image classification, using the OpenCV library is a good place to start. For computer vision to image processing and classification, OpenCV runs on a wide variety of programming languages like Python, C++, Java, etc. Here, we are using OpenCV along with the Histogram Of Oriented Gradients (H.O.G) descriptor to predict objects. This feature got wide recognition after Navneet Dalal and Bill Triggs published a paper called Histogram of Oriented Gradients for Human Detection in 2005. Their initial works were also based on detection in static images.


What is the Histogram Of Oriented Gradients Descriptor?

The key thought behind the H.O.G descriptor is that local object appearance and shape within an image can be described by the distribution of intensity gradients or edge directions(orientations). Each image is divided into minute connected regions called cells, and for the pixels within each cell, a histogram of gradient directions is generated. The descriptor is a nexus of all these histograms. To read more about the theory behind it, visit Wikipedia.

The gradients associated with an image pixel is located using the formulae given below :

source: TheALearner

Using an 8x8 pixel cell, we compute the horizontal and vertical gradients of the pixels. The horizontal and vertical gradients are used to find the Gradient Magnitude and Direction. This is compressed into 9 vectors(0 to 180° each with 20 ° in each bin.), to plot a histogram of magnitudes and angles.

A Classifying model like the Support Vector Algorithm (SVM) can be further used to recognize HOG descriptors of people.

I encourage you to start building projects in Jupyter Notebooks at the beginner level.

import cv2

img=cv2.imread(“image pathway”,0)

hog = cv2.HOGDescriptor()
(regions, _) = hog.detectMultiScale(img,
winStride=(4, 4),
padding=(4, 4),

for (x, y, w, h) in regions:
cv2.rectangle(img, (x, y),
(x + w, y + h),
(0, 0, 255), 2)

cv2.imshow(“Img”, img)


Importing library and running the program
Detection in a converted Grayscale image.