来源:自学PHP网 时间:2020-09-27 14:59 作者:小飞侠 阅读:次
[导读] Python 使用Opencv实现目标检测与识别的示例代码...
|
今天带来Python 使用Opencv实现目标检测与识别的示例代码教程详解 在上章节讲述到图像特征检测与匹配 ,本章节是讲述目标检测与识别。后者是在前者的基础上进一步完善。 import cv2
def is_inside(o, i):
ox, oy, ow, oh = o
ix, iy, iw, ih = i
# 如果符合条件,返回True,否则返回False
return ox > ix and oy > iy and ox + ow < ix + iw and oy + oh < iy + ih
# 根据坐标画出人物所在的位置
def draw_person(img, person):
x, y, w, h = person
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)
# 定义HOG特征+SVM分类器
img = cv2.imread("people.jpg")
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found, w = hog.detectMultiScale(img, winStride=(8, 8), scale=1.05)
# 判断坐标位置是否有重叠
found_filtered = []
for ri, r in enumerate(found):
for qi, q in enumerate(found):
a = is_inside(r, q)
if ri != qi and a:
break
else:
found_filtered.append(r)
# 勾画筛选后的坐标位置
for person in found_filtered:
draw_person(img, person)
# 显示图像
cv2.imshow("people detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()运行结果如图所示:
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com