Skip to content

Commit

Permalink
added example_22-01
Browse files Browse the repository at this point in the history
  • Loading branch information
pkhaustov committed Jun 28, 2017
1 parent 2538aac commit e346a93
Show file tree
Hide file tree
Showing 3 changed files with 1,600 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ add_executable( example_19-04 example_19-04.cpp )
add_executable( example_20-01 example_20-01.cpp )
add_executable( example_20-02 example_20-02.cpp )
add_executable( example_21-01 example_21-01.cpp )
add_executable( example_22-01 example_22-01.cpp )

# NOW COMPILE SOME ANSWERS TO EXERCISES:
add_executable( Exercises_5 Exercises_5.cpp )
Expand Down Expand Up @@ -154,6 +155,7 @@ target_link_libraries( example_19-04 ${OpenCV_LIBS} )
target_link_libraries( example_20-01 ${OpenCV_LIBS} )
target_link_libraries( example_20-02 ${OpenCV_LIBS} )
target_link_libraries( example_21-01 ${OpenCV_LIBS} )
target_link_libraries( example_22-01 ${OpenCV_LIBS} )

# NOW LINK EXERCISES
target_link_libraries( Exercises_5 ${OpenCV_LIBS} )
Expand Down
93 changes: 93 additions & 0 deletions example_22-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Example 22-1. Detecting and drawing faces

#include <vector>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <opencv2/opencv.hpp>
#include "opencv2/objdetect.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using std::cout;
using std::cerr;
using std::vector;
using std::string;

// Detect and draw detected object boxes on image
//
void detectAndDraw(
cv::Mat& img, // input image
cv::Ptr<cv::CascadeClassifier> classifier, // preloaded classifier
double scale = 1.3) { // resize image by ...
// Just some pretty colors to draw with
//
enum { BLUE, AQUA, CYAN, GREEN };
static cv::Scalar colors[] = {
cv::Scalar(0, 0, 255),
cv::Scalar(0, 128, 255),
cv::Scalar(0, 255, 255),
cv::Scalar(0, 255, 0)
};
// Image preparation:
//
cv::Mat gray(img.size(), CV_8UC1);
cv::Mat small_img(cvSize(cvRound(img.cols / scale),
cvRound(img.rows / scale)), CV_8UC1);
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::resize(gray, small_img, small_img.size(), 0.0, 0.0, cv::INTER_LINEAR);
cv::equalizeHist(small_img, small_img);
// Detect objects if any
//
vector<cv::Rect> objects;
classifier->detectMultiScale(
small_img, // input image
objects, // place for the results
1.1, // scale factor
3, // minimum number of neighbors
CV_HAAR_DO_CANNY_PRUNING, // (old format cascades only)
cv::Size(30, 30)); // throw away detections smaller than this

// Loop through to found objects and draw boxes around them
//

int i = 0;
for (vector<cv::Rect>::iterator r = objects.begin();
r != objects.end(); r++, ++i) {
cv::Rect r_ = (*r);
r_.x *= scale;
r_.y *= scale;
r_.width *= scale;
r_.height *= scale;
cv::rectangle(img, r_, colors[i % 4]);
}
}

int main(int argc, char** argv) {
// Program expects at least two arguments:
// - path to image file
// - path to .xml classifier file
//
if (argc < 3) {
cerr << "Error: wrong number of arguments.\n";
cerr << "Use:\n" << argv[0] << " <image_file> <xml_classifier_file>\n"
<< "to run this demo\n\n"
<< "Example:\n"
<< argv[0] << " faces.png haarcascade_frontalface_alt.xml\n"
<< std::endl;
exit(1);
}

string image_file_name = string(argv[1]);
cv::Mat img = cv::imread(image_file_name, CV_LOAD_IMAGE_COLOR);

string cascade_file_name = string(argv[2]);
cv::Ptr<cv::CascadeClassifier> cascade(new cv::CascadeClassifier(cascade_file_name));

detectAndDraw(img, cascade);

cv::imshow("Result", img);
cv::waitKey(0);

return 0;
}
Loading

0 comments on commit e346a93

Please sign in to comment.