Example #1
0
void InitUtils::initDetectors(CascadeClassifier &faceCascade, CascadeClassifier &eyeCascade1, CascadeClassifier &eyeCascade2, char *faceCascadeFilename, char *eyeCascadeFilename1, char *eyeCascadeFilename2)
{
	// Load the Face Detection cascade classifier xml file.
	try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
		faceCascade.load(faceCascadeFilename);
	} catch (cv::Exception &e) {}
	if ( faceCascade.empty() ) {
		AfxMessageBox("ERROR: Could not load Face Detection cascade classifier");
		return;
	}

	// Load the Eye Detection cascade classifier xml file.
	try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
		eyeCascade1.load(eyeCascadeFilename1);
	} catch (cv::Exception &e) {}
	if ( eyeCascade1.empty() ) {
		AfxMessageBox("ERROR: Could not load 1st Eye Detection cascade classifier");
		return;
	}

	// Load the Eye Detection cascade classifier xml file.
	try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
		eyeCascade2.load(eyeCascadeFilename2);
	} catch (cv::Exception &e) {}
	if ( eyeCascade2.empty() ) {
		AfxMessageBox("ERROR: Could not load 2st Eye Detection cascade classifier");
		return;
	}
}
Example #2
0
bool loocvTest(const char* trained_model_fname,int model_type,const char* fname,int label)
{
	if(face_cascade.empty())
		initFaceCascade();

	Ptr<FaceRecognizer> model=g_trainer.load(trained_model_fname,model_type);

	if(model.empty())
	{
		printf("Fail to load trained model, please re-train the model\n");
		return false;
	}



	Mat frame=imread(fname,CV_LOAD_IMAGE_GRAYSCALE);
	if(frame.empty())
	{
		printf("Fail to load loocv file\n");
		return false;
	}

	int predictLabel=-1;
	double confidence=0.;
	predictLabel=model->predict(frame);

	string class_name=g_trainer.label(predictLabel);
	printf("predict: %s\n",class_name.c_str());

	return (predictLabel==label);
}
Example #3
0
// Function main
int main(int argc, const char** argv )
{
    // Load the cascade
    CascadeClassifier faceDetector;
    string inputimg = argv[1];
    string outputimg = argv[2];

    try {
            faceDetector.load(faceCascadeFilename);
    } catch (cv::Exception e) {}
        if ( faceDetector.empty() ) {
            cerr << "ERROR: Couldn't load Face Detector (";
            cerr << faceCascadeFilename << ")!" << endl;
            exit(1);
    }

    // Read the image file
    Mat img = imread(inputimg , CV_LOAD_IMAGE_COLOR);
    //imshow("Original", img);

    // Apply the classifier to the frame
    if (!img.empty()){
      detectAndDisplay(img, faceDetector, outputimg);
    }
    else{
        printf(" --(!) No captured frame -- Break!");
    }

    waitKey(0);
    return 0;
}
void detectAndDraw( Mat& img,
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale)
{
    int i = 0;
    double t = 0;
    vector<Rect> faces;
    const static Scalar colors[] =  { CV_RGB(255,0,0),
//        CV_RGB(0,128,255),
//        CV_RGB(0,255,255),
//        CV_RGB(0,255,0),
//        CV_RGB(255,128,0),
//        CV_RGB(255,255,0),
//        CV_RGB(255,0,0),
//        CV_RGB(255,0,255)
} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;
        center.x = cvRound((r->x + r->width*0.5)*scale);
        center.y = cvRound((r->y + r->height*0.5)*scale);
        radius = cvRound((r->width + r->height)*0.25*scale);
        circle( img, center, radius, color, 3, 8, 0 );
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        }
    }  
    cv::imshow( "result", img );    
}
Example #5
0
void doCapture(const char* trained_model_fname,int model_type)
{
	if(doing_capture) return;	

	if(face_cascade.empty())
		initFaceCascade();

	Ptr<FaceRecognizer> model=g_trainer.load(trained_model_fname,model_type);

	if(model.empty())
	{
		printf("Fail to load trained model, please re-train the model\n");
		return;
	}

	frame_cnt=0;
	doing_capture=true;
	printf("Start capturing, press Q to stop\n");

	CvCapture* capture=0;
	Mat frame;
	
	capture = cvCaptureFromCAM( 0 );
	if( capture )
	{
		while( true )
		{
			//double t = (double)cvGetTickCount();
			frame = cvQueryFrame( capture );
			
			if( !frame.empty() )
			{ 
				detectAndRecognize(frame,model);
			}
			else
			{ printf(" --(!) No captured frame -- Break!"); break; }

			//t = (double)cvGetTickCount() - t;printf( "time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );

			int c = waitKey(10);
			if( (char)c == 'q' ) { break; }
			
			++frame_cnt;
		}
		cvReleaseCapture(&capture);
	}
	else
	{
		printf("Fail to access the Camera\n");
	}
	cvDestroyWindow(CAPTURE_WND_NAME);

	doing_capture=false;
}
static CascadeClassifier *
gst_face_blur_load_profile (GstFaceBlur * filter, gchar * profile)
{
  CascadeClassifier *cascade;

  cascade = new CascadeClassifier (profile);
  if (cascade->empty ()) {
    GST_ERROR_OBJECT (filter, "Invalid profile file: %s", profile);
    delete cascade;
    return NULL;
  }
  return cascade;
}
static void loadFaceDetector( CascadeClassifier &faceDetector, QString path )
{
    try {
        faceDetector.load( path.toLocal8Bit().data() );
    } catch( cv::Exception e ) {}


    if( faceDetector.empty() ) {
        std::cerr<< "ERROR:counldn't load face detector";
        exit(1);
    }

}
Example #8
0
static int
kms_face_detect_init_cascade()
{
  if (!cascade.load(HAAR_CONF_FILE))
    {
      fprintf(stderr,"Error charging cascade");
      return -1;
    }

  if (cascade.empty())
    fprintf(stderr,"****CASCADE IS EMPTY***********");

  return 0;
}
Example #9
0
// Load the face and 1 or 2 eye detection XML classifiers.
void initDetectors(CascadeClassifier &faceCascade,
                   CascadeClassifier &eyeCascade1, CascadeClassifier &eyeCascade2)
{
    // Load the Face Detection cascade classifier xml file.
    try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
        faceCascade.load(faceCascadeFilename);
    } catch (cv::Exception &e) {}
    if ( faceCascade.empty() ) {
        cerr << "ERROR: Could not load Face Detection cascade classifier [" << faceCascadeFilename << "]!" << endl;
        cerr << "Copy the file from your OpenCV data folder (eg: '/usr/local/opencv/share/lbpcascades') into this WebcamFaceRec folder." << endl;
        exit(1);
    }
    cout << "Loaded the Face Detection cascade classifier [" << faceCascadeFilename << "]." << endl;

    // Load the Eye Detection cascade classifier xml file.
    try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
        eyeCascade1.load(eyeCascadeFilename1);
    } catch (cv::Exception &e) {}
    if ( eyeCascade1.empty() ) {
        cerr << "ERROR: Could not load 1st Eye Detection cascade classifier [" << eyeCascadeFilename1 << "]!" << endl;
        cerr << "Copy the file from your OpenCV data folder (eg: '/usr/local/opencv/share/haarcascades') into this WebcamFaceRec folder." << endl;
        exit(1);
    }
    cout << "Loaded the 1st Eye Detection cascade classifier [" << eyeCascadeFilename1 << "]." << endl;

    // Load the Eye Detection cascade classifier xml file.
    try {   // Surround the OpenCV call by a try/catch block so we can give a useful error message!
        eyeCascade2.load(eyeCascadeFilename2);
    } catch (cv::Exception &e) {}
    if ( eyeCascade2.empty() ) {
        cerr << "Could not load 2nd Eye Detection cascade classifier [" << eyeCascadeFilename2 << "]." << endl;
        // Dont exit if the 2nd eye detector did not load, because we have the 1st eye detector at least.
        //exit(1);
    }
    else
        cout << "Loaded the 2nd Eye Detection cascade classifier [" << eyeCascadeFilename2 << "]." << endl;
}
JNIEXPORT jboolean JNICALL Java_org_opencv_objdetect_CascadeClassifier_empty_10
  (JNIEnv* env, jclass , jlong self)
{
    static const char method_name[] = "objdetect::empty_10()";
    try {
        LOGD("%s", method_name);
        CascadeClassifier* me = (CascadeClassifier*) self; //TODO: check for NULL
        bool _retval_ = me->empty(  );
        return _retval_;
    } catch(const std::exception &e) {
        throwJavaException(env, &e, method_name);
    } catch (...) {
        throwJavaException(env, 0, method_name);
    }
    return 0;
}
Example #11
0
int main (int argc, char ** argv)
{
    // read params from stdin    
    param par = zpracujParametry(argc, argv);

    // load LP cascade detector (.xml file)
    CascadeClassifier cascade;
    cascade.load(par.detector);
    if (cascade.empty()){
        cout << "Could not load cascade classifier" << endl;
        exit(1);
    }

    // process image or dataset of images
    if (!par.inputList.empty()) 
        processList(par, cascade);
   
    return 0;
}
/*
 * In this function it is possible to initialize the variables.
 * For example, we set edge_value to 125 and the filter type to
 * edge filter. This values can be changed via set_properties
 */
static void
kms_eye_detect_init (KmsEyeDetect *
		     eye_detect)
{
  int ret=0;
  eye_detect->priv = KMS_EYE_DETECT_GET_PRIVATE (eye_detect);
  eye_detect->priv->img_width = 320;
  eye_detect->priv->img_height = 240;
  eye_detect->priv->img_orig = NULL;
  eye_detect->priv->scale_o2f=1.0;
  eye_detect->priv->scale_o2e=1.0;
  eye_detect->priv->view_eyes=0;
  eye_detect->priv->events_queue= g_queue_new();
  eye_detect->priv->detect_event=0;
  eye_detect->priv->meta_data=0;
  eye_detect->priv->faces= new vector<Rect>;
  eye_detect->priv->eyes_l= new vector<Rect>;
  eye_detect->priv->eyes_r= new vector<Rect>;
  eye_detect->priv->num_frames_to_process=0;
  eye_detect->priv->frames_with_no_detection_el=0;
  eye_detect->priv->frames_with_no_detection_er=0;

  eye_detect->priv->process_x_every_4_frames=PROCESS_ALL_FRAMES;
  eye_detect->priv->num_frame=0;
  eye_detect->priv->scale_factor=DEFAULT_SCALE_FACTOR;
  eye_detect->priv->width_to_process=EYE_WIDTH;
  eye_detect->priv->server_events =SERVER_EVENTS;
  eye_detect->priv->events_ms =EVENTS_MS;


  if (fcascade.empty())
    if (kms_eye_detect_init_cascade() < 0)
      std::cout << "Error: init cascade" << std::endl;

  if (ret != 0)
    GST_DEBUG ("Error reading the haar cascade configuration file");

  g_rec_mutex_init(&eye_detect->priv->mutex);

}
Example #13
0
/*
 * In this function it is possible to initialize the variables.
 * For example, we set edge_value to 125 and the filter type to
 * edge filter. This values can be changed via set_properties
 */
static void
kms_face_detect_init (KmsFaceDetect *
		      face_detect)
{
  int ret=0;
  face_detect->priv = KMS_FACE_DETECT_GET_PRIVATE (face_detect);
  face_detect->priv->scale=1.0;
  face_detect->priv->img_width = DEFAULT_WIDTH;
  face_detect->priv->img_height = DEFAULT_HEIGHT;
  face_detect->priv->img_orig = NULL;
  face_detect->priv->events_queue= g_queue_new();
  face_detect->priv->detect_event=0;
  face_detect->priv->meta_data=0;
  face_detect->priv->faces_detected= new vector<Rect>;
  face_detect->priv->num_frames_to_process=0;

  face_detect->priv->process_x_every_4_frames=PROCESS_ALL_FRAMES;
  face_detect->priv->num_frame=0;
  face_detect->priv->width_to_process=DEFAULT_WIDTH;
  face_detect->priv->scale_factor=DEFAULT_SCALE_FACTOR;

  face_detect->priv->cv_mem_storage=cvCreateMemStorage(0);
  face_detect->priv->face_seq =cvCreateSeq (0, sizeof (CvSeq), sizeof (CvRect),
					    face_detect->priv->cv_mem_storage);
  face_detect->priv->show_faces = 0;

  if (cascade.empty())
    if (kms_face_detect_init_cascade() < 0)
      std::cout << "Error: init cascade" << std::endl;

  face_detect->priv->cascade = & cascade;


  if (ret != 0)
    GST_DEBUG ("Error reading the haar cascade configuration file");

  g_rec_mutex_init(&face_detect->priv->mutex);
	
}
Example #14
0
/*
 * In this function it is possible to initialize the variables.
 * For example, we set edge_value to 125 and the filter type to
 * edge filter. This values can be changed via set_properties
 */
static void
kms_mouth_detect_init (KmsMouthDetect *
		       mouth_detect)
{
  std::cout << "En mouth detect init" << std::endl;
  int ret=0;
  mouth_detect->priv = KMS_MOUTH_DETECT_GET_PRIVATE (mouth_detect);
  mouth_detect->priv->scale_f2m=1.0;
  mouth_detect->priv->scale_m2o=1.0;
  mouth_detect->priv->scale_o2f=1.0;
  mouth_detect->priv->img_width = 320;
  mouth_detect->priv->img_height = 240;
  mouth_detect->priv->img_orig = NULL;
  mouth_detect->priv->view_mouths=0;
  mouth_detect->priv->events_queue= g_queue_new();
  mouth_detect->priv->detect_event=0;
  mouth_detect->priv->meta_data=0;
  mouth_detect->priv->faces= new vector<Rect>;
  mouth_detect->priv->mouths= new vector<Rect>;
  mouth_detect->priv->num_frames_to_process=0;

  mouth_detect->priv->process_x_every_4_frames=PROCESS_ALL_FRAMES;
  mouth_detect->priv->num_frame=0;
  mouth_detect->priv->scale_factor=DEFAULT_SCALE_FACTOR;
  mouth_detect->priv->width_to_process=MOUTH_WIDTH;

  if (fcascade.empty())
    if (kms_mouth_detect_init_cascade() < 0)
      std::cout << "Error: init cascade" << std::endl;

  if (ret != 0)
    GST_DEBUG ("Error reading the haar cascade configuration file");

  g_rec_mutex_init(&mouth_detect->priv->mutex);

  std::cout << "En mouth detect init END" << std::endl;
}
/*
 * In this function it is possible to initialize the variables.
 * For example, we set edge_value to 125 and the filter type to
 * edge filter. This values can be changed via set_properties
 */
static void
kms_ear_detect_init (KmsEarDetect *
		     ear_detect)
{
  int ret=0;
  ear_detect->priv = KMS_EAR_DETECT_GET_PRIVATE (ear_detect);
  ear_detect->priv->scale_f2e=1.0;
  ear_detect->priv->scale_e2o=1.0;
  ear_detect->priv->scale_f2o=1.0;
  ear_detect->priv->img_width = 320;
  ear_detect->priv->img_height = 240;
  ear_detect->priv->img_orig = NULL;
  ear_detect->priv->view_ears=-1;
  ear_detect->priv->show_faces=1;
  ear_detect->priv->discard=1;
  ear_detect->priv->faces= new vector<Rect>;
  ear_detect->priv->ears= new vector<Rect>;
  ear_detect->priv->rear= new vector<Rect>;
  ear_detect->priv->lear= new vector<Rect>;

  ear_detect->priv->num_frames_to_process=0;
  ear_detect->priv->process_x_every_4_frames=PROCESS_ALL_FRAMES;
  ear_detect->priv->num_frame=0;
  ear_detect->priv->scale_factor=DEFAULT_SCALE_FACTOR;
  ear_detect->priv->width_to_process=EAR_WIDTH;
  ear_detect->priv->frames_with_no_detection=0;

  if (fcascade.empty())
    if (kms_ear_detect_init_cascade() < 0)      
      GST_DEBUG("Error: init cascade");
  
  if (ret != 0)
    GST_DEBUG ("Error reading the haar cascade configuration file");
  
  g_rec_mutex_init(&ear_detect->priv->mutex);
  
}
Example #16
0
bool detectAndNormalize(Mat frame,Mat& res,int action=0)
{
	if(face_cascade.empty())
		initFaceCascade();

	res=frame;

	if( !frame.empty() )
	{
		std::vector<Rect> faces;
		Mat frame_gray;

		cvtColor( frame, frame_gray, CV_BGR2GRAY );
		//equalizeHist( frame_gray, frame_gray );

		face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
		if(faces.size()==0)
		{
			return false;
		}

		// first face
		Mat faceROI = frame( faces[0] ); // rgb
		cv::resize(faceROI,faceROI,cv::Size(NORM_IMG_WIDTH,NORM_IMG_HEIGHT));

		res=faceROI;

		if(action>=ENorm_Gray)
			cvtColor( res, res, CV_BGR2GRAY );
		if(action>ENorm_Equalize)
			equalizeHist( res, res );

		return true;
	}
	return false;
}
Example #17
0
/*
 * In this function it is possible to initialize the variables.
 * For example, we set edge_value to 125 and the filter type to
 * edge filter. This values can be changed via set_properties
 */
static void
kms_nose_detect_init (KmsNoseDetect *
		      nose_detect)
{
  int ret=0;
  nose_detect->priv = KMS_NOSE_DETECT_GET_PRIVATE (nose_detect);
  nose_detect->priv->img_width = 320;
  nose_detect->priv->img_height = 240;
  nose_detect->priv->img_orig = NULL;
  nose_detect->priv->scale_f2n=1.0;
  nose_detect->priv->scale_n2o=1.0;
  nose_detect->priv->scale_o2f=1.0;
  nose_detect->priv->view_noses=0;
  nose_detect->priv->events_queue= g_queue_new();
  nose_detect->priv->detect_event=0;
  nose_detect->priv->meta_data=0;
  nose_detect->priv->faces= new vector<Rect>;
  nose_detect->priv->noses= new vector<Rect>;
  nose_detect->priv->num_frames_to_process=0;

  nose_detect->priv->process_x_every_4_frames=PROCESS_ALL_FRAMES;
  nose_detect->priv->num_frame=0;
  nose_detect->priv->width_to_process=NOSE_WIDTH;
  nose_detect->priv->scale_factor=DEFAULT_SCALE_FACTOR;

  if (fcascade.empty())
    if (kms_nose_detect_init_cascade() < 0)
      std::cout << "Error: init cascade" << std::endl;


  if (ret != 0)
    GST_DEBUG ("Error reading the haar cascade configuration file");

  g_rec_mutex_init(&nose_detect->priv->mutex);

}
Example #18
0
int main(int argc, char** argv)
{
    OpenNI2Grabber grabber;
    bool isRunning = false;
    const int timeoutMs = 1000;
    //1.0 api version
    Mat frame, frame_copy, image_size;
    vector<Rect> faces;
    vector< vector<Point> > contours;
    cascade.load(cascade_name);


    if( cascade.empty() ) {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }

    // attempt to start the grabber
    if(grabber.initialize())
    {
        if(grabber.start())
        {
            isRunning = grabber.isRunning();
        }
    }
    else
    {
        std::cout << "Unable to initialize OpenNI2Grabber, program terminating!" << std::endl;
        return 0;
    }

    // acquire frames until program termination
    std::cout << "Press 'q' to halt acquisition..." << std::endl;
    Mat depthImage, colorImage;
    Mat depthImageDraw;
    Mat depthImage8U;

    int flag = -1;
    ofstream myfile;
    myfile.open ("direction.txt");

    while(isRunning)
    {
        // wait for a new image frame
        if(grabber.waitForFrame(timeoutMs))
        {
            // update the display for both frames
            if(grabber.getDepthFrame(depthImage) && grabber.getColorFrame(colorImage))
            {
                depthImage.convertTo(depthImageDraw, -1, 32);

                depthImageDraw.convertTo(depthImageDraw, CV_8UC1, 1.0/256.0);
                imshow("8", depthImageDraw);
                threshold(depthImageDraw,depthImageDraw,190, 255,4);
                medianBlur(depthImageDraw,depthImageDraw,5);
                threshold(depthImageDraw,depthImageDraw,95, 255,3);
                imshow("trun2", depthImageDraw);
                findContours(depthImageDraw,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
                drawContours(colorImage,contours,-1,Scalar(0,0,255),2);
                /// Get the moments
                vector<Moments> mu(contours.size() );
                vector<Point2f> mc( contours.size() );
                vector<float> oldX( contours.size() );
                vector<float> oldY( contours.size() );
                for( int i = 0; i < contours.size(); i++ )
                {
                    if(contourArea(contours[i])<5500 && contourArea(contours[i])>1000) {
                        mu[i] = moments( contours[i], false );
                        mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 );
                        circle( colorImage, mc[i], 4, Scalar(0,255,0), -1, 8, 0 );

                        newpoint[1] = mc[i].x;
                        newpoint[2] = mc[i].y;

                        difference[1] = newpoint[1]-oldpoint[1];
                        difference[2] = newpoint[2]-oldpoint[2];

                        if (difference[1] < -diff_threshold_rl)
                        {
                            cout << "RIGHT TO LEFT RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR \n";
                        }
                        else if (difference[1] > diff_threshold_rl)
                        {
                            cout << "LEFT TO RIGHT LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL\n";

                        }
                        else if (difference[2] < -diff_threshold_tb)
                        {
                            cout << "TOP TO BOTTOM TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT\n";

                        }
                        else if (difference[2] > diff_threshold_tb)
                        {
                            cout << "BOTTOM TO TOP BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n";

                        }

                        oldpoint[1] = newpoint[1];
                        oldpoint[2] = newpoint[2];


                    }


                }

                imshow("rgb", colorImage);
            }
        }
        else
        {
            std::cout << "No new frames received in " << timeoutMs << " ms..." << std::endl;
        }

        // check for program termination
        char key = (char) cv::waitKey(1);
        if(key == 'q' || key == 'Q')
        {
            std::cout << "Terminating program..." << std::endl;
            isRunning = false;
        }
        // check for Blob
        char key2 = (char) cv::waitKey(1);
        if(key2 == 'b' || key2 == 'B')
        {
            std::cout << "Checking for blobs" << std::endl;

        }
    }
    // stop the acquisition
    grabber.stop();
    myfile.close();
    return 0;
}
Example #19
0
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
                    CascadeClassifier& nestedCascade,
                    double scale, bool tryflip )
{
    int i = 0;
    double t = 0;
    vector<Rect> faces, faces2;
    CascadeClassifier nosecascade, mouthcascade;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    if( tryflip )
    {
        flip(smallImg, smallImg, 1);
        cascade.detectMultiScale( smallImg, faces2,
                                 1.1, 2, 0
                                 //|CV_HAAR_FIND_BIGGEST_OBJECT
                                 //|CV_HAAR_DO_ROUGH_SEARCH
                                 |CV_HAAR_SCALE_IMAGE
                                 ,
                                 Size(30, 30) );
        for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
        {
            faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
        }
    }
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );

    if( !nosecascade.load( "haarcascade_mcs_mouth.xml" ) )
    {
        cerr << "ERROR: Could not load classifier cascade" << endl;
    }
    if( !nosecascade.load( "haarcascade_mcs_nose.xml" ) )
    {
        cerr << "ERROR: Could not load classifier cascade" << endl;
    }
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects, noseObjects, mouthObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;

        double aspect_ratio = (double)r->width/r->height;
        if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
        {
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            circle( img, center, radius, colors[0], 3, 8, 0 );
        }
        else
            rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
                       cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
                       colors[0], 3, 8, 0);
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, colors[1], 3, 8, 0 );
        }

        nosecascade.detectMultiScale( smallImgROI, noseObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = noseObjects.begin(); nr != noseObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, colors[2], 3, 8, 0 );
        }

        mouthcascade.detectMultiScale( smallImgROI, mouthObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = mouthObjects.begin(); nr != mouthObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, colors[3], 3, 8, 0 );
        }
    }
    cv::imshow( "result", img );
}
Example #20
0
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
				   CascadeClassifier& nestedCascade,
				   double scale, bool tryflip )
{
	int i = 0;
	double t = 0;
	//建立用于存放人脸的向量容器
	vector<Rect> faces, faces2;
	//定义一些颜色,用来标示不同的人脸
	const static Scalar colors[] =  { CV_RGB(0,0,255),
		CV_RGB(0,128,255),
		CV_RGB(0,255,255),
		CV_RGB(0,255,0),
		CV_RGB(255,128,0),
		CV_RGB(255,255,0),
		CV_RGB(255,0,0),
		CV_RGB(255,0,255)} ;
	//建立缩小的图片,加快检测速度
	//nt cvRound (double value) 对一个double型的数进行四舍五入,并返回一个整型数!
	Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
	//转成灰度图像,Harr特征基于灰度图
	cvtColor( img, gray, CV_BGR2GRAY );
	//改变图像大小,使用双线性差值
	resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
	//变换后的图像进行直方图均值化处理
	equalizeHist( smallImg, smallImg );

	//程序开始和结束插入此函数获取时间,经过计算求得算法执行时间
	t = (double)cvGetTickCount();
	//检测人脸
	//detectMultiScale函数中smallImg表示的是要检测的输入图像为smallImg,faces表示检测到的人脸目标序列,1.1表示
	//每次图像尺寸减小的比例为1.1,2表示每一个目标至少要被检测到3次才算是真的目标(因为周围的像素和不同的窗口大
	//小都可以检测到人脸),CV_HAAR_SCALE_IMAGE表示不是缩放分类器来检测,而是缩放图像,Size(30, 30)为目标的
	//最小最大尺寸
	cascade.detectMultiScale( smallImg, faces,
		1.1, 2, 0
		//|CV_HAAR_FIND_BIGGEST_OBJECT
		//|CV_HAAR_DO_ROUGH_SEARCH
		|CV_HAAR_SCALE_IMAGE
		,
		Size(30, 30));
	//如果使能,翻转图像继续检测
	if( tryflip )
	{
		flip(smallImg, smallImg, 1);
		cascade.detectMultiScale( smallImg, faces2,
			1.1, 2, 0
			//|CV_HAAR_FIND_BIGGEST_OBJECT
			//|CV_HAAR_DO_ROUGH_SEARCH
			|CV_HAAR_SCALE_IMAGE
			,
			Size(30, 30) );
		for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
		{
			faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
		}
	}
	t = (double)cvGetTickCount() - t;
	//   qDebug( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
	int xPoint = 0;
	int yPoint = 0;

	for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
	{
		Mat smallImgROI;
		vector<Rect> nestedObjects;
		Point center;
		Scalar color = colors[i%8];
		int radius;

		double aspect_ratio = (double)r->width/r->height;
		if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
		{
			//标示人脸时在缩小之前的图像上标示,所以这里根据缩放比例换算回去
			center.x = cvRound((r->x + r->width*0.5)*scale);
			center.y = cvRound((r->y + r->height*0.5)*scale);
			radius = cvRound((r->width + r->height)*0.25*scale);
			circle( img, center, radius, color, 3, 8, 0 );
		}
		else
			rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
			cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
			color, 3, 8, 0);
		if (r == faces.begin())
		{
			xPoint = 320-(center.x);
			yPoint = 240-(center.y);
		}
		
		if( nestedCascade.empty() )
			continue;
		smallImgROI = smallImg(*r);
		//同样方法检测人眼
		nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
			1.1, 2, 0
			//|CV_HAAR_FIND_BIGGEST_OBJECT
			//|CV_HAAR_DO_ROUGH_SEARCH
			//|CV_HAAR_DO_CANNY_PRUNING
			|CV_HAAR_SCALE_IMAGE
			,
			Size(30, 30) );
		for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
		{
			center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
			center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
			radius = cvRound((nr->width + nr->height)*0.25*scale);
			circle( img, center, radius, color, 3, 8, 0 );
		}
	}
	cv::imshow( "aaa", img );
	callBackFun(xPoint, yPoint);

}
Example #21
0
	void findcars()                 //main function
	{
    	int i = 0;
    	
		Mat img = storage.clone();
		Mat temp;                    
		
		if(img.empty() )
        {
			cout << endl << "detect not successful" << endl;
		}
		int cen_x;                         
		int cen_y;
    	vector<Rect> cars;
    	const static Scalar colors[] =  { CV_RGB(0,0,255),CV_RGB(0,255,0),CV_RGB(255,0,0),CV_RGB(255,255,0),CV_RGB(255,0,255),CV_RGB(0,255,255),CV_RGB(255,255,255),CV_RGB(128,0,0),CV_RGB(0,128,0),CV_RGB(0,0,128),CV_RGB(128,128,128),CV_RGB(0,0,0)};                   
        	
    	Mat gray; 

    	cvtColor( img, gray, CV_BGR2GRAY );

		Mat resize_image(cvRound (img.rows), cvRound(img.cols), CV_8UC1 );		

    	resize( gray, resize_image, resize_image.size(), 0, 0, INTER_LINEAR );
    	equalizeHist( resize_image, resize_image );

    	
    	cascade.detectMultiScale( resize_image, cars,1.1,2,0,Size(10,10));                 
        	
		
		for( vector<Rect>::const_iterator main = cars.begin(); main != cars.end(); main++, i++ )
    	{
       		Mat resize_image_reg_of_interest;
        	vector<Rect> nestedcars;
        	Point center;
        	Scalar color = colors[i%8];
        	
	        	
			//getting points for bouding a rectangle over the car detected by main
			int x0 = cvRound(main->x);                              
			int y0 = cvRound(main->y);
			int x1 = cvRound((main->x + main->width-1));
			int y1 = cvRound((main->y + main->height-1));

		
			
        	if( checkcascade.empty() )
            	continue;
        	resize_image_reg_of_interest = resize_image(*main);
        	checkcascade.detectMultiScale( resize_image_reg_of_interest, nestedcars,1.1,2,0,Size(30,30));
            	
        	for( vector<Rect>::const_iterator sub = nestedcars.begin(); sub != nestedcars.end(); sub++ )      //testing the detected car by main using checkcascade
        	{
           		center.x = cvRound((main->x + sub->x + sub->width*0.5));        //getting center points for bouding a circle over the car detected by checkcascade
				cen_x = center.x;
			   	center.y = cvRound((main->y + sub->y + sub->height*0.5));
				cen_y = center.y;
				if(cen_x>(x0+15) && cen_x<(x1-15) && cen_y>(y0+15) && cen_y<(y1-15))         //if centre of bounding circle is inside the rectangle boundary over a threshold the the car is certified
				{
                								
					rectangle( image_main_result, cvPoint(x0,y0),
                    	   		cvPoint(x1,y1),
                     	  		color, 3, 8, 0);               //detecting boundary rectangle over the final result
					

					
					//masking the detected car to detect second car if present

					Rect region_of_interest = Rect(x0, y0, x1-x0, y1-y0);
					temp = storage(region_of_interest);
					temp = Scalar(255,255,255);

					num = num+1;     //num if number of cars detected
				
				}
			}	
				
		}		
				
                    	   
	if(image_main_result.empty() )
    {
		cout << endl << "result storage not successful" << endl;
	}			
			
    }      
Example #22
0
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
                    CascadeClassifier& nestedCascade,
                    double scale, bool tryflip )
{
    //int cnt=0;
    char file_name[128];
    char dest_path[256];
    int i = 0;
    double t = 0;
    vector<Rect> faces, faces2;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );


    if( tryflip )
    {
        flip(smallImg, smallImg, 1);
        cascade.detectMultiScale( smallImg, faces2,
                                 1.1, 2, 0
                                 //|CV_HAAR_FIND_BIGGEST_OBJECT
                                 //|CV_HAAR_DO_ROUGH_SEARCH
                                 |CV_HAAR_SCALE_IMAGE
                                 ,
                                 Size(30, 30) );
        for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
        {
            faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
        }
    }
    t = (double)cvGetTickCount() - t;
    //printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    int cnt = 0;
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;

        double aspect_ratio = (double)r->width/r->height;
        if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
        {
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
	    printf("%d %d [%d, %d]\n", r->x, r->y, r->width, r->height);
            now_timeget(file_name, cnt);
	    cnt++;
	    cv::Mat cut_img(img, cv::Rect(r->x*scale, r->y*scale, r->width*scale, r->height*scale));
	    sprintf(dest_path, "/var/www/images/");
	    strncat(dest_path, file_name, 256);
            cv::imwrite(dest_path, cut_img);
	    send_imgFileName((char*)file_name); //send to server!!
      }
        else
            rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
                       cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
                       color, 3, 8, 0);
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
	//cv::imwrite("result.jpg", img);
        }
    }
    cv::imshow( "result", img );
}
Example #23
0
// Search for both eyes within the given face image. Returns the eye centers in 'leftEye' and 'rightEye',
// or sets them to (-1,-1) if each eye was not found. Note that you can pass a 2nd eyeCascade if you
// want to search eyes using 2 different cascades. For example, you could use a regular eye detector
// as well as an eyeglasses detector, or a left eye detector as well as a right eye detector.
// Or if you don't want a 2nd eye detection, just pass an uninitialized CascadeClassifier.
// Can also store the searched left & right eye regions if desired.
void detectBothEyes(const Mat &face, CascadeClassifier &eyeCascade1, CascadeClassifier &eyeCascade2, Point &leftEye, Point &rightEye, Rect *searchedLeftEye, Rect *searchedRightEye)
{
    // Skip the borders of the face, since it is usually just hair and ears, that we don't care about.
/*
    // For "2splits.xml": Finds both eyes in roughly 60% of detected faces, also detects closed eyes.
    const float EYE_SX = 0.12f;
    const float EYE_SY = 0.17f;
    const float EYE_SW = 0.37f;
    const float EYE_SH = 0.36f;
*/
/*
    // For mcs.xml: Finds both eyes in roughly 80% of detected faces, also detects closed eyes.
    const float EYE_SX = 0.10f;
    const float EYE_SY = 0.19f;
    const float EYE_SW = 0.40f;
    const float EYE_SH = 0.36f;
*/

    // For default eye.xml or eyeglasses.xml: Finds both eyes in roughly 40% of detected faces, but does not detect closed eyes.
    const float EYE_SX = 0.16f;
    const float EYE_SY = 0.26f;
    const float EYE_SW = 0.30f;
    const float EYE_SH = 0.28f;

    int leftX = cvRound(face.cols * EYE_SX);
    int topY = cvRound(face.rows * EYE_SY);
    int widthX = cvRound(face.cols * EYE_SW);
    int heightY = cvRound(face.rows * EYE_SH);
    int rightX = cvRound(face.cols * (1.0-EYE_SX-EYE_SW) );  // Start of right-eye corner

    Mat topLeftOfFace = face(Rect(leftX, topY, widthX, heightY));
    Mat topRightOfFace = face(Rect(rightX, topY, widthX, heightY));
    Rect leftEyeRect, rightEyeRect;

    // Return the search windows to the caller, if desired.
    if (searchedLeftEye)
        *searchedLeftEye = Rect(leftX, topY, widthX, heightY);
    if (searchedRightEye)
        *searchedRightEye = Rect(rightX, topY, widthX, heightY);

    // Search the left region, then the right region using the 1st eye detector.
    detectLargestObject(topLeftOfFace, eyeCascade1, leftEyeRect, topLeftOfFace.cols);
    detectLargestObject(topRightOfFace, eyeCascade1, rightEyeRect, topRightOfFace.cols);

    // If the eye was not detected, try a different cascade classifier.
    if (leftEyeRect.width <= 0 && !eyeCascade2.empty()) {
        detectLargestObject(topLeftOfFace, eyeCascade2, leftEyeRect, topLeftOfFace.cols);
        //if (leftEyeRect.width > 0)
        //    cout << "2nd eye detector LEFT SUCCESS" << endl;
        //else
        //    cout << "2nd eye detector LEFT failed" << endl;
    }
    //else
    //    cout << "1st eye detector LEFT SUCCESS" << endl;

    // If the eye was not detected, try a different cascade classifier.
    if (rightEyeRect.width <= 0 && !eyeCascade2.empty()) {
        detectLargestObject(topRightOfFace, eyeCascade2, rightEyeRect, topRightOfFace.cols);
        //if (rightEyeRect.width > 0)
        //    cout << "2nd eye detector RIGHT SUCCESS" << endl;
        //else
        //    cout << "2nd eye detector RIGHT failed" << endl;
    }
    //else
    //    cout << "1st eye detector RIGHT SUCCESS" << endl;

    if (leftEyeRect.width > 0) {   // Check if the eye was detected.
        leftEyeRect.x += leftX;    // Adjust the left-eye rectangle because the face border was removed.
        leftEyeRect.y += topY;
        leftEye = Point(leftEyeRect.x + leftEyeRect.width/2, leftEyeRect.y + leftEyeRect.height/2);
    }
    else {
        leftEye = Point(-1, -1);    // Return an invalid point
    }

    if (rightEyeRect.width > 0) { // Check if the eye was detected.
        rightEyeRect.x += rightX; // Adjust the right-eye rectangle, since it starts on the right side of the image.
        rightEyeRect.y += topY;  // Adjust the right-eye rectangle because the face border was removed.
        rightEye = Point(rightEyeRect.x + rightEyeRect.width/2, rightEyeRect.y + rightEyeRect.height/2);
    }
    else {
        rightEye = Point(-1, -1);    // Return an invalid point
    }
}
Example #24
0
void detect(Mat& img, CascadeClassifier& cascade,
    CascadeClassifier& nestedCascade, double scale) {
  int i = 0;
  double t = 0;
  vector<Rect> faces;
  Mat gray, smallImg;

  cvtColor(img, gray, CV_BGR2GRAY);
  resize(gray, smallImg, Size(), 1 / scale, 1 / scale, INTER_LINEAR);
  equalizeHist(smallImg, smallImg);

  t = (double) cvGetTickCount();
  cascade.detectMultiScale(smallImg, faces, 1.1, 2, 0
  //|CV_HAAR_FIND_BIGGEST_OBJECT
  //|CV_HAAR_DO_ROUGH_SEARCH
      | CV_HAAR_SCALE_IMAGE, Size(30, 30));
  t = (double) cvGetTickCount() - t;
//  printf("detection time = %g ms\n",
//      t / ((double) cvGetTickFrequency() * 1000.));

  // Bounding boxes of all eyes found this frame, in full-size camera space
  vector<Rect> allEyes;
  for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end();
      r++, i++) {
    Mat smallImgROI;
    vector<Rect> nestedObjects;

    if (nestedCascade.empty())
      continue;
    smallImgROI = smallImg(*r);
    nestedCascade.detectMultiScale(smallImgROI, nestedObjects, 1.1, 2, 0
    //|CV_HAAR_FIND_BIGGEST_OBJECT
    //|CV_HAAR_DO_ROUGH_SEARCH
    //|CV_HAAR_DO_CANNY_PRUNING
        | CV_HAAR_SCALE_IMAGE, Size(30, 30));

    for (vector<Rect>::const_iterator e = nestedObjects.begin();
        e != nestedObjects.end(); ++e) {
      allEyes.push_back((*e + r->tl()) * scale);
    }
  }

  // Find which tracked eyes are closest to the observed eyes this frame
  vector<pair<int, int> > matching = assignTracksToEyes(eyeTracks, allEyes);
  vector<bool> foundTracks(eyeTracks.size());
  vector<bool> foundEyes(allEyes.size());
  for (vector<pair<int, int> >::const_iterator trackToEye = matching.begin();
      trackToEye != matching.end(); ++trackToEye) {
    foundTracks[trackToEye->first] = true;
    foundEyes[trackToEye->second] = true;
    const Rect& eyeSrc = allEyes[trackToEye->second];
    EyeTrack& track = eyeTracks[trackToEye->first];

    record(track, img, eyeSrc);
  }

  // Forget any tracks that haven't been picked up for a while
  for (int i = 0; i < eyeTracks.size(); ++i) {
    if (!eyeTracks[i].lastSeen.empty() && !foundTracks[i]) {
      int framesSinceLastSeen = ++(eyeTracks[i].numFramesSinceLastSeen);
      if (framesSinceLastSeen > 5)
        eyeTracks[i] = EyeTrack();
    }
  }

  // Put any new eyes into a free slot, if possible
  int j = 0;
  for (int i = 0; i < allEyes.size(); ++i) {
    if (!foundEyes[i]) {
      while (j < eyeTracks.size() && !eyeTracks[j].lastSeen.empty())
        j++;
      if (j == eyeTracks.size())
        break;
      record(eyeTracks[j], img, allEyes[i]);
    }
  }

  for (vector<EyeTrack>::iterator t = eyeTracks.begin(); t != eyeTracks.end();
      ++t) {
    if (t->frames.size() >= 10) {
      t->frames.erase(t->frames.begin(), t->frames.end() - 10);
      t->lastSeen.erase(t->lastSeen.begin(), t->lastSeen.end() - 10);
    }
  }

  i = 0;
  for (vector<Rect>::const_iterator r = faces.begin(); r != faces.end();
      ++r, ++i) {
    Scalar color = colors[i % 8];
    rectangle(img, *r * scale, color, 3);
  }

  i = 0;
  // for(vector<Rect>::const_iterator e = allEyes.begin(); e != allEyes.end(); ++e, ++i) {
  //   const Scalar& color = colors[i % maxEyes];
  //   rectangle(img, *e, color, 3);
  // }
}
Example #25
0
// the actual detection code
void detectAndDraw( Mat& img,
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale)
{
    /* BEGINNING OF CODE WE NEED TO REPLACE */
    // initialize vars
    int i = 0;
    double t = 0;
    vector<Rect> faces;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    // take "img" and convert it to gray-scale: "gray." the rest of the code is processing the new "gray" img
    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    // find the objects in the image that match the haar classifiers, store them in faces
    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    
    /* END OF CODE WE NEED TO REPLACE */
    
    // detection done, faces stored as rects, now print the stuff to the image
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;
        
        // set the vars to values based off of the faces rects
        center.x = cvRound((r->x + r->width*0.5)*scale);
        center.y = cvRound((r->y + r->height*0.5)*scale);
        radius = cvRound((r->width + r->height)*0.25*scale);
        
        // draw the cascade circles
        /* EDITED TO MAKE FULL CIRCLES, RATHER THAN EDGES, FOR COMPARISON */
        circle( img, center, radius, color, -1 /*3*/, 8, 0 );
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
            
        // draw nested cascade circles
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, -1 /*3*/, 8, 0 );
        }
    }
    cv::imshow( "result", img );
}
Example #26
0
void detectAndDraw( Mat& img, // <- aqui eh passado
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale)
{
    int i = 0;
    double t = 0;
    vector<Rect> faces; // <- o que a gnte quer
    char filename[128];
    static int filename_count = 0;
    int radius;

    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
        
    vector<int> compression_params;
    compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
    compression_params.push_back(9);
 
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, CV_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    //aqui detecta e coloca no faces, que é um array de retangulos. yep
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        //|CV_HAAR_FIND_BIGGEST_OBJECT
        //|CV_HAAR_DO_ROUGH_SEARCH
        |CV_HAAR_SCALE_IMAGE
        ,
        Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for(vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        
        center.x = cvRound((r->x + r->width*0.5)*scale);
        center.y = cvRound((r->y + r->height*0.5)*scale);
        radius = cvRound((r->width + r->height)*0.25*scale);
        
        if (radius != 4203968) {
          sprintf(filename, "%d.png", ++filename_count);
          printf("filename: %s\n", filename);
          cv::imwrite(filename, img, compression_params);

        }
        
        circle( img, center, radius, color, 3, 8, 0 );

        if( nestedCascade.empty() )
            continue;

        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CV_HAAR_FIND_BIGGEST_OBJECT
            //|CV_HAAR_DO_ROUGH_SEARCH
            //|CV_HAAR_DO_CANNY_PRUNING
            |CV_HAAR_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        } 
        
    }
    cv::imshow( "result", img );
}
Example #27
0
void detectAndDraw( Mat& img, IplImage* image,
                   CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
                   double scale, IplImage* motion)
{
    int i = 0;
    double t = 0;
    vector<Rect> faces;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );
	
    cvtColor( img, gray, CV_BGR2GRAY );
	
	
	
    static vector<Rect> old_faces;
    static int occ = 0;
	
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );
	
    update_mhi(image, motion, 30);
	
	
    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
							 1.1, 2, 0
							 //|CV_HAAR_FIND_BIGGEST_OBJECT
							 |CV_HAAR_DO_ROUGH_SEARCH
							 //|CV_HAAR_SCALE_IMAGE
							 ,
							 Size(30, 30) );
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
	
    if (faces.empty())
    {
		if (occ++ == 20)
		{
			occ = 0;
			old_faces.clear();
		}
		else
			faces = old_faces;
    }
    else
		old_faces = faces;
	
	if (faces.size() == 0)
	{
		std::stringstream messagecpp;
		std::string str_noob;
		
		messagecpp << "up:1:" << 0 << ":" << 0;
		str_noob = messagecpp.str();
		const char *message_to_send = str_noob.c_str();
		
		// server send and rcv!!
		
		send(conn, message_to_send, strlen(message_to_send) + 1, MSG_OOB);
		
		memset(&message, 0, MSG_SIZE);
		
		size_recv = recv(conn, message, MSG_SIZE, MSG_PEEK);
		if ( size_recv == -1)
		{
			perror("didn't received message");
			close(sd);
			return;
		}
		else if ( size_recv == 0)
		{
			perror("the peer closed the connexion");
			close(sd);
			return;
		}
		else {
			std::cout << message << std::endl;
		}
	}
	else
    {
		int cpt_loop = 0;
		for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
		{
			Mat smallImgROI;
			vector<Rect> nestedObjects;
			Point center;
			Scalar color = colors[i%8];
			int radius;
			
			std::stringstream messagecpp;
			std::string str_noob;
			cpt_loop++;
			messagecpp << "down:" << cpt_loop << ":" << r->x << ":" << r->y;
			str_noob = messagecpp.str();
			const char *message_to_send = str_noob.c_str();
			//std::cout << str_noob << std::endl;
			//std::cout << message_to_send << std::endl;
			// server send and rcv!!
			
			send(conn, message_to_send, strlen(message_to_send) + 1, MSG_OOB);
			
			
			memset(&message, 0, MSG_SIZE);
			
			size_recv = recv(conn, message, MSG_SIZE, MSG_PEEK);
			if ( size_recv == -1)
			{
				perror("didn't received message");
				close(sd);
				return;
			}
			else if ( size_recv == 0)
			{
				perror("the peer closed the connexion");
				close(sd);
				return;
			}
			else {
				std::cout << message << std::endl;
			}
			
			// fin de la partie server.
			
			center.x = cvRound((r->x + r->width*0.5)*scale);
			center.y = cvRound((r->y + r->height*0.5)*scale);
			radius = cvRound((r->width + r->height)*0.25*scale);
			
			//std::cout << "radius = " << radius << std::endl;
			if (radius > 25)
				continue;
			
			
			circle( img, center, radius, color, 3, 8, 0 );
			if( nestedCascade.empty() )
				continue;
			smallImgROI = smallImg(*r);
			nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
										   1.1, 2, 0
										   //|CV_HAAR_FIND_BIGGEST_OBJECT
										   //|CV_HAAR_DO_ROUGH_SEARCH
										   //|CV_HAAR_DO_CANNY_PRUNING
										   |CV_HAAR_SCALE_IMAGE
										   ,
										   Size(30, 30) );
			for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
			{
				center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
				center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
				radius = cvRound((nr->width + nr->height)*0.25*scale);
				circle( img, center, radius, color, 3, 8, 0 );
			}
		}	
	}


    cv::imshow( "result", img );
    cvShowImage( "Motion", motion );
}
void detectAndDraw( Mat& img,
		CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
		double scale, char* argv[])
{
	int i = 0;
	double t = 0;
	vector<Rect> faces;
	const static Scalar colors[] =  { CV_RGB(0,0,255),
		CV_RGB(0,128,255),
		CV_RGB(0,255,255),
		CV_RGB(0,255,0),
		CV_RGB(255,128,0),
		CV_RGB(255,255,0),
		CV_RGB(255,0,0),
		CV_RGB(255,0,255)} ;
	Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

	cvtColor( img, gray, CV_BGR2GRAY );

	resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
	equalizeHist( smallImg, smallImg );

	t = (double)cvGetTickCount();
	cascade.detectMultiScale( smallImg, faces,
			1.1, 2, 0
			//|CV_HAAR_FIND_BIGGEST_OBJECT
			//|CV_HAAR_DO_ROUGH_SEARCH
			|CV_HAAR_SCALE_IMAGE
			,
			Size(30, 30) );
	t = (double)cvGetTickCount() - t;
	for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
	{
		Mat smallImgROI;
		vector<Rect> nestedObjects;
		Point center;
		Scalar color = colors[i%8];
		int radius;
		center.x = cvRound((r->x + r->width*0.5)*scale);
		center.y = cvRound((r->y + r->height*0.5)*scale);
		radius = cvRound((r->width + r->height)*0.25*scale);
		Rect rect = Rect(center.x - radius , center.y - radius , 2*radius , 2*radius );
		Mat faceImage = gray(rect);
		char s1[100];
		sprintf(s1,"dataset/%s/%s_%d.jpg",argv[1],argv[1],count_face++);
		imwrite(s1,faceImage);
		//imshow( "result", faceImage );
		//circle( img, center, radius, color, 3, 8, 0 );
		rectangle(img, rect, Scalar(255,0,0));
		if( nestedCascade.empty() )
			continue;
		smallImgROI = smallImg(*r);
		nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
				1.1, 2, 0
				//|CV_HAAR_FIND_BIGGEST_OBJECT
				//|CV_HAAR_DO_ROUGH_SEARCH
				//|CV_HAAR_DO_CANNY_PRUNING
				|CV_HAAR_SCALE_IMAGE
				,
				Size(30, 30) );
		for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
		{
			center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
			center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
			radius = cvRound((nr->width + nr->height)*0.25*scale);
			circle( img, center, radius, color, 3, 8, 0 );
			Rect rect1 = Rect(center.x - radius , center.y - radius , 2*radius , 2*radius );
			Mat faceImage1 = gray(rect1);
			char s2[100];
			sprintf(s2,"dataset/%s/%s_%d.jpg",argv[1],argv[1],count_face++);
			imwrite(s2,faceImage1);
		}
	}
	//cv::imshow( "result", img );
}
Example #29
0
Mat detectAndDraw( Mat& img, CascadeClassifier& cascade,
                    CascadeClassifier& nestedCascade,
                    double scale, bool tryflip )
{
	Mat img2;
	Mat newROI;
	 //Converson to rgchromaticity space
	img.copyTo(img2);
	Mat M;
	img.copyTo(M);
	//for( int i = 0; i < img.rows; i++ ) {
   	//	const int* ptr = img2.ptr<int>(i);
    	//	float* dptr = M.ptr<float>(i);
    	//	for( int j = 0; j < img.cols; j++ ) {
        //	dptr[3*j] = ptr[3*j+2]*1.0 /(ptr[3*j+0] + ptr[3*j+1] + ptr[3*j+2]);
        //		dptr[3*j+1] = ptr[3*j+1]*1.0 /(ptr[3*j+0] + ptr[3*j+1] + ptr[3*j+2]);
        //		dptr[3*j+2] = (ptr[3*j+0] + ptr[3*j+1] + ptr[3*j+2]);
    	//	}
  	//}

	//cvtColor(img,img2,COLOR_BGR2GRAY);
    int i = 0;
    double t = 0;
    vector<Rect> faces, faces2;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, COLOR_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
        |CASCADE_FIND_BIGGEST_OBJECT
        //|CASCADE_DO_ROUGH_SEARCH
        |CASCADE_SCALE_IMAGE
        ,
        Size(30, 30) );
    if( tryflip )
    {
        flip(smallImg, smallImg, 1);
        cascade.detectMultiScale( smallImg, faces2,
                                 1.1, 2, 0
                                 |CASCADE_FIND_BIGGEST_OBJECT
                                 //|CASCADE_DO_ROUGH_SEARCH
                                 |CASCADE_SCALE_IMAGE
                                 ,
                                 Size(30, 30) );
        for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
        {
            faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
        }
    }
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        
        Point faceCenter;
        Scalar color = colors[i%8];
        int radius;

        double aspect_ratio = (double)r->width/r->height;
        if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
        {
            
            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            faceCenter = center;
            radius = cvRound((r->width + r->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        }
        else
            rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
                       cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
                       color, 3, 8, 0);
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CASCADE_FIND_BIGGEST_OBJECT
            //|CASCADE_DO_ROUGH_SEARCH
            //|CASCADE_DO_CANNY_PRUNING
            |CASCADE_SCALE_IMAGE,
            Size(30, 30) );
        int nestedFlag=0;
        Scalar s1,s2;
            
        float gvalue=0;
        int count=0;
        stringstream s;
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            //printf("%d",center.x);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            	
            char c= waitKey(5);
            
            
            if(center.x < faceCenter.x )
            {
            	Mat finalc;
            	circle( img, center, radius, color, 3, 8, 0 );
            	newROI = img2(Rect(center.x-radius,center.y-radius,radius*2,radius*2));
	            newROI.copyTo(finalc);
                //imshow("newROI",newROI);
                int top=150-newROI.rows;
                int left=150-newROI.cols;
                //copyMakeBorder(finalCropped,finalCropped,top,0,left,0,BORDER_REPLICATE);
                resize(finalc,finalCropped,Size(128,128));
                
                vw << finalCropped;
            }
        }
      
     // 	printf("%f\n",gvalue);
    }
    	
   // cv::imshow( "result", img);
   // cv::imshow( "newROI", newROI );
   return img;
}
Example #30
0
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
                    CascadeClassifier& nestedCascade,
                    double scale, bool tryflip )
{
    int i = 0;
    double t = 0;
    vector<Rect> faces, faces2;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

    cvtColor( img, gray, COLOR_BGR2GRAY );
    resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
    equalizeHist( smallImg, smallImg );

    t = (double)cvGetTickCount();
    cascade.detectMultiScale( smallImg, faces,
        1.1, 2, 0
       // |CASCADE_FIND_BIGGEST_OBJECT
       // |CASCADE_DO_ROUGH_SEARCH
        |CASCADE_SCALE_IMAGE
        ,
        Size(30, 30) );
    if( tryflip )
    {
        flip(smallImg, smallImg, 1);
        cascade.detectMultiScale( smallImg, faces2,
                                 1.1, 2, 0
                                // |CASCADE_FIND_BIGGEST_OBJECT
                                 //|CASCADE_DO_ROUGH_SEARCH
                                 |CASCADE_SCALE_IMAGE
                                 ,
                                 Size(30, 30) );
        for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
        {
            faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
        }
    }
    t = (double)cvGetTickCount() - t;
    printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
    for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
    {
        Mat smallImgROI;
        vector<Rect> nestedObjects;
        Point center;
        Scalar color = colors[i%8];
        int radius;

        double aspect_ratio = (double)r->width/r->height;
        if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
        {
             Mat newImage;
            img.copyTo(newImage);

            center.x = cvRound((r->x + r->width*0.5)*scale);
            center.y = cvRound((r->y + r->height*0.5)*scale);
            radius = cvRound((r->width + r->height)*0.25*scale);
            //circle( img, center, radius, color, 3, 8, 0 );
            radius=radius*0.8;
            int hf=0.2*radius;
            printf("size=%d\n",radius*2);
            Rect roi(center.x-radius,center.y-radius,2*radius,2*radius+hf);
            int se=20;
            Rect roi_out(center.x-radius-se,center.y-radius-se,2*radius+2*se,2*radius+2*se+hf);
            Rect roi_in(center.x-radius+se,center.y-radius+se,2*radius-2*se,2*radius-2*se);
            Mat mask_smooth=Mat::zeros(img.rows,img.cols,CV_8U);
            mask_smooth(roi_out)=255;
            mask_smooth(roi_in)=0;
            Mat image_roi=img(roi);
            Mat mask=Mat::zeros(img.rows,img.cols,CV_8U);
            mask(roi)=1;
            Scalar avg_pic_intensity=mean(image_roi);
            newImage(Rect(0,0,img.cols,img.rows))=avg_pic_intensity;
            img.copyTo(newImage,mask);
            Mat newImageBlurred; 
            blur(newImage,newImageBlurred,Size(20,20));
            blur(mask_smooth,mask_smooth,Size(20,20));
            newImageBlurred.copyTo(newImage,mask_smooth);
            
            //newImage(roi)=image_roi;
            imshow("blurred",newImageBlurred);
            imwrite("OutputImage.jpg",newImage);
            imshow("new",newImage);
            imshow("mask",mask_smooth);
            waitKey(0);
            std::cout<<avg_pic_intensity; 
            /*for(int i=img.;i<img.rows;i++)
            {
                for(int j=0;j<img.cols;j++)
                {
                    
                }
            }*/
            
            rectangle(img,cvPoint(center.x-radius,center.y-radius),cvPoint(center.x+radius,center.y+radius),avg_pic_intensity,3,8,0);
        }
        else
            rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
                       cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
                       color, 3, 8, 0);
        if( nestedCascade.empty() )
            continue;
        smallImgROI = smallImg(*r);
        nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
            1.1, 2, 0
            //|CASCADE_FIND_BIGGEST_OBJECT
            //|CASCADE_DO_ROUGH_SEARCH
            //|CASCADE_DO_CANNY_PRUNING
            |CASCADE_SCALE_IMAGE
            ,
            Size(30, 30) );
        for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
        {
            center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
            center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
            radius = cvRound((nr->width + nr->height)*0.25*scale);
            circle( img, center, radius, color, 3, 8, 0 );
        }
    }
    cv::imshow( "result", img );
}