七:目标检测与识别
梯度直方图(Histogram of Oriented Gradient)
图像金字塔 (image pyramid)
滑动窗口(sliding window)
7.1、目标检测与识别
HOG描述符(详情见opencv特征提取描述符)
将图像分成小单元(16*16),每个单元包含视觉表示,安八个方向(N,NW,W,SW,W,SW,S,SE,E,NE)计算颜色梯度
尺度: ??
位置:检测目标可能位于图像任何地方,需扫描图像各个部分,找出感兴趣区域,并尝试检测目标
为解决位置、尺寸问题需熟悉图像金字塔和滑动窗口概念
非最大(极大)抑制:对图像同一区域相关的结果进行抑制,只关心结果最好的窗口,丢弃评分低的重叠窗口。
支持向量机SVM:
对于带有标签的训练数据,通过一个优化的超平面来对这些数据进行分类
2、检测人(opencv的HOG)
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2017/06/10
# @Author :
# @Site : 检测人
# @Software: PyCharm
#检测行人 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) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # person = np.array([[x, y, x + w, y + h] for (x, y, w, h) in person]) # pick = non_max_suppression(person, probs=None, overlapThresh=0.65) img = cv2.imread("C:\\Software\\Python\\snapshotoutdoor\\rightRGB_11.jpg") hog = cv2.HOGDescriptor() # 检测人的默认检测器 内置目标检测器 实际效果并不好(已经训练好的模型??) hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # SVM 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: found_filtered.append(r) #append 什么作用?? for person in found_filtered: draw_person(img, person) cv2.imshow("people detection", img) cv2.waitKey(0) cv2.destroyAllWindows()
另:上述方法对近景人检测效果不佳,下面来自一老外的方法,近景效果不错
#检测近景的效果与test_ObjectDetection.py效果好 # import the necessary packages from __future__ import print_function from imutils.object_detection import non_max_suppression from imutils import paths import numpy as np import argparse import imutils import cv2 # left_camera1 = cv2.VideoCapture(0) # construct the argument parse and parse the arguments # ap = argparse.ArgumentParser()# the path to the directory that contains the list of images we are going to perform pedestrian detection on. # ap.add_argument("-i", "--images", required=True, help="path to images directory") # args = vars(ap.parse_args()) imagePath = "C:\\Software\\Python\\snapshotoutdoor\\rightRGB_11.jpg" # initialize the HOG descriptor/person detector hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # loop over the image paths # for imagePath in paths.list_images(args["images"]):#start looping over the images in our --images directory # load the image and resize it to (1) reduce detection time # and (2) improve detection accuracy while True: # ret1, frame1 = left_camera1.read() # cv2.imshow("frame1", frame1) # cv2.waitKey(10) # image = frame1 image = cv2.imread(imagePath) # image = imutils.resize(image, width=min(400, image.shape[1])) orig = image.copy() # detect people in the image (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) # draw the original bounding boxes for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) # show some information on the number of bounding boxes # filename = imagePath[imagePath.rfind("/") + 1:] # print("[INFO] {}: {} original boxes, {} after suppression".format( # filename, len(rects), len(pick))) # show the output images cv2.imshow("Before NMS", orig) cv2.imshow("After NMS", image) cv2.waitKey(1)
3、创建和训练目标检测器
使用SVM和词袋(Bag of Word,BOW)
词袋:计算文档中每个词出现的次数,用该些次数构成的向量重新表示文档
示列如下:
I like opencv and i like python
I like c++ and python
I don’t like artichokes
可以用以下值来建立字
{
I:4,
Like:4,
Opencv:2,
And:2
Python:2
C++:1
Don’t:1
Artichokes:1
}
以上的三句话可以用以下向量表示:
[2,2,1,1,1,0,0,0]
[1,1,0,1,1,1,0,0]
[1,1,0,0,0,0,1,1]
代码实现???以后每学一个知识要留下学习过程并整理保存https://blog.csdn.net/lilai619/article/details/46740837
BoW(词袋模型)+python代码实现
????
K-means聚类:数据分析的向量量化方法
???