Skip to content

Instantly share code, notes, and snippets.

@ser1zw
Created March 23, 2014 07:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ser1zw/9719848 to your computer and use it in GitHub Desktop.
Save ser1zw/9719848 to your computer and use it in GitHub Desktop.
#include <iostream>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc, char *argv[]) {
cv::String faceCascadeFileName = "./cascade.xml";
cv::String windowName = "Face detection";
cv::String imageFileName = "./lenna.png";
cv::CascadeClassifier faceCascade;
cv::Mat face;
cv::Mat faceGray;
std::vector<cv::Rect> faceRects;
if (!faceCascade.load(faceCascadeFileName)) {
return -1;
}
face = cv::imread(imageFileName, 1);
if (face.empty()) {
return -1;
}
cv::cvtColor(face, faceGray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(faceGray, faceGray);
faceCascade.detectMultiScale(faceGray, faceRects, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (size_t i = 0; i < faceRects.size(); i++) {
cv::Point center(faceRects[i].x + faceRects[i].width / 2, faceRects[i].y + faceRects[i].height / 2);
cv::ellipse(face, center, cv::Size(faceRects[i].width / 2, faceRects[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);
}
cv::imshow(windowName, face);
cv::waitKey();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment