【发布时间】:2016-05-17 03:36:00
【问题描述】:
我想使用 HOG 描述符和 SVM 检测场景中的人,但程序没有检测到场景中的所有当前人:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
vector <Rect> drawBoundingBox (vector<vector<Point> > contours)
{
vector <Rect> listOfRectangles;
for (int i = 0; i< contours.size(); i++)
{
// Get bounding box for contour
Rect roi = boundingRect(contours[i]);
if (roi.area() > 1000)
{
listOfRectangles.push_back(roi);
}
}
//merge rectangles
int size = listOfRectangles.size();
for( int k = 0; k < size; k++ )
{
listOfRectangles.push_back(Rect(listOfRectangles[k]));
}
groupRectangles(listOfRectangles, 1, 0.7);
return listOfRectangles;
}
//! main program
int main(int argc, char* argv[])
{
Mat frame_capture,frame_capture2;
VideoCapture capture("Baseline/PETS2006/input/in%06d.jpg"); // path to input images
VideoCapture capture2("Baseline/PETS2006/groundtruth/gt%06d.png"); // path to groundtruth images
if((!capture.isOpened())&(!capture2.isOpened() ))
{
printf("Could not open video file!!!");
cin.get();
return -1;
}
Mat gray;
//! do detection and tracking
int i=0;
//Hog Descriptor
cv::HOGDescriptor hog;
hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
while (1)
{ ++i;
capture >> frame_capture;
capture2 >> frame_capture2;
if(i >= 300){
cv::cvtColor(frame_capture2,gray, CV_RGB2GRAY);
cv::Mat im_gray;
cv::equalizeHist( gray, im_gray );
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(gray,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector <Rect> listOfRectangles;
listOfRectangles = drawBoundingBox(contours);
// HOG person detector
vector <Rect> detected_person;
//Hog detectMultiscale
hog.detectMultiScale(im_gray, detected_person, 0.0, cv::Size(8, 8), cv::Size(0,0), 1.05, 4);
if(!frame_capture.empty()){
for (int i =0; i<detected_person.size();++i)
{
//rectangle (frame_capture, listOfRectangles[i], Scalar(255,255,0),1,8,0); //! display detections
//detected persons
rectangle(frame_capture, detected_person[i], Scalar(255,255,0),2,8,0);
//putText(frame_capture,"person", detected_person[i].tl(),FONT_HERSHEY_COMPLEX_SMALL,0.8,cvScalar(200,200,250),1,CV_AA);
}
//oVideoWriter.write(frame_capture);
imshow("video",frame_capture);
waitKey(25);
}
}
}
destroyAllWindows();
return 0;
}
【问题讨论】:
-
在背景中添加更多光线。
-
如果后面的人太小(最小检测器尺寸是人高约为 110 像素),您可以尝试调整图像大小(增加图像大小)
-
我使用的是 720*576 图片。
-
放人不填满整个图像大小。 PERSON(不是图像)的最小像素高度应该是 110 像素左右(我没有在你的图像中测量它)
-
不需要正好是 110,也可以更大。如果将整个图像的大小调整 1.5 倍,您也会检测到第二个人(右上角)。我没有设法检测到第三个,因为我说过你应该为场景添加更多的光(可能不可能,但无论如何你不应该假设完美的结果)。欢迎来到视频检测的世界。