3 #include <opencv2/objdetect.hpp>
4 #include <opencv2/highgui.hpp>
5 #include <opencv2/imgproc.hpp>
6 #include <opencv2/imgcodecs.hpp>
7 #include <opencv2/video.hpp>
8 #include <opencv2/videoio.hpp>
16 "{ help h | | print help message }"
17 "{ image i | | specify input image}"
18 "{ camera c | | enable camera capturing }"
19 "{ video v | ../data/vtest.avi | use video as input }"
20 "{ directory d | | images directory}"
23 static void detectAndDraw(const HOGDescriptor &hog, Mat &img)
25 vector<Rect> found, found_filtered;
26 double t = (double) getTickCount();
27 // Run the detector with default parameters. to get a higher hit-rate
28 // (and more false alarms, respectively), decrease the hitThreshold and
29 // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
30 hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
31 t = (double) getTickCount() - t;
32 cout << "detection time = " << (t*1000./cv::getTickFrequency()) << " ms" << endl;
34 for(size_t i = 0; i < found.size(); i++ )
39 // Do not add small detections inside a bigger detection.
40 for ( j = 0; j < found.size(); j++ )
41 if ( j != i && (r & found[j]) == r )
44 if ( j == found.size() )
45 found_filtered.push_back(r);
48 for (size_t i = 0; i < found_filtered.size(); i++)
50 Rect r = found_filtered[i];
52 // The HOG detector returns slightly larger rectangles than the real objects,
53 // so we slightly shrink the rectangles to get a nicer output.
54 r.x += cvRound(r.width*0.1);
55 r.width = cvRound(r.width*0.8);
56 r.y += cvRound(r.height*0.07);
57 r.height = cvRound(r.height*0.8);
58 rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
62 int main(int argc, char** argv)
64 CommandLineParser parser(argc, argv, keys);
66 if (parser.has("help"))
68 cout << "\nThis program demonstrates the use of the HoG descriptor using\n"
69 " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n";
70 parser.printMessage();
71 cout << "During execution:\n\tHit q or ESC key to quit.\n"
72 "\tUsing OpenCV version " << CV_VERSION << "\n"
73 "Note: camera device number must be different from -1.\n" << endl;
78 hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
79 namedWindow("people detector", 1);
81 string pattern_glob = "";
82 string video_filename = "../data/vtest.avi";
84 if (parser.has("directory"))
86 pattern_glob = parser.get<string>("directory");
88 else if (parser.has("image"))
90 pattern_glob = parser.get<string>("image");
92 else if (parser.has("camera"))
94 camera_id = parser.get<int>("camera");
96 else if (parser.has("video"))
98 video_filename = parser.get<string>("video");
101 if (!pattern_glob.empty() || camera_id != -1 || !video_filename.empty())
103 //Read from input image files
104 vector<String> filenames;
105 //Read from video file
109 if (!pattern_glob.empty())
111 String folder(pattern_glob);
112 glob(folder, filenames);
114 else if (camera_id != -1)
120 msg << "can't open camera: " << camera_id;
121 throw runtime_error(msg.str());
126 vc.open(video_filename.c_str());
128 throw runtime_error(string("can't open video file: " + video_filename));
131 vector<String>::const_iterator it_image = filenames.begin();
135 if (!pattern_glob.empty())
137 bool read_image_ok = false;
138 for (; it_image != filenames.end(); ++it_image)
140 cout << "\nRead: " << *it_image << endl;
141 // Read current image
142 frame = imread(*it_image);
147 read_image_ok = true;
152 //No more valid images
155 //Release the image in order to exit the while loop
167 detectAndDraw(hog, frame);
169 imshow("people detector", frame);
170 int c = waitKey( vc.isOpened() ? 30 : 0 ) & 255;
171 if ( c == 'q' || c == 'Q' || c == 27)