# 样本 训练 预测test
# 样本 pos:neg = 1:2 或1:3
# 步骤: 1参数 2hog 3svm 4compute hog 5label 6train 7pred 8draw
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1par
posNum = 820
negNum = 1931
winSize = (64, 128)
blockSize = (16, 16) # 105block
blockStride = (8, 8)
cellSize = (8, 8) # 每个block有4cell
nBin = 9 # 每个cell有9bin => 3780维
# 2hog
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nBin)
# 3svm
svm = cv2.ml.SVM_create()
# 4 comoute hog
featureNum = int(((128 - 16)/8 + 1) * ((64 - 16)/ 8 + 1)* 4 * 9) # 3780
print('featureNum:', featureNum)
featureArray = np.zeros((posNum + negNum, featureNum), np.float32)
# 5 label
labelArray = np.zeros((posNum + negNum, 1), np.int32)
# svm监督学习需要样本(中的hog特征)和标签
for i in range(0, posNum):
fileName = 'pos/' + str(i + 1) + '.jpg'
img = cv2.imread(fileName)
hist = hog.compute(img, (8, 8)) # 3780
# hist(3780维)装入featureArray
for j in range(0, featureNum):
featureArray[i, j] = hist[j]
# hog1: hog[1, :] hog2: hog[2, :]
# n*1 第一个是正样本 正样本处理完
labelArray[i, 0] = 1
for i in range(0, negNum):
fileName = 'neg/' + str(i + 1) + '.jpg'
img = cv2.imread(fileName)
hist = hog.compute(img, (8, 8))
for j in range(0, featureNum):
featureArray[i + posNum, j] = hist[j]
# 负样本处理完
labelArray[i + posNum, 0] = -1
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)
# 6train
ret = svm.train(featureArray, cv2.ml.ROW_SAMPLE, labelArray)
# 一维
alpha = np.zeros((1), np.float32)
# svm训练后得到,判决时用到
rho = svm.getDecisionFunction(0, alpha)
print('alpha: ', alpha)
print('rho: ', rho)
# 与svm数组相乘 alphaArray为一行一列的二维数组【一维】
alphaArray = np.zeros((1, 1), np.float32)
supportVArray = np.zeros((1, featureNum), np.float32) # 支持向量个数
resultArray = np.zeros((1, featureNum), np.float32)
alphaArray[0, 0] = alpha
resultArray = -1 * alphaArray * supportVArray # 3780维
# 7 test(detect) 核心:myHog-> myDetect
# myDetect为一维数组,来自resultArray与rho;最后myHog.detectMultiScale
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)
# load
imageSrc = cv2.imread('Test.jpg', 1)
# detectMultiScale是的检测核心 win滑动步长:(8, 8) winSize:(32, 32) 缩放系数:1.05
objs = myHog.detectMultiScale(imageSrc, 0, (8, 8), (32, 32), 1.05, 2)
# xy wh 三维 最后一维的0-3
x = int(objs[0][0][0])
y = int(objs[0][0][1])
w = int(objs[0][0][2])
h = int(objs[0][0][3])
# 8 绘制展示
cv2.rectangle(imageSrc, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('dst', imageSrc)
cv2.waitKey(0)
识别结果如下: