需注意此代码要在Release x64之下运行
#include <iostream>
#include <stdexcept>
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
//Release x64 版本
using namespace cv;
using namespace std;
int main()
{
Mat src = imread("pic4.png");;
if (!src.data)
{
cout << "图片未找到!" << endl;
return -1;
}
imshow("input title", src);
/*resize(src, src, Size(64, 128));
HOGDescriptor detector = HOGDescriptor(Size(64, 128), Size(16, 16), Size(8, 8), Size(8, 8), 9);
vector<float>descriptions;
vector<Point>points;
detector.compute(src, descriptions, Size(0, 0), Size(0, 0), points);
cout << "the number of HOG descriptions:" <<descriptions.size()<< endl;
*/
HOGDescriptor hog = HOGDescriptor();
hog.setSVMDetector(hog.getDefaultPeopleDetector());//opencv中已经有训练好的行人检测数据集
Mat resultImg = src.clone();
vector<Rect>foundlocations;
hog.detectMultiScale(src, foundlocations, 0, Size(3, 3), Size(32, 32), 1.05, 2);
//根据图片需要随时调整Size()大小
for (size_t i = 0; i < foundlocations.size(); i++)
{
//cout << "foundlocation:" << foundlocations[i] << endl;
rectangle(resultImg, foundlocations[i], Scalar(0, 0, 255), 2, 8, 0);
}
imshow("HOG SVM demo", resultImg);
waitKey(0);
return 0;
}
运行结果:
我的总结:
之前复制网上代码一致不行,执行到 hog.setSVMDetector(hog.getDefaultPeopleDetector())就出错。大概就是Debug Error之类的。曾经怀疑是不是我的opencv3版本中没有这个,于是找到opencv中的samples下peopledetect.cpp案例。那就说明此opencv版本是支持hog的。于是接着找问题在哪里。什么改变平台之类的都试过一遍,还是不行。最后想起可能是引入的包不对?于是就把sample中peopledetect.cpp中的所有include拷贝过来就ok了。