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)
1 #include <opencv2/opencv.hpp> 2 //#include <opencv2/xfeatures2d.hpp> 3 #include <iostream> 4 5 using namespace cv; 6 //using namespace cv::xfeatures2d; 7 using namespace std; 8 9 int main(int argc, char** argv) { 10 Mat src = imread("test.jpg"); 11 if (src.empty()) { 12 printf("could not load image... "); 13 return -1; 14 } 15 namedWindow("input image", CV_WINDOW_AUTOSIZE); 16 imshow("input image", src); 17 18 Mat dst, dst_gray; 19 resize(src,dst,Size(64,128));// 改变大小 20 21 cvtColor(dst,dst_gray,COLOR_BGR2GRAY); 22 23 HOGDescriptor detector(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8),9); 24 vector<float> descriptors;//直方图向量 25 vector<Point>locations; 26 detector.compute(dst_gray, descriptors,Size(0,0),Size(0,0),locations); 27 printf("number of HOG descriptors :%d", descriptors.size()); 28 29 waitKey(0); 30 return 0; 31 }
使用OpenCV已经训练好的模型实现行人检测
1 #include <opencv2/opencv.hpp> 2 #include <iostream> 3 4 using namespace cv; 5 using namespace std; 6 7 int main(int argc, char** argv) { 8 Mat src = imread("行人.jpg"); 9 if (src.empty()) { 10 printf("could not load image... "); 11 return -1; 12 } 13 namedWindow("input image", CV_WINDOW_AUTOSIZE); 14 imshow("input image", src); 15 16 //使用opencv已经训练好的模型,实现行人检测 17 HOGDescriptor hog= HOGDescriptor(); 18 hog.setSVMDetector(hog.getDefaultPeopleDetector()); 19 20 vector<Rect> foundLocations; 21 hog.detectMultiScale(src, foundLocations,0,Size(8,8),Size(32,32),1.05,2);//在多尺度上寻找 22 for (size_t t = 0; t < foundLocations.size(); t++) { 23 rectangle(src, foundLocations[t],Scalar(0,0,255),2,8,0); 24 } 25 26 namedWindow("HOG行人检测",CV_WINDOW_AUTOSIZE); 27 imshow("HOG行人检测",src); 28 29 waitKey(0); 30 return 0; 31 }