HOG(Histogram of Oriented Gradients)&SVM (Support Vector Machine)
1. Background Information
1.1 Hog (Histogram of Oriented Gradients)
HOG is a feature description for object detection in computer vision and image processing. It constructs features by calculating and counting the gradient direction histogram of the local area of the image.
The first-order differentiation in digital image processing is achieved with the magnitude of the gradient.
For f (x, y), the gradient of f at coordinates (x, y) is defined as a two-dimensional column vector:
As with the definition of the simulation function z = f (x, y), this vector has an important geometric meaning, which indicates the direction of the maximum rate of change at the (x, y) position. , Its amplitude (length) can be expressed as M (x, y) as:
1.2 SVM(Support Vector Machine)
Support Vector Machine (SVM) is a generalized linear classifier that performs binary classification on data by supervised learning. Its decision boundary is the maximum margin hyperplane that solves the learning samples.
There are two main functions that svm can achieve, one is classification, and the other is regression. Here we use classification.
Details can be seen in the article published in 5.6.
2. Flow Chart
2.1 Normalization of picture gamma and color
2.2 Calculate gradient
2.3 Construct a histogram
2.4 Normalization of the aliasing space block of Block
2.5 Construct HOG feature descriptors
2.6 SVM training
The above six points are the general process steps for object detection utilizing HOG and SVM.
However, with the development of technology, someone has developed an optimized object detection program that combines the two skills.
Opencv, as an open source visual library, has already integrated HOG + SVM algorithm. So we only need to call API (Application Programming Interface) to implement pedestrian detection, the program can be seen below.
3. Template Program
Purpose: to identify the people in the picture we have given.
Step1. Import the packages.
import cv2
import numpy as np
import matplotlib.pyplot as plt
Step 2. Define the function to detect person using svm.
def svmdetectperson(img):
hog=cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
person,w= hog.detectMultiScale(img)
return person
Step 3.Find the outermost contour of each detected object, and avoid duplication.
#judge b is not include a
def is_inside(a,b):
x1,y1,w1,h1=a
x2,y2,w2,h2=b
return x1>x2 and y1>y2 and x1+w1<x2+w2 and y1+h1<y2+h2
Step 4.Mark the detected area with a rectangle
def draw(img,a):
x,y,w,h=a
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
Step 5. Mix the functions above and run the program.
if __name__=="__main__":
img=cv2.imread('test4.jpg')
person=svmdetectperson(img)
filtered=[]# to hold the contours.
# Find each outermost contour and add them to an array called filtered.
for i,p in enumerate(person):
for j,p1 in enumerate(person):
if i!=j and is_inside(p,p1):
break
filtered.append(p)
# draw the rectangle to indicate the detected area.
for p in filtered:
draw(img,p)
cv2.imshow("person",img)
cv2.waitKey(0)
result:
Thank you for reading!
--credit by dora 2020.5.7
Resources:
https://blog.csdn.net/qq_15642411/article/details/80732261
https://blog.csdn.net/qq_26898461/article/details/46786033
https://pan.baidu.com/s/1lh4UGXnbJ9smvq3QVTM8ww#list/path=%2F