Ask Your Question

Revision history [back]

Actually if you want to crop faces you will first have to execute a face detector and then store each and every found detection in a vector of Mat elements. Below is a code sample that will do what you look for:

Mat img_scene = imread(argv[2], IMREAD_GRAYSCALE);
CascadeClassifier detector("/data/LBP_frontalface.xml"); // this model can be found in the repository
Mat temp;
vector<Rect> detections;
equalizeHist(img_scene, temp);
detector.detectMultiScale(temp, detections, 1.1, 3);

vector<Mat> all_faces;
for(size_t i=0; i < detections.size(); i++){
   Mat croppedFace = img_scene(detections[i]).clone(); // be sure to do this deep clone of the data
   all_faces.push_back(croppedFace);
}

Actually if you want to crop faces you will first have to execute a face detector and then store each and every found detection in a vector of Mat elements. Below is a code sample that will do what you look for:

Mat img_scene = imread(argv[2], IMREAD_GRAYSCALE);
CascadeClassifier detector("/data/LBP_frontalface.xml"); // this model can be found in the repository
detector("/data/LBP_frontalface.xml");
Mat temp;
vector<Rect> detections;
equalizeHist(img_scene, temp);
detector.detectMultiScale(temp, detections, 1.1, 3);

vector<Mat> all_faces;
for(size_t i=0; i < detections.size(); i++){
   Mat croppedFace = img_scene(detections[i]).clone(); // be sure to do this deep clone of the data
img_scene(detections[i]).clone();
   all_faces.push_back(croppedFace);
}