OpenCV 自带的HOGDescriptor类可以用来检测人。
下面是一个简单的例子,只使用默认参数。
import cv2
import numpy as np
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(img, (x,y),(x+w,y+h),(0,255,255),2)
img =cv2.imread("6.jpg")
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found,w = hog.detectMultiScale(img)
found_filtered = []
for ri, r in enumerate(found):
for qi,q in enumerate(found):
if ri != qi and is_inside(r,q):
break
else:
print(w[ri])
found_filtered.append(r)
for person in found_filtered:
#for person in found:
draw_person(img,person)
print("找到%d个人"%len(found_filtered))
cv2.imshow("people detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
下面是检测结果:
不尽如人意,检测器好像对人腿情有独钟。下面给它喂一点大长腿:
这张却只检测到手臂了.......说明它很任性。
detectMultiScale()函数参数说明:
'''
detectMultiScale(...) method of cv2.HOGDescriptor instance
detectMultiScale(img[, hitThreshold[, winStride[, padding[, scale[, finalThreshold[, useMeanshiftGrouping]]]]]]) -> foundLocations, foundWeights
. @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
. of rectangles.
. @param img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
. @param foundLocations Vector of rectangles where each rectangle contains the detected object.
. @param foundWeights Vector that will contain confidence values for each detected object.
. @param hitThreshold Threshold for the distance between features and SVM classifying plane.
. Usually it is 0 and should be specified in the detector coefficients (as the last free coefficient).
. But if the free coefficient is omitted (which is allowed), you can specify it manually here.
. @param winStride Window stride. It must be a multiple of block stride.
. @param padding Padding
. @param scale Coefficient of the detection window increase.
. @param finalThreshold Final threshold
. @param useMeanshiftGrouping indicates grouping algorithm
<1>img:源图像。
<2>foundlocations:检测出的物体的边缘。
<3>foundWeights: 检测窗口得分
<4>hit_threshold:阀值,特征向量和SVM划分超平面的距离,大于这个值的才作为目标返回。
<4>win_stride:窗口步长,必须是block步长的整数倍。
<5>padding:图片边缘补齐参数,gpu版本必须是(0,0)。
<6>scale0:检测窗口增长参数。
<7>finalThreshold:检测结果聚类参数<8>useMeanshiftGrouping:聚类方式选择的参数
'''
本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!