Exemplo n.º 1
0
void KinectGrabber::extractFaceLoop() {
	while (isConnected()) {
		extractFace();
	}
}
Exemplo n.º 2
0
/*....................................................................*/
int
buildRayCellChain(const int numDims, double *x, double *dir, double *vertexCoords\
  , struct simplex *dc, _Bool **cellVisited, unsigned long dci, int entryFaceI\
  , int levelI, int nCellsInChain, const double epsilon\
  , faceType **facePtrs[N_DIMS+1], unsigned long **chainOfCellIds, intersectType **cellExitIntcpts\
  , int *lenChainPtrs){
  /*
This function is designed to follow a ray (defined by a starting locus 'x' and a direction vector 'dir') through a convex connected set of cells (assumed simplicial). The function returns an integer status value directly, and two lists (plus their common length) via the argument interface: chainOfCellIds and cellExitIntcpts. Taken together, these lists define a chain of cells traversed by the ray.

The task of determining which cells are traversed by the ray is simple in principle, but complications arise in computational practice due to the finite precision of floating-point calculations. Where the ray crosses a cell face near to one of its edges, numerical calculation of the 'impact parameter' may return an answer which is erroneous either way: i.e., a ray which just misses a face may be reported as hitting it, and vice versa. To deal with this, a distinction is made between impacts which are (i) 'good', that is far from any face edge; (ii) 'bad', i.e. clearly missing the face; and (iii) 'marginal', i.e. closer to the edge than some preset cutoff which is represented in the argument list by the number 'epsilon'. Note that a tally is kept of those cells which have already been tested for the current ray, and any face which abuts a neighbouring cell which has been visited already will be flagged as 'bad'.

The function therefore looks at all the exit faces of the cell and sorts them into these three categories. How it proceeds then depends on the relative numbers of each, as described below.

	- If there is more than 1 good exit face, an exception is generated. This is a sign that epsilon has been chosen with too small a value.

	- If there is just 1 good exit face, the marginal ones are ignored. The current cell data are appended to the chain and the cell abutting the exit face becomes the new working cell.

	- If there are no good exit faces, we look at the marginal ones. If there is only 1 of these, we loop as before. If there are more than 1, the function is called recursively for each cell on the far side of an exit face.

Thus there are two alternate modes of operation within the function: a straightforward loop along the cells in a single chain, which will continue so long as there is only a single exit face of type either 'good' or 'marginal'; and a recursive launch of the function at a fork in the chain into each of its several possible branches.

The function terminates under the following conditions:
	- It detects that the edge of the model is reached (returns success).
	- There are no exit faces (returns failure).
	- There are no good exit faces and either
		* all of the recursive calls to marginal faces have been unsuccessful (returns failure), or
		* one of these has been successful (returns success).

At a successful termination, therefore, details of all the cells to the edge of the model are correctly stored in chainOfCellIds and cellExitIntcpts, and the number of these cells is returned in lenChainPtrs.

***** Note that it is assumed here that a mis-indentification of the actual cell traversed by a ray will not ultimately matter to the external calling routine. This is only reasonable if whatever function or property is being sampled by the ray does not vary in a stepwise manner at any cell boundary. *****
  */

  const int numFaces=numDims+1;
  _Bool followingSingleChain;
  const int bufferSize=1024;
  int numGoodExits,numMarginalExits,fi,goodExitFis[numFaces],marginalExitFis[numFaces],exitFi,i,status,newEntryFaceI;
  faceType face;
  intersectType intcpt[numFaces];

  followingSingleChain = 1; /* default */
  do{ /* Follow the chain through 'good' cells, i.e. ones for which entry and exit are nicely distant from face edges. (Here we also follow marginal exits if there are no good ones, and only 1 marginal one.) */
    (*cellVisited)[dci] = 1;

    /* If there is not enough room in chainOfCellIds and cellExitIntcpts, realloc them to new value of lenChainPtrs. */
    if(nCellsInChain>=(*lenChainPtrs)){
      *lenChainPtrs += bufferSize;
      *chainOfCellIds  = realloc(*chainOfCellIds,  sizeof(**chainOfCellIds) *(*lenChainPtrs));
      *cellExitIntcpts = realloc(*cellExitIntcpts, sizeof(**cellExitIntcpts)*(*lenChainPtrs));
    }

    /* Store the current cell ID (we leave storing the exit face for later, when we know what it is). */
    (*chainOfCellIds)[nCellsInChain] = dci;

    /* calculate num good and bad exits */
    numGoodExits = 0;
    numMarginalExits = 0;
    for(fi=0;fi<numFaces;fi++){
      if(fi!=entryFaceI && (dc[dci].neigh[fi]==NULL || !(*cellVisited)[dc[dci].neigh[fi]->id])){
        /* Store points for this face: */
        if(facePtrs==NULL){
          face = extractFace(numDims, vertexCoords, dc, dci, fi);
        }else{
          face = (*facePtrs)[dci][fi];
        }

        /* Now calculate the intercept: */
        intcpt[fi] = intersectLineWithFace(numDims, x, dir, &face, epsilon);
        intcpt[fi].fi = fi; /* Ultimately we need this so we can relate the bary coords for the face back to the Delaunay cell. */

        if(intcpt[fi].orientation>0){ /* it is an exit face. */
          if(intcpt[fi].collPar-epsilon>0.0){
            goodExitFis[numGoodExits] = fi;
            numGoodExits++;
          }else if (intcpt[fi].collPar+epsilon>0.0){
            marginalExitFis[numMarginalExits] = fi;
            numMarginalExits++;
          }
        }
      }
    }

    if(numGoodExits>1){
      error(RTC_ERR_BUG, "Some sort of bug: more than 1 firm candidate found for ray exit from cell.");

    }else if(numGoodExits==1 || numMarginalExits==1){
      if(numGoodExits==1)
        exitFi = goodExitFis[0];
      else
        exitFi = marginalExitFis[0];

      /* Store the exit face details: */
      (*cellExitIntcpts)[nCellsInChain] = intcpt[exitFi];

      nCellsInChain++;

      if(dc[dci].neigh[exitFi]==NULL){ /* Signals that we have reached the edge of the model. */
        /* Realloc the ptrs to their final sizes: */
        *chainOfCellIds  = realloc(*chainOfCellIds,  sizeof(**chainOfCellIds) *nCellsInChain);
        *cellExitIntcpts = realloc(*cellExitIntcpts, sizeof(**cellExitIntcpts)*nCellsInChain);
        *lenChainPtrs = nCellsInChain;

        return 0;
      }

      entryFaceI = getNewEntryFaceI(numDims, dci, *(dc[dci].neigh[exitFi]));
      dci = dc[dci].neigh[exitFi]->id;

    }else{
      followingSingleChain = 0;
    }
  }while(followingSingleChain);

  /* Now we have run out of good (or at least single) exit-face options, let's try the marginal ones. */

  if(numMarginalExits<1)
    return 3; /* Unsuccessful end of this chain. */

  /* If we have got to this point, we must have numMarginalExits>1; thus we have a fork in the chain, and must explore each branch. We recurse here because a recursive scheme is the best way to do that. */

  i = 0;
  status = 4; /* default */
  while(i<numMarginalExits && status>0){
    exitFi = marginalExitFis[i];
    (*cellExitIntcpts)[nCellsInChain] = intcpt[exitFi];

    if(dc[dci].neigh[exitFi]==NULL){ /* Signals that we have reached the edge of the model. */
      /* Realloc the ptrs to their final sizes: */
      nCellsInChain++;
      *chainOfCellIds  = realloc(*chainOfCellIds,  sizeof(**chainOfCellIds) *nCellsInChain);
      *cellExitIntcpts = realloc(*cellExitIntcpts, sizeof(**cellExitIntcpts)*nCellsInChain);
      *lenChainPtrs = nCellsInChain;

      status = 0;

    }else{
      newEntryFaceI = getNewEntryFaceI(numDims, dci, *(dc[dci].neigh[exitFi]));

      /* Now we dive into the branch: */
      status = buildRayCellChain(numDims, x, dir, vertexCoords, dc, cellVisited\
        , dc[dci].neigh[exitFi]->id, newEntryFaceI, levelI+1, nCellsInChain+1\
        , epsilon, facePtrs, chainOfCellIds, cellExitIntcpts, lenChainPtrs);
    }
    i++;
  }

  return status;
}
Exemplo n.º 3
0
void initialize(int argc, char** argv ){
	IplImage* image;
    // 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 = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }else{
    	cerr<< cascade_name << " Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n"<<endl;
    	throw BAD_ARGUMENT();;
    }

    // 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 ){
        cerr<<"ERROR: Could not load classifier cascade\n"<<endl;
        throw BAD_ARGUMENT();
    }

    // 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 );
    cvWaitKey(2000);
    //cvSetCaptureProperty(capture, CV_CAP_PROP_POS_AVI_RATIO, (double)0.9);
    //cvSetCaptureProperty(capture,CV_CAP_PROP_FORMAT,1);
    //cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 256);
    //cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 256);
    // 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
            	cvCopy( frame, frame_copy, 0 );

            extractFace(frame_copy,cascade);
            //cvShowImage("Result",frame_copy);
            // Wait for a while before proceeding to the next frame
//            cvWaitKey(20);
            if( cvWaitKey( 20 ) == 27 ) //exit if Esc is pressed(repeatedly)
                break;
        }

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

    // If the capture is not loaded successfully, 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
        image = cvLoadImage( filename, 1 );

        // If Image is loaded successfully, then:
        if( image ){
        	extractFace(image,cascade);
        	//cvShowImage("result",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 successfully, then:
                    if( image ){
						//return image;
                    	//cvShowImage("result",image);
                    	extractFace(image,cascade);
                        // Wait for the user input, and release the memory
                        //cvWaitKey(0);
                        //cvReleaseImage( &image );
                    }
                }
                // Close then file
                fclose(f);
            }
        }
    }
    // Destroy the window previously created with filename: "result"
    //cvDestroyWindow("result");
}