Hog and SVM

2018-08-03  本文已影响0人  cxfwxy
#object recognation through Hog and SVM
#source code from net
#modified and comment by cxfwxy
#env: Python 3.5, OpenCV 3.3.1, Anaconda, Jupyter Notebook

import cv2
import numpy as np
#import matplotlib.pyplot as plt

##basic parameters
#number of positive samples
PosNum = 820
#number of negative samples
NegNum = 1931
##for Hog
#size of window
winSize = (64, 128)
#size of block
blockSize = (16, 16)
#step for block
blockStride = (8, 8)
#size of cell
cellSize = (8, 8)
#number of bin for one cell, each 20 degree in reverse
nBin = 9

#create Hog
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nBin)

#create SVM
svm = cv2.ml.SVM_create()

#calculate the feature number, the total feature number here is 3780
featureNum = int(((128-16)/8+1)*((64-16)/8+1)*4*9)
#create the feature array, here we stach the positive samples and negative samples
#into together, so the matrix is (2751, 3780), and the data type is set as float32
featureArray = np.zeros(((PosNum+NegNum),featureNum),np.float32)
#for this is a supervised learning, so the label is necessary, and referring to the 
#feature array, the size of label array is (2751, 1), and the data type is int32 
labelArray = np.zeros(((PosNum+NegNum),1),np.int32)

#supervised learning, label
##set labels for positive samples, for positive label is 1
for i in range(0, PosNum):
    #from pos folder read pictures
    fileName = 'pos\\'+str(i+1)+'.jpg'
    img = cv2.imread(fileName)
    #compute feature value, cell size is (8, 8)
    hist = hog.compute(img,(8,8))
    #put feature value into feature array
    for j in range(0, featureNum):
        featureArray[i,j]=hist[j]
    #set positive samples' labels as 1
    labelArray[i,0] = 1

##set labels for negative samples, for negative label is -1
for i in range(0, NegNum):
    #from neg folder read pictures
    fileName = 'neg\\'+str(i+1)+'.jpg'
    img = cv2.imread(fileName)
    #compute feature value, cell size is (8, 8)
    hist = hog.compute(img,(8,8))
    #put feature value into feature array
    for j in range(0, featureNum):
        featureArray[i+PosNum,j]=hist[j]
    #set negative samples' labels as -1
    labelArray[i+PosNum,0] = -1
    
#set SVM type as SVM_C_SVC
svm.setType(cv2.ml.SVM_C_SVC)
#use linear method
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)

#training SVM
svm.train(featureArray,cv2.ml.ROW_SAMPLE,labelArray)

#get alpha from SVM
alpha = np.zeros((1),np.float32)
rho = svm.getDecisionFunction(0,alpha)
#print(rho)
#print(alpha)
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

myDetect = np.zeros((3781),np.float32)
for i in range(0,3780):
    myDetect[i] = resultArray[0,i]
myDetect[3780] = rho[0]

myHog = cv2.HOGDescriptor()
myHog.setSVMDetector(myDetect)
imageSrc = cv2.imread('test.jpg',1)
objs = myHog.detectMultiScale(imageSrc,0,(8,8),(32,32),1.05,2)
#vertices for rectangle 
x = int(objs[0][0][0])
y = int(objs[0][0][1])
w = int(objs[0][0][2])
h = int(objs[0][0][3])
#use rectangle to mark object
cv2.rectangle(imageSrc,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('dst',imageSrc)
cv2.waitKey(0)
上一篇 下一篇

猜你喜欢

热点阅读