HOG实现过程:
1灰度化(将图像看做一个x,y,z(灰度)的三维图像)
2采用Gamma校正法对输入图像进行颜色空间的标准化(归一化)
3计算图像每个像素的梯度(包括大小和方向)
4将图像划分为cells
5统计每个cell的梯度直方图(不同梯度的个数),得到cell的描述子
6将每几个cell组成一个block,得到block的描述子
7将图像image内的所有block的HOG特征descriptor串联起来就可以得到HOG特征,该特征向量就是用来目标检测或分类的特征。
import cv2
import numpy as np
#判断矩形i是否完全包含在矩形O中
def is_inside(o,i):
ox,oy,ow,oh = o
ix,iy,iw,ih = i
return ox > ix and oy > iy and ox+ow < ix + iw and oy +oh < iy + ih
#对人体绘制颜色框
def draw_person(image,person):
x,y,w,h = person
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,255),2)
img = cv2.imread("test5.jpg")
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found,w = hog.detectMultiScale(img,0.1,(1,1))
print(found)
print(w)
#丢弃某些完全被其他矩形包含在内的矩形
found_filtered=[]
for ri,r in enumerate(found):
for qi,q in enumerate(found):
if ri != qi and is_inside(r,q):
break
else:
found_filtered.append(r)
print(found_filtered)
#对不包含在内的有效矩形进行颜色框定
for person in found_filtered:
draw_person(img,person)
cv2.imshow("people detection",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
结果:
原图来源:百度图库
步长为(1,1)时候:
代码中由
(1,1)->(2,2)
改成(3,3)结果如下:
改成(7,7),据说。。最多检测5人。