API:
HOGDescriptor(Size _winSize, ---:窗口大小,即检测的范围大小,前面的64*128
Size _blockSize,--- 前面的2*2的cell,即cell的数量,这里要填像素值Size(16,16)
Size _blockStride,---每次block移动的步长,以像素计,为一个cell像素块大小
Size _cellSize, ---cell的大小,前面的8*8
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)
#include <opencv2/opencv.hpp>
//#include <opencv2/xfeatures2d.hpp>
#include <iostream> using namespace cv;
//using namespace cv::xfeatures2d;
using namespace std; int main(int argc, char** argv) {
Mat src = imread("test.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); Mat dst, dst_gray;
resize(src,dst,Size(,));// 改变大小 cvtColor(dst,dst_gray,COLOR_BGR2GRAY); HOGDescriptor detector(Size(, ), Size(, ), Size(, ), Size(, ),);
vector<float> descriptors;//直方图向量
vector<Point>locations;
detector.compute(dst_gray, descriptors,Size(,),Size(,),locations);
printf("number of HOG descriptors :%d", descriptors.size()); waitKey();
return ;
}
使用OpenCV已经训练好的模型实现行人检测
#include <opencv2/opencv.hpp>
#include <iostream> using namespace cv;
using namespace std; int main(int argc, char** argv) {
Mat src = imread("行人.jpg");
if (src.empty()) {
printf("could not load image...\n");
return -;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); //使用opencv已经训练好的模型,实现行人检测
HOGDescriptor hog= HOGDescriptor();
hog.setSVMDetector(hog.getDefaultPeopleDetector()); vector<Rect> foundLocations;
hog.detectMultiScale(src, foundLocations,,Size(,),Size(,),1.05,);//在多尺度上寻找
for (size_t t = ; t < foundLocations.size(); t++) {
rectangle(src, foundLocations[t],Scalar(,,),,,);
} namedWindow("HOG行人检测",CV_WINDOW_AUTOSIZE);
imshow("HOG行人检测",src); waitKey();
return ;
}