前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV 检测人

OpenCV 检测人

作者头像
用户6021899
发布2020-07-13 10:05:18
8580
发布2020-07-13 10:05:18
举报

OpenCV 自带的HOGDescriptor类可以用来检测人。

下面是一个简单的例子,只使用默认参数。

代码语言:javascript
复制
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()函数参数说明:

代码语言:javascript
复制
'''
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:聚类方式选择的参数

'''
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档