예제 #1
0
파일: main.cpp 프로젝트: MiaoChaoran/shiyan
int main( int argc, char** argv )
{
    cascade_name = "d:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml";
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
    cvNamedWindow( "result", 1 );

    const char* filename = "d:\\picture\\1112.jpg";
    IplImage* image = cvLoadImage( filename, 1 );

    if( image )
    {
        detect_and_draw( image );
        cvWaitKey(0);
        cvReleaseImage( &image );
    }

    cvDestroyWindow("result");

    return 0;
}
예제 #2
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Create a sample image
    //IplImage *img = cvLoadImage("image.png",1);
    
    IplImage *img;
    //IplImage *image;
	CvCapture *capture;
	capture = cvCreateCameraCapture(1);
	
	cvNamedWindow("Projet SI", CV_WINDOW_AUTOSIZE);
	
	char key;
	
	int j=0;
	
	while(key != 'q' && key != 'Q') {
 
		++j;
	   // On récupère une image
	   //puts("lecture");
	   img = cvQueryFrame(capture);
 
		if(j>=10)
		{
			j=0;
			detect_and_draw(img);
		}
		
	   // On affiche l'image dans une fenêtre
	   cvShowImage( "Projet SI", img);
 
	   // On attend 10ms
	   key = cvWaitKey(10);
 
	}
	
	//img = cvQueryFrame(capture);
    
    // Call the function to detect and draw the face positions
    //detect_and_draw(img);

    // Wait for user input before quitting the program
    //cvWaitKey();

    // Release the image
    cvReleaseImage(&img);
    //sleep(2);
    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("Projet SI");
    
    

    // return 0 to indicate successfull execution of the program
    return 0;
}
예제 #3
0
void refresh_dots(IplImage* img, IplImage* gray, vector<Point2f> points[], boost::mutex* faceMutex, CvRect* faceBox)
{
	faceMutex->lock();
	// Call the function to detect and draw the face positions
	cvSetImageROI(img,cvRect(0,0, img->width, img ->height));
	faceBox = detect_and_draw(img, "C:/University/OpenCV2.2/data/haarcascades/haarcascade_frontalface_alt.xml");
	//shrinks facebox
	faceBox->width /= 2;
	faceBox->x += faceBox->width/2;
	faceBox->height /= 2;
	faceBox->y += faceBox->height/2;
	
	//add dots to image
	add_dots(gray, &points[FACE], faceBox);
	cvSetImageROI(img,cvRect(0,0, img->width, img ->height));
	faceMutex->unlock();
}
예제 #4
0
// A Simple Camera Capture Framework
int main()
{

  // Gets the input video stream from camera
  CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
  
  // Checks if the input stream is obtained
  if( !capture ) 
  {
    fprintf( stderr, "ERROR: capture is NULL \n" );
    getchar();
    return -1;
  }

  // Show the image captured from the camera in the window and repeat
  while( 1 )
  {

    // Get one frame
    IplImage* frame = cvQueryFrame( capture );
    
    // Cecks if a frame is obtained
    if( !frame )
    {
      fprintf( stderr, "ERROR: frame is null...\n" );
      getchar();
      break;
    }

    // Flips the frame into mirror image 
    cvFlip(frame,frame,1);

    // Call the function to detect and draw the hand positions
    detect_and_draw(frame);

    //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
    //remove higher bits using AND operator
    if( (cvWaitKey(10) & 255) == 27 ) 
      break;
  }

  // Release the capture device housekeeping
  cvReleaseCapture( &capture );

  return 0;
}
예제 #5
0
 void imageCb(const sensor_msgs::ImageConstPtr& msg)
 {
     cv_bridge::CvImagePtr cv_ptr;
     try
     {
         cv_ptr = cv_bridge::toCvCopy(msg, enc::BGR8);
     }
     catch (cv_bridge::Exception& e)
     {
         ROS_ERROR("cv_bridge exception: %s", e.what());
         return;
     }
     //Do the fancy opencv magic
     IplImage ipled = cv_ptr->image;
     IplImage * downscaled = cvCreateImage(cvSize(640/imgscale,480/imgscale),ipled.depth, ipled.nChannels);
     cvResize(&ipled, downscaled, CV_INTER_LINEAR);
     detect_and_draw(downscaled,ipled);
 }
예제 #6
0
int main(void) {
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    cascade = (CvHaarClassifierCascade*)cvLoad(cascadeFile, 0, 0, 0 );

    if( !cascade ) {
        fprintf( stderr,"ERROR: Could not load classifier cascade\n" );
    }
    storageHaart = cvCreateMemStorage(0);
    capture = cvCaptureFromAVI(aviFile);


    cvNamedWindow( "Detecta", 1 );
    cvNamedWindow( "onlyhaart", 1 );

    if (capture) {
        for(;;) {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            CvSize size = cvSize(frame->width,frame->height); // get current frame size
            if( !frame )
                break;
            if( !frame_copy ) {
                frame_copy = cvCreateImage(size,
                                           IPL_DEPTH_8U, frame->nChannels );
            }
            if( frame->origin == IPL_ORIGIN_TL ) {
                cvCopy( frame, frame_copy, 0 );
            } else {
                cvFlip( frame, frame_copy, 0 );
            }
            if( cvWaitKey( 1 ) >= 0 ){
                break;
            }
            detect_and_draw(frame_copy);
        }

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    cvDestroyWindow("Detecta");
    cvDestroyWindow("onlyhaart");
}
예제 #7
0
JNIEXPORT jint JNICALL Java_ispy_main_OpenCV_facedetect(JNIEnv* env, jobject thiz)
{
	//int result[9];
	//result = (*env)->NewIntArray(env, 9);

/*
	if (result == NULL) {
	     return NULL;
	 }
*/
	cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
	if(!cascade)
	{
		LOGE("Error in Opening cascade classifier") ;
	}
	storage = cvCreateMemStorage(0);
	IplImage *pWorkImage=cvCreateImage(cvGetSize(pImage),IPL_DEPTH_8U,1);
	cvCvtColor(pImage,pWorkImage,CV_BGR2GRAY);




	if(num==1){

		LOGE("****************************Tracking*************************************");
			track(pImage,flag,Cx,Cy,R);
			flag=1;


		}

	if(num==0){
		LOGE("****************************Detecting Faces**************************");
	pWorkImage = detect_and_draw( pImage );
	}

	 //(*env)->SetIntArrayRegion(env, result, 0, 9, coord);
	 LOGE("Value returning");
     return num;

}
예제 #8
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Create a sample image
    IplImage *img = cvLoadImage("1.jpg");

    // Call the function to detect and draw the face positions
    detect_and_draw(img);

    // Wait for user input before quitting the program
    cvWaitKey();

    // Release the image
    cvReleaseImage(&img);
    
    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}
예제 #9
0
int main (int argc, char **argv)
{

	CvCapture *capture = 0;
	IplImage *frame, *frame_copy = 0;
	cascade = (CvHaarClassifierCascade *) cvLoad ("yolo.xml", 0, 0, 0);

	if (!cascade)
	{
		printf ("ERROR: Could not load classifier cascade\n");
		return -1;
	}

	storage = cvCreateMemStorage (0);

	capture = cvCaptureFromCAM (0);

	if (capture){
		int j = 0;

		for (;;){

			FILE *fin;
			int i = 0;
			flag = 0, f = 0;

			if(!cvGrabFrame (capture)){
				break;
			}

			frame = cvRetrieveFrame (capture);

			if (!frame){
				break;
			}

			if (!frame_copy){
				frame_copy = cvCreateImage(
					cvSize (frame->width, frame->height),
					IPL_DEPTH_8U, frame->nChannels);
			}

			system ("ps -e | grep totem > sample.txt");

			fin = fopen ("sample.txt", "r");

			fflush (fin);

			while (!feof (fin)){
				char a[40];
				fscanf (fin, "%s\n", a);
				if (a[i] == 't' && a[i + 1] == 'o' && a[i + 2] == 't'
				&& a[i + 3] == 'e' && a[i + 4] == 'm'){
					f = 1;
					break;
				}
      			else{
					f = 0;
				}
    		}

			fclose (fin);

			if (frame->origin == IPL_ORIGIN_TL){
				cvCopy (frame, frame_copy, 0);
			}
			else{
				cvFlip (frame, frame_copy, 0);
			}

  			flag = detect_and_draw (frame_copy);

			if (f == 0)
			{
				printf("no totem playing\n
						please switch off the application from the command centre\n
						or open a video file\n");
				sleep (5);
			}
			else if (flag == 0 && f == 1 && played == 1)
			{
				system ("totem --pause");
				played = 0;
			}
			else if (flag == 1 && f == 1 && played == 0)
			{
				system ("totem --play");
				played = 1;
			}

			if (cvWaitKey (10) >= 0)
				break;

		}
예제 #10
0
int main( int argc, char** argv )
{
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    int optlen = strlen("--cascade=");

    int tampil = 0, dgcascade = 0;

    if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
        dgcascade=1;
    }
    else
    {
        cascade_name = "porn1.xml";
        input_name = argc > 1 ? argv[1] : 0;
        dgcascade=0;
    }

    if( argc > 2+dgcascade ) {     
        if(strncmp(argv[2+dgcascade],"show", strlen("show")) == 0 ) {
          tampil=1;
        }
    } 
    
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: porndetect --cascade=\"<cascade_path>\" [filename|camera_index] <show>\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);

    
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else 
    {

        const char* filename = input_name; 
        IplImage* image = cvLoadImage( filename, 1 );

        if( image )
        {
	    capture = 0;
	
        } else {

            capture = cvCaptureFromAVI( input_name ); 
	}
    }
   
    //create window "result"
    cvNamedWindow( "result", 1 );

    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
            
            detect_and_draw( frame_copy, 1 );

            if( cvWaitKey( 10 ) >= 0 )
                break;
        }

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        const char* filename = input_name ? input_name : (char*)argv[1];
        IplImage* image = cvLoadImage( filename, 1 );

        if( image )
        {
            detect_and_draw( image, tampil );
            if(tampil==1)
            {
              cvWaitKey(0);
            }
            cvReleaseImage( &image );
		
        }
        /*else
        {
            // assume it is a text file containing the
            //   list of the image filenames to be processed - one per line 
            FILE* f = fopen( filename, "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }*/

    }
    
    //close "result" win
    cvDestroyWindow("result");

    return 0;
}
예제 #11
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Used for calculations
    int optlen = strlen("--cascade=");

    // Input file name for avi or image file.
    const char* input_name;


    // Load the HaarClassifierCascade

    
    // Allocate the memory storage
    storage = cvCreateMemStorage(0);


    capture = cvCaptureFromCAM( 0);
    // Create a new named window with title: result
    cvNamedWindow( "result", 1 );

  
    // If loaded succesfully, then:
    if( capture )
    {
        // Capture from the camera.
        for(;;)
        {
            // Capture the frame and load it in IplImage
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );

            // If the frame does not exist, quit the loop
            if( !frame )
                break;
            
            // Allocate framecopy as the same size of the frame
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );

            // Check the origin of image. If top left, copy the image frame to frame_copy. 
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            // Else flip and copy the image
            else
                cvFlip( frame, frame_copy, 0 );
            

		for(int i=0;i<4;i++)
		{
	
	    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name[i], 0, 0, 0 );
    
	    // Check whether the cascade has loaded successfully. Else report and error and quit
	    if( !cascade )
    		{
       		 fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
       		 return -1;
    		}
            // Call the function to detect and draw the face
            	if(detect_and_draw(frame_copy,cascade))
		{

			std::cout<< i <<std::endl;
			switch(i)
			{
			case 0:	
				std::cout<<"face detetected"<<std::endl;
				break;
			case 1:	
				std::cout<<"eye detetected"<<std::endl;
				break;
			case 2:	
				std::cout<<"nose detetected"<<std::endl;
				break;
			case 3:	
				std::cout<<"mouth detetected"<<std::endl;
				break;
					
			}	
				
		}						

		}
            // Wait for a while before proceeding to the next frame
            if( cvWaitKey( 1 ) >= 0 )
             break;

//              cvWaitKey (10);
   


     }

        // Release the images, and capture memory
	cvReleaseHaarClassifierCascade(&cascade);

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
	cvReleaseMemStorage(&storage);


    }

    // If the capture is not loaded succesfully, then:
 return 0;

}
예제 #12
0
파일: facerecog.c 프로젝트: atiti/OFR
void process_image(IplImage *img) {
	detect_and_draw(img);

}
예제 #13
0
파일: main.cpp 프로젝트: bztrn/3dface
int main(int argc, char **argv)
{
	cascade = (CvHaarClassifierCascade *) cvLoad("cascade.xml", 0, 0, 0);
	IplImage *faceDepth = cvCreateImage(cvSize(100, 100), 8, 1);
	char name[1000];
	int imageCnt=0;
	PCAWrapper pca;

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

	storage = cvCreateMemStorage(0);

	int key = -1;

	while ((key & 0xFF) != 0x1B)
	{							// Break when ESC is pressed.
		key = cvWaitKey(10);

		IplImage *image = freenect_sync_get_rgb_cv(0);
		if (!image)
		{
			printf("Error: Kinect not connected?\n");
			return -1;
		}

		// DEBUG;
		/* 
		   IplImage *irimage = freenect_sync_get_ir_cv(0); if (!irimage) {
		   printf("Error: Kinect not connected?\n"); return -1; } */

		// DEBUG;

		// cvCvtColor(image, image, CV_RGB2BGR);
		IplImage *depth = freenect_sync_get_depth_cv(0);
		if (!depth)
		{
			printf("Error: Kinect not connected?\n");
			return -1;
		}

		// DEBUG;


		// printf("%d\n", key);

		if ((key & 0xFF) == 'p')
		{
			detect_and_draw(image, depth, faceDepth, true);
		}
		detect_and_draw(image, depth, faceDepth, false);

		if ((key & 0xFF) == 'i')
		{
			imageCnt++;

			sprintf(name, "face%d", imageCnt);

			printf("face %s registered!\n", name);

			pca.insertImage(faceDepth, name);
		}

		if((key & 0xFF) == 'r')
		{
			printf("%s Dist = %f\n", pca.search(faceDepth), pca.searchDist(faceDepth));
		}

		if((key & 0xFF) == 't')
		{
			printf("Training...\n");
			pca.training();
		}

		cvShowImage("RGB", image);

		// DEBUG;
		// cvShowImage("IR", irimage);
		cvShowImage("Depth", depth);
		// cvShowImage("Depth", GlViewColor(depth));
	}
	return 0;
}
예제 #14
0
public:void principal() {
        CvCapture* capture = 0;
        IplImage *frame, *frame_copy = 0;
        IplImage *frameAnterior;
        cascade = (CvHaarClassifierCascade*)cvLoad(cascadeFile, 0, 0, 0 );

        if( !cascade )
        {
            fprintf( stderr,"ERROR: Could not load classifier cascade\n" );
        }
        storageHaart = cvCreateMemStorage(0);

        capture = cvCaptureFromAVI(aviFile);


        cvNamedWindow( "Detecta", 1 );
        cvNamedWindow( "bluedetect", 1 );
        cvNamedWindow( "onlyhaart", 1 );

        if (capture) {
            for(int c=0; c<1; c++) {
                if( !cvGrabFrame( capture ))
                    break;
                frame = cvRetrieveFrame( capture );
                CvSize size = cvSize(frame->width,frame->height); // get current frame size
                if( !frame )
                    break;
                if( !frame_copy ) {
                    frame_copy = cvCreateImage(size,
                                                IPL_DEPTH_8U, frame->nChannels );
                    frameAnterior = cvCreateImage(size,
                                            IPL_DEPTH_8U, frame->nChannels );
                }
                if( frame->origin == IPL_ORIGIN_TL ) {
                    cvCopy( frame, frame_copy, 0 );
                } else {
                    cvFlip( frame, frame_copy, 0 );
                }


                if( cvWaitKey( 10 ) >= 0 ){
                    break;
                }

                if( !mhi) {
                    if( buf == 0 ) {
                        buf = (IplImage**)malloc(N*sizeof(buf[0]));
                        memset( buf, 0, N*sizeof(buf[0]));
                    }

                    for(int i = 0; i < N; i++ ) {
                        cvReleaseImage( &buf[i] );
                        buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
                        cvZero( buf[i] );
                    }
                    cvReleaseImage( &mhi );
                    cvReleaseImage( &orient );
                    cvReleaseImage( &segmask );
                    cvReleaseImage( &mask );

                    mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
                    cvZero( mhi ); // clear MHI at the beginning
                    orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
                    segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
                    mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
                }
                detect_and_draw(frame_copy, frameAnterior);
            }

            cvReleaseImage( &frame_copy );
            cvReleaseImage( &frameAnterior );
            cvReleaseCapture( &capture );
        }
        cvDestroyWindow("Detecta");
        cvDestroyWindow("bluedetect");
        cvDestroyWindow("onlyhaart");
    }
예제 #15
0
int signDetection( IplImage* image )
{
    //CvCapture* capture = 0;
    //IplImage *frame, *frame_copy = 0;
    //int optlen = strlen("--cascade=");
    //const char* input_name = "test.avi";

    /*if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        cascade_name = "cascade/nopark_F_cascade.xml";
        input_name = argc > 1 ? argv[1] : 0;
    }
*/
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
       // fprintf( stderr,
       // "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
    
    //if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
    //    capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    //else
        //capture = cvCaptureFromAVI( input_name ); 

    cvNamedWindow( "DETECTION", 1 );
	//printf("capture= %s\n",capture);
    /*if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
            
            detect_and_draw( frame_copy );

            if( cvWaitKey( 10 ) >= 0 )
                break;
        }

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {*/
        //const char* filename = input_name ? input_name : (char*)"lena.jpg";
        //IplImage* image = cvLoadImage( filename, 1 );

        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            //FILE* f = fopen( filename, "rt" );
			FILE* f = fopen( "lena.jpg", "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }

   // }
    
    cvDestroyWindow("DETECTION");

    return 0;
}
예제 #16
0
int main( int argc, char** argv )
{
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    int optlen = strlen("--cascade=");
    const char* input_name;
 
    if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        cascade_name= "haarcascade_frontalface_alt2.xml";
        //opencv装好后haarcascade_frontalface_alt2.xml的路径,
       //也可以把这个文件拷到你的工程文件夹下然后不用写路径名cascade_name= "haarcascade_frontalface_alt2.xml";  
       //或者cascade_name ="C:\\Program Files\\OpenCV\\data\\haarcascades\\haarcascade_frontalface_alt2.xml"
        input_name = argc > 1 ? argv[1] : 0;
    }
 
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
 
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
 
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else
        capture = cvCaptureFromAVI( input_name ); 
 
    cvNamedWindow( "result", 1 );
 
    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
 
            detect_and_draw( frame_copy );
 
            if( cvWaitKey( 10 ) >= 0 )
                break;
        }
 
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        const char* filename = input_name ? input_name : (char*)"00.jpg";
        IplImage* image = cvLoadImage( filename, 1 );
 
        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( filename, "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }
 
    }
 
    cvDestroyWindow("result");
 
    return 0;
}
예제 #17
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Used for calculations
    int optlen = strlen("--cascade=");

    // Input file name for avi or image file.
    const char* input_name;

    // Check for the correct usage of the command line
    if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name_face = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        fprintf( stderr,
                 "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        //return -1;
        input_name = argc > 1 ? argv[1] : 0;
    }

    // Load the HaarClassifierCascade
    cascade_eye = (CvHaarClassifierCascade*)cvLoad( cascade_name_eye, 0, 0, 0 );
    cascade_face = (CvHaarClassifierCascade*)cvLoad( cascade_name_face, 0, 0, 0 );

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if( !cascade_face )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Find whether to detect the object from file or from camera.
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else
        capture = cvCaptureFromAVI( input_name );


    // Create a new named window with title: result
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    //cvResizeWindow( "result", 640, 480 );

    // Find if the capture is loaded successfully or not.

    // If loaded succesfully, then:
    if( capture )
    {
        // Capture from the camera.
        for(;;)
        {
            // Capture the frame and load it in IplImage
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );

            // If the frame does not exist, quit the loop
            if( !frame )
                break;

            // Allocate framecopy as the same size of the frame
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );

            // Check the origin of image. If top left, copy the image frame to frame_copy.
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            // Else flip and copy the image
            else
                cvFlip( frame, frame_copy, 0 );

            // Call the function to detect and draw the face
            //fprintf( stdout, "Passing to detectAndDraw\n");
            detect_and_draw( frame_copy );

            // Wait for a while before proceeding to the next frame
            if( cvWaitKey( 10 ) >= 0 )
                break;
        }

        // Release the images, and capture memory
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }

    // If the capture is not loaded succesfully, then:
    else
    {
        // Assume the image to be lena.jpg, or the input_name specified
        const char* filename = input_name ? input_name : (char*)"lena.jpg";

        // Load the image from that filename
        IplImage* image = cvLoadImage( filename, 1 );

        // If Image is loaded succesfully, then:
        if( image )
        {
            // Detect and draw the face
            detect_and_draw( image );

            // Wait for user input
            cvWaitKey(0);

            // Release the image memory
            cvReleaseImage( &image );
        }
        else
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( filename, "rt" );
            if( f )
            {
                char buf[1000+1];

                // Get the line from the file
                while( fgets( buf, 1000, f ) )
                {

                    // Remove the spaces if any, and clean up the name
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';

                    // Load the image from the filename present in the buffer
                    image = cvLoadImage( buf, 1 );

                    // If the image was loaded succesfully, then:
                    if( image )
                    {
                        // Detect and draw the face from the image
                        detect_and_draw( image );

                        // Wait for the user input, and release the memory
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                // Close the file
                fclose(f);
            }
        }

    }

    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}
예제 #18
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Input file name for avi or image file.
    const char* input_name;

    // Check for the correct usage of the command line
    if( argc <= 2 )
        input_name = argv[1];
    else
    {
        fprintf( stderr,
        "Usage: BSubtraction Filename\n" );
        system ("pause"); // MS-DOS pause command
        return -1;
        /*input_name = argc > 1 ? argv[1] : 0;*/
    }

    // Configure output file
    OutFile = fopen("svgout.svg", "w+");
    
    
    // Initialize Face detection
    // Load the HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    
    
    // Allocate the memory storage
    storage = cvCreateMemStorage(0);
    
    // Find whether to detect the object from file or from camera.
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else
        capture = cvCaptureFromAVI( input_name ); 

    // Create a new named window with title: result
    cvNamedWindow( "result", 1 );
    cvNamedWindow("original", 1);

    // Find if the capture is loaded successfully or not.

    // If loaded succesfully, then:
    if( capture )
    {
        
        // Capture from the camera.
        for(;;)
        {
            // Capture the frame and load it in IplImage
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );

            // If the frame does not exist, quit the loop
            if( !frame )
                break;

            // Allocate framecopy as the same size of the frame
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );

            // Reserve Memory for background subtraction if you haven't already
            pGMM = (pGMM==0) ? cvCreatePixelBackgroundGMM(frame->width,frame->height)
                           : pGMM;
            pGMM->fAlphaT = .005f;

            // Check the origin of image. If top left, copy the image frame to frame_copy. 
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            // Else flip and copy the image
            else
                cvFlip( frame, frame_copy, 0 );
            
            // Call the function to detect and draw the facees
            detect_and_draw( frame_copy );
            
            //system ("pause"); // MS-DOS pause command
            
            // Wait for a while before proceeding to the next frame
            if( cvWaitKey( 10 ) >= 0 )
                break;
        }

        // Release the images, and capture memory
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }else{// Assume the image to be lena.jpg, or the input_name specified
        const char* filename = input_name ? input_name : (char*)"test.jpg";
        singleframe = true;
        // Load the image from that filename
        IplImage* image = cvLoadImage( filename, 1 );
        // If Image is loaded succesfully, then:
        if( image ){
            pGMM = (pGMM==0) ? cvCreatePixelBackgroundGMM(image->width,image->height)
                           : pGMM;
            pGMM->fAlphaT = .005f;
            // Detect and draw the face
            detect_and_draw( image );

            // Release the image memory
            cvReleaseImage( &image );
        }
    }      

    
    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");
    cvDestroyWindow("original");
    
    // release the background subtraction structure
    //cvReleasePixelBackgroundGMM(&pGMM);
    
    // write out terminal data and close your svg file
    page_svg_close(OutFile);    
    fclose(OutFile);    

    // return 0 to indicate successfull execution of the program
    return 0;
}
예제 #19
0
/** @funcion main */ 
int main( int argc, char** argv ){
	presentacion();
	CvCapture* capture = 0;  
    // Imagen
    IplImage *frame, *frame_copy = 0;	
    
	// Carga el HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

	 // Se verifica si el archivo Cascade se ha cargado o no
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }

    // Se reserva memoria para el almacenamiento de las caracteristicas a buscar con el archivo cascade
    storage0 = cvCreateMemStorage(0);
    storage1 = cvCreateMemStorage(0);
    storage2 = cvCreateMemStorage(0);

    cvNamedWindow( "result", 1 );

    // Find if the capture is loaded successfully or not.
    // If loaded succesfully, then:
    int i=0;
	//capture=cvCreateFileCapture("6.avi");
    ///*
	capture = cvCreateCameraCapture(0);
    if ( !capture ) {
        capture = cvCaptureFromCAM(0);
        if ( !capture ) {
            fprintf( stderr, "Cannot open initialize webcam!\n" );
            return 1;
        }
    }
	//*/
    if( capture )
    {
        for(;;i++)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );

            // Si el frame no existe salimos del lazo
            if( !frame )
                break;

            // Le colocamos los mismos parametros de tamaño al frame_copy
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels );

            // Verifica el origen de la imagen para establecer la copia
            if( frame->origin == IPL_ORIGIN_TL ){
                cvCopy( frame, frame_copy, 0 );
				//cvCopy( frame, canny, 0 );
			}
            // Caso contrario se la invierte para poder manejarla
            else
                cvFlip( frame, frame_copy, 0 );

            // Se llama a la funcion que detecta rostros y otros patrones faciales		
			
			
			detect_and_draw( frame_copy );

            
            char c =  cvWaitKey( 10 );
           switch(c){
               case 27:
                   exit(0);
                   break;
               case '1':
                   cabeza_opt=!cabeza_opt;				   
                    break;
               case '2':
                   ojo_opt=!ojo_opt;
                   break;            
               case '3':
                   nariz_opt=!nariz_opt;                   
                   break;
               case '4':
				   boca_opt=!boca_opt;
                   break;
			   case '5':			  
					oreja_opt=!oreja_opt;
					break;
			   case '6':			  
					menton_opt=!menton_opt;
					break;
			
        }
        }
		
		
        // Se liberan los recursos de memoria
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
	

    
    cvDestroyWindow("result");
    return 0;
}
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        printf("\n--Argument error!\n--Please specify input/output directory. ");
        printf("(e.g. AddNewFace.exe faces_to_find)\n");
        return -1;
    }
    //description
    printf("* This program is used to ADD NEW FACE images to be found.\n");
    printf("* Transfer face images to FEATURE DATA, and store in file 'feature.dat'.\n");
    printf("* Face image should be better named as PERSON's name.\n");
    printf("Press any key to continue...");
    getchar();

    //load face cascade
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);

    //find all images
    string strFilePath(argv[1]);
    strFilePath.append("\\*");
    WIN32_FIND_DATAA FileData;
    HANDLE hFind;
    hFind = FindFirstFileA(strFilePath.c_str(), &FileData);
    if (hFind == INVALID_HANDLE_VALUE)
    {
        printf ("\n--Invalid File Handle. GetLastError reports %d/n", GetLastError ());
        return -1;
    }
    //get image names
    vector<string> fileNames;
    do
    {
        //eliminate directories
        //if(!strcmp(FileData.cFileName,"..") || !strcmp(FileData.cFileName,"."))
        if(FileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
            continue;
        fileNames.push_back(string(FileData.cFileName));
    } while (FindNextFileA(hFind, &FileData));
    //FindClose(&hFind);

    //file to store feature data
    ofstream featureDataFile;
    char dataFileName[256];
    sprintf(dataFileName, "%s\\%s", argv[1], feature_file_name);
    featureDataFile.open(dataFileName, ofstream::out);
    //class object of processing
    CFaceRecognize* pFaceRec = new CFaceRecognize;
    //process images
    for (size_t i = 0; i < fileNames.size(); i++)
    {
        string fileName = fileNames[i];
        int index = fileName.rfind(".");
        string name = fileName.substr(0, index);	//image name, i.e. person's name
        string extend = fileName.substr(index);		//extend name

        //skip feature data file
        if (extend == ".dat")
            continue;

        printf("\nprocessing image: %s.", fileName.c_str());

        //1--load image
        char filePath[256];
        sprintf(filePath, "%s\\%s", argv[1], fileName.c_str());
        IplImage* srcImg = cvLoadImage(filePath, 1);		//rgb
        IplImage* dstImg = 0;
        CvRect roi;
        if( srcImg )
        {
            //get key parts of face
            bool res = detect_and_draw( srcImg, roi);
            if (!res)	continue;

            //use roi to crop image
            dstImg = cvCreateImage(cvSize(roi.width,roi.height),srcImg->depth,srcImg->nChannels);
            cvSetImageROI(srcImg, roi);
            cvCopy(srcImg, dstImg);
            cvResetImageROI(srcImg);
            cvReleaseImage( &srcImg );
        }
        else
        {
            printf("--Error loading source image!\n");
            continue;
        }

        //2--standard image
        IplImage* standImage = pFaceRec->StandardImage(dstImg);
        cvShowImage("standard face",standImage);
        cvWaitKey(30);

        //3--generate LGBPHS data
        vector<int> LGBPHist;
        pFaceRec->GetLGBPHS(standImage, LGBPHist);

        //4--write into feature data file
        size_t imm = 0;
        featureDataFile<<name<<":";
        for (; imm<LGBPHist.size()-1; imm++)
            featureDataFile<<LGBPHist[imm]<<",";
        featureDataFile<<LGBPHist[imm]<<"\n";
    }

    cvReleaseMemStorage(&storage);

    printf("\n\nAll finished...\n");
    //system("pause");
    return 0;
}
예제 #21
0
파일: main.c 프로젝트: RokIrt/HandGesture
int main(int argc,char ** argv){
	int key,t;
	double *Features;
	//code to record video
	CvVideoWriter *writer=0;
	int isColor=1;
	int fps=10;
	int frameW=640;
	int frameH=480;
	writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'),fps,cvSize(frameW,frameH),isColor);
	CvMat *mat=cvCreateMat(8,8,CV_64FC1);
	char *filenamesCov = "../../../fourier/fourier/Variances";
	char * FileMeans = "../../../fourier/fourier/Means.txt";
	int numGestures;
	numGestures=11;
	int numTraining=80;	
	int numFeatures=8;
	//CvArr** invCovMatrices= (CvArr **)malloc(sizeof(CvArr *)*numGestures);
	CvArr** eigenVects= (CvArr **)malloc(sizeof(CvArr *)*numGestures);
	CvArr** Means=(CvArr **)malloc(sizeof(CvArr *)*numGestures);
	CvArr** eigenVals=(CvArr **)malloc(sizeof(CvArr*)*numGestures);
	//end record video       
	int indx[] = {6,11,12,14,15,22,27,28,33,36,42}; 
	//5, a, b, c,caps,g,l,LC,p,RC,v,

	CvCapture* capture = NULL;
	cascade = (CvHaarClassifierCascade*)cvLoad( "../NewTrained.xml",
	0, 0, 0 );	
	storage = cvCreateMemStorage(0); 
	IplImage* img;
	IplImage* img1;
	IplImage* img2;
	IplImage* imb;

	if(NULL==(capture = cvCaptureFromCAM(1))){
		printf("\nError on cvCaptureFromCAM");
		return -1;
	}
	
	train(Means,eigenVects,eigenVals,numGestures,19,numFeatures,indx);
	fprintf(stderr,"blahblah\n");
	//ReadInData(filenamesCov,FileMeans,invCovMatrices,Means,numGestures,numFeatures);
	//-----
	cvNamedWindow("Capture", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Capture2", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Capture3", CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Window",CV_WINDOW_AUTOSIZE);	

	cvMoveWindow("Capture", 550, 250);
	cvMoveWindow("Capture2",850, 50);
	cvMoveWindow("Capture3",100,500);
	cvMoveWindow("Capture4",500,600);

	for(;;){
		if(NULL==(img=cvQueryFrame(capture))){
			printf("\nError on cvQueryFrame");
		break;
		}

	
		img1=detect_and_draw(img,1);
	    img2=binary_threshold_hsl(img1);
		
		cvShowImage("Capture3",img2);
		cvShowImage("Capture", img1);
		cvShowImage("Capture2",img);
		if(HAND==1)
		{
			
			t=simpleHuRecognize(img2,11);
		}
		if(HAND==1)
		{
			fprintf(stderr,"Found a hand. The gesture recognized is : %c\n",(char) Gest[t]);
			//imb=cvCreateImage("",8,1);
		}
		cvWriteFrame(writer,img1);
		key = cvWaitKey(10);

		if(key==0x1b)
			break;
	}

	cvReleaseCapture(&capture);
	cvDestroyWindow("Capture");
	cvDestroyWindow("Capture2");
	cvDestroyWindow("Capture3");

	cvReleaseImage( &img );
	cvReleaseImage(&img1);

	cvWaitKey(0);
}
// main()
int main( int argc, char** argv )
{

	int starting = 3;
	int flag=0;
	CvPoint pt;
	int x = 0,diffx=0,prevx=0,initx=0;
	int y = 0,diffy=0,prevy=0,inity=0;
 
  	// Open X display
	Display *display = XOpenDisplay (NULL);
	if (display == NULL)
        {
      		fprintf (stderr, "Can't open display!\n");
      		return -1;
    	}
  
  	// Wait 3 seconds to start
  	printf ("Starting in ");
  		fflush (stdout);
  	while (starting > 0)
    	{
      		printf ("\b\b\b %d...", starting);
      		fflush (stdout);
      		sleep (1);
      		starting--;
    	}
  	printf ("\n");
    IplImage* temp=cvCreateImage(cvSize(80,120),8,3);
	IplImage* pframe1;
	
	CvRect *pHandRect=0,*vrect=NULL;
	capture=cvCaptureFromCAM(0);	
	if( !initAll() ) exitProgram(-1);
	
		int g;
	piframe=cvQueryFrame(capture);
	pframe=invert(piframe);
	pframe1=cvCloneImage(piframe);
	// Capture and display video frames until a hand
	// is detected
	int i=0;
	char c;	
	initPCA();
    char ch;

	x :
	printf("came to x\n");
	while( 1 )
	{		
		// Look for a hand in the next video frame
		pframe=cvQueryFrame(capture);
		pframe1=cvCloneImage(pframe);
    	detect_and_draw(pframe);
		pHandRect = detectHand(pframe);
		
		if((pHandRect)&&(pHandRect->x>4)&&(pHandRect->y>4)&&(pHandRect->x*pHandRect->y<(240*300))&&(pHandRect->x<630)&&(pHandRect->y<470))
		{	
			cvRectangle(pframe1,cvPoint((pHandRect->x-4),pHandRect->y-4),cvPoint((pHandRect->x+pHandRect->width+4),pHandRect->y+pHandRect->height+4),CV_RGB(255,0,0),1,8,0);		
			i++;
		}
		else 
			i=0;
		// Show the display image
		cvShowImage( DISPLAY_WINDOW, pframe1 );
		cvMoveWindow(DISPLAY_WINDOW,0,0);
		c=cvWaitKey(10); 
		if(c==27)
                {   
        	exitProgram(0);
		}
		if(i>=3)
		{	// exit loop when a hand is detected
			if(pHandRect) {
				i=0;
				prevx=pHandRect->x;
				initx=pHandRect->x;
				prevy=pHandRect->y+pHandRect->height;
				flag=3;
				break;
			}
		}
	}

	// initialize tracking
	KalmanFilter kfilter;
	startTracking(pframe, *pHandRect,kfilter);
	// Track the detected hand using CamShift
	while( 1 )
	{
		CvRect handBox;

		// get the next video frame
		pframe=cvQueryFrame(capture);
		pframe1=cvCloneImage(pframe);
		handBox = combi_track(pframe,kfilter);
        int old_ht;
        int a;
		IplImage* temp;
		if(!((handBox.x<0)||(handBox.y<0)||((handBox.x+handBox.width)>pframe->width)||((handBox.y+handBox.height)>pframe->height))) 
        {
            if(handBox.height>(1.3*handBox.width))
            {
                old_ht=handBox.height;
                handBox.height=2.4*handBox.width;
                handBox.y-=handBox.height-old_ht;
            }
            cvSetImageROI(pframe,handBox);
            temp=cvCreateImage(cvGetSize(pframe),8,3);

            cvCopy(pframe,temp,NULL);

	        a=recognize(temp);
	        cvReleaseImage(&temp);
	        if(handBox.height>(2.3*handBox.width))
            {	
            	if(a==3)
            		a=5;
            }
			diffx=handBox.x+(handBox.width/2)-initx;
			diffy=handBox.y+handBox.height-(handBox.width/2)-prevy;
			prevx=handBox.x+(handBox.width/2);
			prevy=handBox.y+handBox.height-(handBox.width/2);

	        cvResetImageROI(pframe);
    		cvRectangle(pframe1,cvPoint(handBox.x,handBox.y),cvPoint(handBox.x+handBox.width,handBox.y+handBox.height),CV_RGB(0,0,255),3,8,0);		
            
	        if(diffx<(-60))
	        {	click(display,1,0);
	        	printf("right click\n");
	        	goto x;
	        }
	        else if(diffx>(60))
	        {
	        	fake(display, 0);
	        	printf("left click\n");
	        	goto x;
	        }
	        else
	        {}

        }
        else
        	goto x;

		cvShowImage( DISPLAY_WINDOW, pframe1 );

		ch=cvWaitKey(10);
		if( ch==27 ) {
			exitProgram(0);			
			break;
		}
		if(ch=='s'){
		    cvSetImageROI(pframe,handBox);
		    cvResize(pframe,temp);
		    cvSaveImage("image6.jpg",temp);
		    cvResetImageROI(pframe);
		}
	}
	return 0;
}
예제 #23
0
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;
    int key = 0;

    //Removed the following lines because we're always using the same cascade.

    // Used for calculations
    //int optlen = strlen("--cascade=");

    // Input file name for avi or image file.
    //const char* input_name;

   
    // Check for the correct usage of the command line
    /*if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        fprintf( stderr,
        "Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
        return -1;
        input_name = argc > 1 ? argv[1] : 0;*/
    //}


    // Load the HaarClassifierCascade
    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

    // Check whether the cascade has loaded successfully. Else report and error and quit
    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        return -1;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Find whether to detect the object from file or from camera.
    //if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
    capture = cvCaptureFromCAM(0);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1280);
    cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 800);
    // else
    //    capture = cvCaptureFromAVI( input_name );

    // Create a new named window with title: result
    cvNamedWindow( "result", 1 );

    // Find if the capture is loaded successfully or not.

    // If loaded succesfully, then:
    if( capture )
    {
        // Capture from the camera.
        for(;;)
        {
            // Capture the frame and load it in IplImage
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );

            // If the frame does not exist, quit the loop
            if( !frame )
                break;

            // Allocate framecopy as the same size of the frame
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );

            // Check the origin of image. If top left, copy the image frame to frame_copy.
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            // Else flip and copy the image
            else
                cvFlip( frame, frame_copy, 0 );

            // Call the function to detect and draw the face
            // Wait for a while before proceeding to the next frame

            if( (key = cvWaitKey(100)) >= 0 ){
                if(key == ('d' | 'D')){
                 //printf("D key received.");
                 detect_and_draw(frame_copy, true);
                }
                if(key == ('x' | 'X')) break;
            } else {
                        detect_and_draw( frame_copy , false);
                        }

        }

        // Release the images, and capture memory
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }

    // If the capture is not loaded succesfully, then:
    else
    {
       printf("Capture did not load successfully.");
    }
    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");

    // return 0 to indicate successfull execution of the program
    return 0;
}
예제 #24
0
int main( int argc, char** argv )
{
    CvCapture* capture = 0;
    IplImage *frame, *frame_copy = 0;
    IplImage *image = 0;
    const char* scale_opt = "--scale=";
    int scale_opt_len = (int)strlen(scale_opt);
    const char* cascade_opt = "--cascade=";
    int cascade_opt_len = (int)strlen(cascade_opt);
    const char* nested_cascade_opt = "--nested-cascade";
    int nested_cascade_opt_len = (int)strlen(nested_cascade_opt);
    int i;
    const char* input_name = 0;

    for( i = 1; i < argc; i++ )
    {
        if( strncmp( argv[i], cascade_opt, cascade_opt_len) == 0 )
            cascade_name = argv[i] + cascade_opt_len;
        else if( strncmp( argv[i], nested_cascade_opt, nested_cascade_opt_len ) == 0 )
        {
            if( argv[i][nested_cascade_opt_len] == '=' )
                nested_cascade_name = argv[i] + nested_cascade_opt_len + 1;
            nested_cascade = (CvHaarClassifierCascade*)cvLoad( nested_cascade_name, 0, 0, 0 );
            if( !nested_cascade )
                fprintf( stderr, "WARNING: Could not load classifier cascade for nested objects\n" );
        }
        else if( strncmp( argv[i], scale_opt, scale_opt_len ) == 0 )
        {
            if( !sscanf( argv[i] + scale_opt_len, "%lf", &scale ) || scale < 1 )
                scale = 1;
        }
        else if( argv[i][0] == '-' )
        {
            fprintf( stderr, "WARNING: Unknown option %s\n", argv[i] );
        }
        else
            input_name = argv[i];
    }

    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );

    if( !cascade )
    {
        fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
        fprintf( stderr,
        "Usage: facedetect [--cascade=\"<cascade_path>\"]\n"
        "   [--nested-cascade[=\"nested_cascade_path\"]]\n"
        "   [--scale[=<image scale>\n"
        "   [filename|camera_index]\n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);
    
    if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
        capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    else if( input_name )
    {
        image = cvLoadImage( input_name, 1 );
        if( !image )
            capture = cvCaptureFromAVI( input_name );
    }
    else
        image = cvLoadImage( "lena.jpg", 1 );

    cvNamedWindow( "result", 1 );

    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
                                            IPL_DEPTH_8U, frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame, frame_copy, 0 );
            else
                cvFlip( frame, frame_copy, 0 );
            
            detect_and_draw( frame_copy );

            if( cvWaitKey( 10 ) >= 0 )
                goto _cleanup_;
        }

        cvWaitKey(0);
_cleanup_:
        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else if( input_name )
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( input_name, "rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf, 1000, f ) )
                {
                    int len = (int)strlen(buf), c;
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '\0';
                    printf( "file %s\n", buf ); 
                    image = cvLoadImage( buf, 1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        c = cvWaitKey(0);
                        if( c == 27 || c == 'q' || c == 'Q' )
                            break;
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }
    }
    
    cvDestroyWindow("result");

    return 0;
}