HOG(Histogram of Oriented Gradients)&SVM (Support Vector Machine)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值