网络编程
位置:首页>> 网络编程>> Python编程>> Python 使用Opencv实现目标检测与识别的示例代码

Python 使用Opencv实现目标检测与识别的示例代码

作者:Xy-Huang  发布时间:2023-01-23 17:46:11 

标签:Opencv,目标检测,识别

在上章节讲述到图像特征检测与匹配 ,本章节是讲述目标检测与识别。后者是在前者的基础上进一步完善。
在本章中,我们使用HOG算法,HOG和SIFT、SURF同属一种类型的描述符。功能代码如下:


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()

运行结果如图所示:

Python 使用Opencv实现目标检测与识别的示例代码

这个例子是使用HOG特征进行SVM算法训练,这部分已开始涉及到机器学习的方面,通过SVM算法训练数据集,然后根据某图像与数据集进行匹配。

来源:https://blog.csdn.net/HuangZhang_123/article/details/80746847

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com