Thread: OpenCV facedetection, a problem with c syntax

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    3

    OpenCV facedetection, a problem with c syntax

    I worked with OpenCV and built a basic source code with Harpia for a OpenCV face detection. I made the changes I needed, compiled it and it works great. It detects all faces and gives me a file.
    But the only thing I don't know how to do is to save each face in an separate file. I think the face file is overwritten each time a face is detected, because I always get the last detected face saved in the file.
    Does any one know how I can save each face in separate file?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <cv.h>
    #include <cvaux.h>
    #include <highgui.h>
    #include <math.h>
    
    #define PI 3.1415926535898
    double rads(double degs)
    {
            return (PI/180 * degs);
    }
    
    int main(int argc, char *argiv[])
    
    
    {
        if ( argc != 2 ) /* argc should be 2 for correct execution */
        {
            /* We print argv[0] assuming it is the program name */
            printf( "usage: %s filename", argiv[0] );
        }
        else
        {
            // We assume argv[1] is a filename to open
            FILE *file = fopen( argiv[1], "r" );
    
            /* fopen returns 0, the NULL pointer, on failure */
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
    
    }
            //declaration block
    
    
    char *block1_arg_Filename = argiv[1];
    IplImage * block1_img_o1 = NULL;
    IplImage * block2_img_i1 = NULL;
    CvPoint block2_point_o1 = cvPoint(0,0);
    CvRect block2_rect_o2 = cvRect( 0, 0, 1, 1);
    IplImage * block2_img_o3 = NULL;
    double block2_double_o4 = 0.0;
    static CvMemStorage* block2_storage = 0;
    static CvHaarClassifierCascade* block2_cascade = 0;
    const char* block2_cascade_name = "/usr/share/harpia/images/haarcascade_frontalface_alt2.xml";
    IplImage * block12_img_i1 = NULL;
    IplImage * block12_img_o1 = NULL;
    IplImage * block7_img_i1 = NULL;
    IplImage * block7_img_o1 = NULL;
    CvRect  block7_rect_i2;
    IplImage * block8_img_i1 = NULL;
    IplImage * block8_img_o1 = NULL;
    
            //execution block
    //Weight: 1
    block1_img_o1 = cvLoadImage(block1_arg_Filename,-1);
    block2_img_i1 = cvCloneImage(block1_img_o1);// IMAGE conection
    //Weight: 2
    
    if(block2_img_i1){
            double scale = 1.3;
            block2_cascade = (CvHaarClassifierCascade*)cvLoad( block2_cascade_name, 0, 0, 0 );
            IplImage* gray = cvCreateImage( cvSize(block2_img_i1->width,block2_img_i1->height), 8, 1 );
            IplImage* small_img = cvCreateImage( cvSize( cvRound  (block2_img_i1->width/scale), cvRound  (block2_img_i1->height/scale)),8, 1 );
            cvCvtColor( block2_img_i1, gray, CV_BGR2GRAY );
            cvResize( gray, small_img, CV_INTER_LINEAR );
            cvEqualizeHist( small_img, small_img );
            if(!block2_img_o3)
            block2_img_o3 = cvCloneImage(block2_img_i1);
            cvCopy(block2_img_i1,block2_img_o3,0);
            block2_storage = cvCreateMemStorage(0);
            cvClearMemStorage( block2_storage );
    
       block2_rect_o2 = cvRect( 0, 0, 1, 1);
            CvSeq* faces = cvHaarDetectObjects( small_img, block2_cascade,  block2_storage,1.1, 2.0, 0/*CV_HAAR_DO_CANNY_PRUNING*/,cvSize(30, 30) );
            block2_double_o4 = faces->total;
            if(faces)
            {
                    int i;
                    for( i = 0; i < (faces ? faces->total : 0); i++ )
                    {
                    CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
                            if(r)
                            {
                                    CvPoint center;
                                    int radius;
                                    center.x = cvRound((r->x + r->width*0.5)*scale);
                                    center.y = cvRound((r->y + r->height*0.75)*scale);
                                    radius = cvRound((r->width + r->height)*0.3*scale);
                                    cvCircle( block2_img_o3, center, radius, cvScalarAll(0), 3, 8, 0 );
                                    if(i == 0)
                                    {
                                            block2_point_o1 = center;
                                            block2_rect_o2.x = (r->x)*scale;
                                            block2_rect_o2.y = (r->y)*scale;
                                            block2_rect_o2.width = (r->width)*scale;
                                            block2_rect_o2.height = (r->height)*scale;
                                    }
    
                            }
                             
    
                    }
    
            }
    
     cvReleaseImage( &gray );
            cvReleaseImage( &small_img );
    }
    block7_rect_i2 = block2_rect_o2;// RECT conection
    block7_img_i1 = cvCloneImage(block2_img_o3);// IMAGE conection
    block12_img_i1 = cvCloneImage(block2_img_o3);// IMAGE conection
    //Weight: 3
    block12_img_o1 = cvCloneImage(block12_img_i1);
    
    if(block12_img_i1)
    cvSaveImage("/home/user/Desktop/facedetect.png" ,block12_img_i1, 0);
    //Weight: 5
    
    if(block7_img_i1){
    
    
            block7_rect_i2.x = MAX(0,block7_rect_i2.x);//Check whether point is negative
            block7_rect_i2.y = MAX(0,block7_rect_i2.y);
    
            block7_rect_i2.x = MIN(block7_img_i1->width-1,block7_rect_i2.x);//Check whether point is out of the image
            block7_rect_i2.y = MIN(block7_img_i1->height-1,block7_rect_i2.y);
            block7_rect_i2.width =  MIN(block7_img_i1->width-block7_rect_i2.x,block7_rect_i2.width);//Check  whether rect reaches out of the image
            block7_rect_i2.height = MIN(block7_img_i1->height-block7_rect_i2.y,block7_rect_i2.height);
            block7_img_o1 =  cvCreateImage(cvSize(block7_rect_i2.width,block7_rect_i2.height),  block7_img_i1->depth,block7_img_i1->nChannels);
    
    
             cvSetImageROI(block7_img_i1,block7_rect_i2);
            cvSaveImage("/home/user/Desktop/face2.png" ,block7_img_i1, 0);
    
            cvCopyImage(block7_img_i1,block7_img_o1);
    
    
    }
    block8_img_i1 = cvCloneImage(block7_img_o1);// IMAGE conection
    //Weight: 6
    block8_img_o1 = cvCloneImage(block8_img_i1);
    
    if(block8_img_i1)
    cvSaveImage("/home/user/Desktop/face.png" ,block8_img_i1, 0);
    //cvSaveImage("/home/user/Desktop/face2.png" ,block7_img_i1, 0);
            cvNamedWindow("Control Window",CV_WINDOW_AUTOSIZE );
            //deallocation block
    cvReleaseImage(&block1_img_o1);
    cvReleaseImage(&block2_img_o3);
    cvReleaseImage(&block2_img_i1);
    cvReleaseMemStorage(&block2_storage);
    cvReleaseImage(&block12_img_o1);
    cvReleaseImage(&block12_img_i1);
    cvReleaseImage(&block7_img_o1);
    cvReleaseImage(&block7_img_i1);
    cvReleaseImage(&block8_img_o1);
    cvReleaseImage(&block8_img_i1);
    
    return 0;
    
    
    } //closing main()
      }

    Thanks for reading
    hope you can help me

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the 3 times you call cvSaveImage, you give it a fixed filename. so yes it overwrites whatever was there before. you need to give that function a different filename each time you run. you could pass that in as a second argument, or perhaps take the input filename, modify it and use that in cvSaveImage. the easiest way to start would be adding a second argument with the output filename.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    A simple solution would be to add a time-stamp of some sort to the end of the file name.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    hey folks,
    thx for your answers!!
    i know the code is not beautiful, so thanks for taking the time to read it!
    Yes, i suppose i should give it an second argument...
    what i imagine would be the best, that the second argument is a starting value of an integer which will be counting higher, when the programm detects a face and give the counted value as output filename.
    But I have no clue haow to do it
    could you help me?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > But I have no clue haow to do it
    > could you help me?
    You managed to read through the openCV manual and construct a face detection program, but you can't even manage to create a variable filename?

    https://www.google.co.uk/search?q=%2...Weight%3A+1%22
    It's bad enough that you tried to palm off copy/paste code as your own work.
    The fact that the bulk of 'your' code seems to have been machine generated just makes it worse.

    At least do yourself a favour and copy from another human.
    Copying "Auto-generated C Code" with lots of useless machine-generated variable names like block12_img_i1 and block7_rect_i2 isn't going to teach you anything useful at all.


    References
    c++.de :: Eine Frage zu OpenCV
    c - OpenCV, Harpia, face detection, save more than face - Stack Overflow
    c - OpenCV: after exporting source code from harpia: "too few arguments to function
    OpenCV, Harpia, face detection, save more than face | BlogoSfera
    Eine Frage zu OpenCV @ coding-board - Das Forum für Programmierer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    3
    HI Salem,

    thanks for your reply.

    I did auto generated the code with a tool called Harpia, then take the source code and edited for my use.
    I didn't want to make anybody angry, but I think i diddnt anything wrong by using harpia, editing code and ask for help in a coding board.

    Sorry if thats wrong in your Eyes.

    edit: what you googled there is an question in stackoverflow from myself o0
    Last edited by suckabird; 06-23-2013 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c with opencv
    By smuthukumarbsc in forum C++ Programming
    Replies: 1
    Last Post: 09-01-2011, 10:31 PM
  2. opencv
    By Dibyayan Chakra in forum C Programming
    Replies: 1
    Last Post: 05-02-2011, 09:47 AM
  3. problem combining forms in visual c++ with opencv
    By Ben1220 in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2011, 02:23 AM
  4. OpenCV .....
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-05-2010, 07:41 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM