HOG+SVM|object detection

Today, I will show you something interesting, including feature of HOG as well as SVM to make object detection based on Python. Let’s start!

1.HOG

HOG(Histogram of Oriented Gradient), the way being used in computer vision to find the edge of object.
Let’s make comparison with dy/dx in calculus, it’s seems that HOG just use the same way in pixel to discover something in common. for instance, I give you about 1000 pictures of different lions which will be dealt by a lot of windows, blocks, cells and bins. Wait a minute, you maybe never know what these words mean. It’s just like a simple square combined with 4 or 9 pixels, which will sweep every single pixel to do as dy/dx in order to draw an arrow with size and direction, similar to gradient in calculus.
在这里插入图片描述
Well, it is so apparent that the edge of the number “3” is shown without any mistakes, but how we can use this way to detect specific object? Please continue reading!

2.SVM

SVM(Support Vector Machine), a way to make classification based on a lot of positive and negative samples, is so popular to solve problems about object detection.
Please imagine a 2D flat with two different points, whose color is white and black. And we have to find difference between them to detect the white points. The first idea coming to our mind is to draw a line to divide them into different groups. But it’s easy to notice that only a single line is not enough because of so many places in common. Next, Thinking about these points in 3D, 4D or more complex spaces, the solution is not a line, but a multiple flat. Most importantly, we don’t have to discover these flats by ourselves, and there are plenty of ways to do the best job for this.

The above is some basic theories for HOG and SVM. Next, let’s accomplish it based on our code.

3. achievement

  1. import your modules and set parameters properly.
    More clearly, we have to set the size of widows, blocks, cells and bins as the base of following steps.
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1 par
PosNum = 820
NegNum = 1931
winSize = (64,128)
blockSize = (16,16)# 105
blockStride = (8,8)#4 cell
cellSize = (8,8)
nBin = 9#9 bin 3780

2)create your model by HOG and SVM in cv2

hog = cv2.HOGDescriptor(winSize,blockSize,blockStride,cellSize,nBin)
svm = cv2.ml.SVM_create()
# 4 computer hog
featureNum = int(((128-16)/8+1)*((64-16)/8+1)*4*9) #3780
featureArray = np.zeros(((PosNum+NegNum),featureNum),np.float32)
labelArray = np.zeros(((PosNum+NegNum),1),np.int32)
  1. divide them into two different groups, including positive and negative species.
for i in range(0,PosNum):
    fileName = 'pos/'+str(i+1)+'.jpg'
    img = cv2.imread(fileName)
    hist = hog.compute(img,(8,8))# 3780
    for j in range(0,featureNum):
        featureArray[i,j] = hist[j]
    # featureArray hog [1,:] hog1 [2,:]hog2 
    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))# 3780
    for j in range(0,featureNum):
        featureArray[i+PosNum,j] = hist[j]
    labelArray[i+PosNum,0] = -1

4)train your model
Set your type and kernel properly and train it.

svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_LINEAR)
svm.setC(0.01)
# 6 train
ret = svm.train(featureArray,cv2.ml.ROW_SAMPLE,labelArray)

5)create your decision function based on the result of training to find feature. In addition, you also have to create your own detection

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
# detect
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)
# load 
imageSrc = cv2.imread('24.jpg',1)

cap = cv2.VideoCapture("1.mp4")

6)Finally, draw the rectangle to make your detection obviously.
What’s more, the “objs” has so many different dimensions, but we only focus on the last dimension, including the size of your detection.

while(cap.isOpened()):
    ret,frame1 = cap.read()
    # (8,8) win 
    objs = myHog.detectMultiScale(frame1,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(frame1,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.imshow('dst',frame1)
    if cv2.waitKey(1) == 27:
        break

cv2.destroyAllWindows()
cv2.release()

In short, it’s so important to combine features and your classification properly to find objects in your positive and negative samples. Sometimes, the computer will help us save a lot of time!
That’s all, thank you for reading.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值