#检测行人
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()
HOG分析
hog = cv2.HOGDescriptor()
HOGDescriptor (Size _winSize, Size _blockSize, Size _blockStride, Size _cellSize, int _nbins, int _derivAperture=1, double _winSigma=-1, int _histogramNormType=HOGDescriptor::L2Hys, double _L2HysThreshold=0.2, bool _gammaCorrection=false, int _nlevels=HOGDescriptor::DEFAULT_NLEVELS, bool _signedGradient=false)
1、HOGDescriptor() [1/4]
cv::HOGDescriptor::HOGDescriptor ( )
Creates the HOG descriptor and detector with default params.
//用默认参数创建HOG描述符和探测器
aqual to HOGDescriptor(Size(64,128), Size(16,16), Size(8,8), Size(8,8), 9, 1 )
2、HOGDescriptor() [2/4]
cv::HOGDescriptor::HOGDescriptor (
Size _winSize, #设置被给值的窗口值
Size _blockSize,#设置被给值的区块值
Size _blockStride,#设置被给值的步长值
Size _cellSize, #设置被给值的分辨率
int _nbins, #?
int _derivAperture = 1,
double _winSigma = -1,
int _histogramNormType = HOGDescriptor::L2Hys,#HOG类型
double _L2HysThreshold = 0.2, #L2Hys 阈值
bool _gammaCorrection = false, #?
int _nlevels = HOGDescriptor::DEFAULT_NLEVELS, #?
bool _signedGradient = false #?
)
3、HOGDescriptor() [3/4]
cv::HOGDescriptor::HOGDescriptor ( const String & filename )
//filename 包含HOGDescriptor 特性和训练分类器的系数
~HOGDescriptor()
virtual cv::HOGDescriptor::~HOGDescriptor ( )
Default destructor.#?
//成员功能说明:
checkDetectorSize()
bool cv::HOGDescriptor::checkDetectorSize ( ) const
//检测探测器尺寸是否等于描述符尺寸
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
setSVMDetector()
virtual void cv::HOGDescriptor::setSVMDetector ( InputArray _svmdetector )
//设置线性支持向量机分类器系数
//_svmdetector 线性支持向量机分类器系数
getDefaultPeopleDetector()
static std::vector<float> cv::HOGDescriptor::getDefaultPeopleDetector ( )
Returns coefficients of the classifier trained for people detection (for 64x128 windows).
Examples:
peopledetect.cpp.
//返回行人检测训练分类器的系数
found, w = hog.detectMultiScale(img) # 加载图像
detectMultiScale() [1/2]
virtual void cv::HOGDescriptor::detectMultiScale (
InputArray img,#8为单通道或3通道矩阵类型的图像,包含被检测的目标
std::vector< Rect > & foundLocations,#矩形向量,每个矩形包含被检测的目标
std::vector< double > & foundWeights,#
double hitThreshold = 0,
Size winStride = Size(),
Size padding = Size(),
double scale = 1.05,
double finalThreshold = 2.0,
bool useMeanshiftGrouping = false
) const
Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
Parameters
img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
foundLocations Vector of rectangles where each rectangle contains the detected object.
foundWeights Vector that will contain confidence values for each detected object.
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.
winStride Window stride. It must be a multiple of block stride.
padding Padding
scale Coefficient of the detection window increase.
finalThreshold Final threshold
useMeanshiftGrouping indicates grouping algorithm
Examples:
hog.cpp, peopledetect.cpp, and train_HOG.cpp.
detectMultiScale() [2/2]
virtual void cv::HOGDescriptor::detectMultiScale ( InputArray img,
std::vector< Rect > & foundLocations,
double hitThreshold = 0,
Size winStride = Size(),
Size padding = Size(),
double scale = 1.05,
double finalThreshold = 2.0,
bool useMeanshiftGrouping = false
) const
virtual
Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
Parameters
img Matrix of the type CV_8U or CV_8UC3 containing an image where objects are detected.
foundLocations Vector of rectangles where each rectangle contains the detected object.
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.
winStride Window stride. It must be a multiple of block stride.
padding Padding
scale Coefficient of the detection window increase.
finalThreshold Final threshold
useMeanshiftGrouping indicates grouping algorithm
for ri, r in enumerate(found):
1、class enumerate(object):
"""
enumerate(iterable[, start]) -> iterator for index, value of iterable
Return an enumerate object. iterable must be another object that supports
iteration. The enumerate object yields pairs containing a count (from
start, which defaults to zero) and a value yielded by the iterable argument.
enumerate is useful for obtaining an indexed list:
(0, seq[0]), (1, seq[1]), (2, seq[2]), ...
"""