I'm trying to run the following code for face detection but there is a problem in
circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
Full code:
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include<highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
IplImage* img = cvLoadImage( "1.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
Mat image=cvarrToMat(img);
CascadeClassifier face_cascade;
face_cascade.load( "haarcascade_frontalface_alt2.xml" );
std::vector<Rect> faces;
face_cascade.detectMultiScale( image, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
// Draw circles on the detected faces
for( int i = 0; i < faces.size(); i++ )
{
Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
circle( image, center,Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360,Scalar( 255, 0, 255 ), 4, 8, 0 );
}
imshow("Detected Face",image);
waitKey(0);
return 0;
}
Please check the return value of
face_cascade.load( "haarcascade_frontalface_alt2.xml")
. Otherwise, nothing happens if the filehaarcascade_frontalface_alt2.xml
is not found.I may not have the same version of opencv as yours, but here is a code based on yours that produces an acceptable output. The path to the include files and
haarcascade_frontalface_alt2.xml
may need to be changed.The code above it compiled by :
Based on this image in the public domain, i got this :