SVM, Hog feature

A .SVM (SVM)
1. Definition:
SVM classifier is essentially a still solve the problem of classification, by seeking an optimal hyperplane to complete the classification. Classification can be classified by a straight line, a curve may be, the following case is a straight line with the classification, i.e., the core is svm line.
2. Case:
training samples person's height and weight to classify, to determine boys or girls, and arbitrarily given a person's height and weight to make predictions boys or girls.

import cv2
import numpy as np

#1.准备数据
rand1 = np.array([[155,48],[159,50],[164,53],[168,56],[172,60]]) #女生样本
rand2 = np.array([[152,53],[156,55],[160,56],[172,64],[176,65]]) #男生样本

#2.标签label
label = np.array([[0],[0],[0],[0],[0],[1],[1],[1],[1],[1]])

#3.数据
data = np.vstack((rand1,rand2)) #将数据1和2合并
data = np.array(data,dtype='float32')

#SVM所有的数据都要有label,即监督学习 0负样本 1正样本
#[155,48]——0,女生  [152,53]——1,男生
#4.SVM属性的设置
svm = cv2.ml.SVM_create() #创建SVM ml机器学习模块

#SVM属性设置
svm.setType(cv2.ml.SVM_C_SVC) #设置SVM的类型
svm.setKernel(cv2.ml.SVM_LINEAR) #设置线性分类器
svm.setC(0.01) #SVM内核

#5.开始训练
result = svm.train(data,cv2.ml.ROW_SAMPLE,label) #将svm设为简单模式

#6.预测
pt_data = np.vstack([[167,55],[162,57]]) #预测为0女生 1男生
pt_data = np.array(pt_data,dtype='float32')
print(pt_data) #打印初始样本
(par1,par2) = svm.predict(pt_data)
print(par2) #打印预测的值

Output:
[[167. 55.]
[162. 57.]]
[[of 0. The] [1.]]
3.SVM using step
① preparing data samples, positive and negative samples requires two data, of the numbers it does not matter;
② data for each sample to be tagged, that a positive and negative, which is supervised learning: 0 negative samples 1 positive samples;
③ start training data:
a first we need to create a SVM:. cv2.ml. SVM_create ()
. the second set of SVM type B: setType (cv2.ml.SVM_C_SVC)
C select which type of classifier:. setKernel ()
. D SVM kernel size set SetC ()
E forecast data.

Wherein two .Hog
1.Hog feature definition
feature refers to the results after a certain pixel obtained by the calculation, the Haar features are calculation results obtained by the template, and the template after Hog characterized by operation needs further calculated, the ratio Haar features more complicated.
2. The workflow
① module division
② compute the gradient and direction of each module
③bin projection
Hog characterized ④ calculated overall
judgment characteristics ⑤
3. proper noun interpretation, conceptual understanding
Here Insert Picture Description
Step a: ① module division
1.image (upper integer images) windows form (blue rectangle)
block (red rectangle)
cell (green rectangle)
size: image> windows Forms> block> cell
2.Windows form is the topmost cell feature calculation, i.e., includes all the features of the target (for example, you want to test the car, and that car windows form is the whole picture) windows window size is arbitrary, the official recommended: 128 × 64-
3.block size is arbitrary, but windows form size is an integer multiple of the block size, the official recommendation: 16 × 16
with a step size when 4.block moves setp, setp represents in block form the sliding windows, the size of 8 × 8, represents the distance of the sliding block It is the horizontal and vertical directions for each of 8 units
counting the number of the block: block count = ((64-16) / 8 + 1) × ((128-16) / 8 + 1) = 105 th block
5.cell size recommended: 8 × 8, block = 4 th cell ((16 × 16) / (8 × 8)), named for this four cell Cell1, Cell2, the cell 3, CELL4
6.cell corresponding there bin, bin refers to the calculations of the gradient of
each pixel has a magnitude of the gradient, called f (amplitude), i.e. the direction (angle angle) of
the 0-360 divided every 40 degrees one, i.e. 360 / 40 = 9, 9 into the bin, as shown below
Here Insert Picture Description
7.Hog feature dimensions: Haar feature value is calculated, and the calculated Hog is a vector (a vector are all dimensions), a complete description of the target All information.
Dimension = 105 × 4 × 9 = 3780 ( number of windows for each block of the window 105, the number of each cell in the block is 4, the number of the bin 9 for each cell)

Step two: ② compute the gradient direction of each module and
each pixel has a gradient, but here with the features Hog feature template, and this Haar similar, but less Hog feature module type.
Horizontal feature template: feature template [1 0 -1] in the vertical direction: [[1] [0] [-1]]
pixels in the horizontal direction a = p1 × 1 + p2 × 0 + p3 × (- 1) = difference between adjacent pixels of
the difference between the upper and lower pixels of the pixel b in the vertical direction = p1 × 1 + p2 × 0 + p3 × (-1) =
then the magnitude of the gradient magnitude at root f = (a + square b square)
angle = arctan (A / b)

Step Three: ③bin projection: gradient projection
bin 360 is divided into 9 portions, i.e. each bin is 0-40 degrees, 40 degrees and this is not continuous, it is assumed bin1 range 0-20 degrees, and bin1 range is 180-200 degrees, i.e. symmetrical relationship. For example a pixel amplitude a = 10, belong bin1, a = 190 belong bin1.
If the magnitude of a bin just between the two, if it is 25, and belong to bin1 BIN2, there is a simple to understand formula: f1 = f × f angle
f2 = f × (1-f angle) (the PS : f angle range is 0 to 1.0, float)

Step Four: Hog calculated overall characteristics ④: cell multiplexing
overall dimensions are 3780, Hog features a bin in a certain cell in a block in each dimension is calculated in a window windows

Step Five: ⑤ decision features
all features calculated 3780 Hog dimensions
cited here svm line training example, the threshold T Hog × svm if greater than the set, it is determined that target.

4. Case
steps:
1. Prepare the sample data (positive and negative samples have been uploaded)
2. Start training
3. The results of the training was predicted
1.1 pos: positive samples, containing the detected target neg: negative sample does not include the detection target to unify image size, the proportion of positive and negative samples is 1: 2 or 1: 3, sample diverse as possible

import cv2
import numpy as np

"""1.准备样本数据"""
PosNum = 820   #正样本数量820
NegNum = 1931  #负样本数量1931
windowsSize = (64,128) #设置windows窗体的大小
blockSize = (16,16) #windows = 105个 block
blockStep = (8,8) #block移动的步长
cellSize = (8,8) #cell的大小
nBin = 9 #每个cell中bin的个数

"""2.Hog的创建"""
hog = cv2.HOGDescriptor(windowsSize,blockSize,blockStep,cellSize,nBin)

"""3.创建SVM"""
svm = cv2.ml.SVM_create()

"""4.计算Hog特征"""
featureNum = int(((128-16)/8+1)*((64-16)/8+1)*4*9) #维度数3780
featureArray = np.zeros(((PosNum+NegNum),featureNum),np.float32)
#featureArray表示所有样本的Hog信息,是一个二维的数据
labelArray = np.zeros(((PosNum+NegNum),1),np.int32) #标签数
#这里之所以要设置featureArray和labelArray,是因为svm是监督学习,需要样本和标签
#而svm学习的样本,是我们原数据的Hog特征,而不是直接的样本,所以需要featureArray
for i in range(0,PosNum): #遍历每个正样本获取Hog特征
    filename = 'E:\\pictures\\pos\\' + str(i+1) + '.jpg'
    img = cv2.imread(filename)
    hist = hog.compute(img,(8,8)) #用hist装载正样本的hog特征,是3780维度的
    for j in range(0,featureNum): #将获取到的正样本hog特征装载到featureArray二维中
        featureArray[i,j] = hist[j]
        #理解:featureArray是(820+1931)*3780,hist是1*3780
        #即把每一个hist装载到featureArray的前820列
    labelArray[i,0] = 1 #正样本label 1
for i in range(0,NegNum): #遍历每个负样本获取Hog特征
    filename = 'E:\\pictures\\neg\\' + str(i + 1) + '.jpg'
    img = cv2.imread(filename)
    hist = hog.compute(img, (8, 8))  # 用hist装载正样本的hog特征,是3780维度的
    for j in range(0, featureNum):  # 将获取到的正样本hog特征装载到featureArray二维中
        featureArray[i+PosNum,j] = hist[j]
        # 理解:featureArray是(820+1931)*3780,hist是1*3780
        # 即把每一个hist装载到featureArray的前820列
    labelArray[i+PosNum, 0] = -1  # 负样本label -1

"""5.设置svm属性"""
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)

"""6.训练样本"""
result = svm.train(featureArray,cv2.ml.ROW_SAMPLE,labelArray)
#虽然看上去result后面没有调用,但只有SVM训练了(svm.train)后面才能用

"""7.检测"""
alpha = np.zeros((1),np.float32)
rho = svm.getDecisionFunction(0,alpha)
#rho表示svm得到Hog的一个描述信息,rho来源于svm,svm来源于svm.train
print(rho)
print(alpha)
alphaArray = np.zeros((1,1),np.float32)
support_SVMVector_Array = np.zeros((1,featureNum),np.float32)
resultArray = np.zeros((1,featureNum),np.float32)
alphaArray[0,0] = alpha
resultArray = -1*alphaArray*support_SVMVector_Array

"""8.预测"""
#创建myDetect
myDetect = np.zeros((3781),np.float32)
for i in range(0,3780):
    myDetect[i] = resultArray[0,i]
myDetect[3780] = rho[0]
#构建Hog
myHog = cv2.HOGDescriptor()
myHog.setSVMDetector(myDetect)
imageSrc = cv2.imread("E:\\pictures\\Test2.jpg",1) #下载待检测图片
objs = myHog.detectMultiScale(imageSrc,0,(8,8),(32,32),1.05,2)#返回三维数组
#最后一维
x = int(objs[0][0][0])
y = int(objs[0][0][1])
w = int(objs[0][0][2])
h = int(objs[0][0][3])
#目标绘制的展示
cv2.rectangle(imageSrc,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('dst',imageSrc)
cv2.waitKey(0)

Output:
(0.2555259838518084,
Array ([[. 1].]), Array ([[0]], DTYPE = Int32))
[of 0. The]
Here Insert Picture Description

Published 25 original articles · won praise 0 · Views 440

Guess you like

Origin blog.csdn.net/qq_45445740/article/details/104497343
svm