opencv行人跟踪检测
#include <iostream>
#include <string>#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("3123.jpg");
HOGDescriptor hog;//HOG特征检测器
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());//设置SVM分类器为默认参数
vector<Rect> found, found_filtered;//矩形框数组
hog.detectMultiScale(src, found, 0, Size(8, 8), Size(32, 32), 1.05, 2);//对图像进行多尺度检测,检测窗口移动步长为(8,8)
cout << "矩形个数:" << found.size() << endl;
//找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中
for (int i = 0; i < found.size(); i++)
{
Rect r = found[i];
int j;
for (j=0; j < found.size(); j++)
if (j != i && (r & found[j]) == r)
break;
if (j == found.size())
found_filtered.push_back(r);
}
cout << "过滤后矩形的个数:" << found_filtered.size() << endl;
//画矩形框
for (int i = 0; i<found_filtered.size(); i++)
{
Rect r = found_filtered[i];
r.x += cvRound(r.width*0.1);
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.07);
r.height = cvRound(r.height*0.8);
rectangle(src, r.tl(), r.br(), Scalar(0, 255, 0), 3);
}
imwrite("ImgProcessed.jpg", src);
namedWindow("src", 0);
imshow("src", src);
waitKey();
system("pause");
}
当有其他物体叫人体挡住时,效果不明显