class="cpp" name="code">#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/objdetect.hpp>
#include<iostream>
using namespace std;
using namespace cv;
int main(int argc, char **argv)
{
Mat img;
//读取图片
img = imread("D:\\b.png", 1);
//HOG特征检测器
HOGDescriptor defaultHog;
//设置SVM分类器
defaultHog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
//矩形框数组
vector<Rect> found, found_filtered;
//对图像进行多尺度检测
defaultHog.detectMultiScale(img, found);
//detectMultiScale(const Mat& image,CV_OUT vector<Rect>& objects,double scaleFactor = 1.1,int minNeighbors = 3,int flags = 0,Size minSize = Size(),Size maxSize = Size());
//待检测图片,被检测物体的矩形框向量组,搜索窗口的比例系数,
//输出矩形个数
cout << "矩形个数:" << found.size() << endl;
// 找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中
for (int i = 0; i < found.size(); i++)
{
Rect r = found[i];
int j = 0;
for (; 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[i];
rectangle(img, r.tl(), r.br(), Scalar(0, 255, 0), 3);
}
namedWindow("Detect pedestrain", WINDOW_AUTOSIZE);
imshow("Detect pedestrain", img);
waitKey(0);
return 0;
}
???????? 小编把行人检测1的代码运行后的结果给导师看,导师说要解决这个问题,需要合并窗口。小编并不知道合并窗口是什么意思,所以上网搜了搜,找到了一段代码插了进去,发现矩形框多出的问题解决了。以上是小编新写的代码,对比前一篇,只是多了一段代码。
???????? 小编又定义了一个新的矩形框,用for循环,找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的话,则取外面最大的那个矩形框放入found_filtered中。然后在画矩形框的时候,画出外面最大的那个矩形框。
??????? 结果如下图所示,刚好两个框。
???????? 
?
?
?