Ejemplo n.º 1
0
void CvFeatureEvaluator::init( const CvFeatureParams *_featureParams, int _maxSampleCount, Size _winSize )
{
  CV_Assert( _maxSampleCount > 0 );
  featureParams = (CvFeatureParams *) _featureParams;
  winSize = _winSize;
  numFeatures = _featureParams->numFeatures;
  cls.create( (int) _maxSampleCount, 1, CV_32FC1 );
  generateFeatures();
}
Ejemplo n.º 2
0
void Ferns::init(std::vector<cv::Size> const& scales)
{
    this->clear();
    generateFeatures(scales);
    
    acum = 0;
    thrN = 0.5 * nstructs;
    
    for (int i=0; i<nstructs; i++) {
        posteriors.push_back(std::vector<float>(std::pow(2.0, nBIT*structSize), 0));
        pCounter.push_back(std::vector<int>(std::pow(2.0, nBIT*structSize), 0));
        nCounter.push_back(std::vector<int>(std::pow(2.0, nBIT*structSize), 0));
    }
}
Ejemplo n.º 3
0
void CvHaarEvaluator::generateFeatures()
{
  generateFeatures( featureParams->numFeatures );
}
Ejemplo n.º 4
0
// my main function
int main(int argc, char** argv)
{
  // Main Variables
  int i, j;
  time_t t1, t2, t3, t4, t5, t6, t7, renderTime, deallocateTime;

  if (argc != 2) {
    fatal_error("usage: %s <PATH_FOR_IMAGES>", argv[0]);
  }

  // ************************TIME CHECKPOINT 1: Initialization****************************
  (void) time(&t1);
  printf("Starting Checkpoint 1 - initialization...\n");

  // Variables
  int file_count = 0;
  char** filenames = NULL;
  struct pData* poses = NULL;

  FILE *poseFile;
  char poseFilename[strlen(argv[1]) + strlen("poses.txt")];
  sprintf(poseFilename, "%sposes.txt", argv[1]);
    
  poseFile = fopen(poseFilename, "r");

  if (poseFile != NULL) {
    int c, step = 0;    
    while (file_count < NUM_IMAGES) {
      filenames = (char **) realloc(filenames, (file_count+1) * sizeof(char *));
      poses = (struct pData*) realloc(poses, (file_count+1) * sizeof(struct pData));

      if ((step % STEP) == 0) {
	initialize(poseFile, poses, filenames, file_count);
	file_count++;
	step++;
      } else {
	while((c = fgetc(poseFile)) != '\n');
	step++;
      }
    }      
  }

  // ************************TIME CHECKPOINT 2: Loading Image Data***************************
  (void) time(&t2);
  printf("Starting Checkpoint 2 - loading images from directory...\n");

  // Variables
  int image_count = 0;
  IplImage** images = NULL;

  images = (IplImage**) malloc(file_count * sizeof(IplImage*));
  // ERROR check allocation
  if (images == NULL) {
    fatal_error("allocating memory for images");
  }

  // load images
  memset(images, 0, file_count*sizeof(IplImage*));  
  image_count = loadImages(argv[1], filenames, file_count, images);

  // Release the memory for filenames.  Not needed anymore.
  free(filenames);

  // ************************TIME CHECKPOINT 3: Finding Features*****************************
  (void) time(&t3);
  printf("Starting Checkpoint 3 - finding features in each image...\n");

  // Step 1: generate features form images
  generateFeatures(images, image_count);

  // ************************TIME CHECKPOINT 4: Calculating Homographies*********************
  (void) time(&t4);
  printf("Starting Checkpoint 4 - matching images...\n");

  // A match represents a homography between two images
  int match_count = 0;
  CvMat** matches = (CvMat **) malloc(image_count * image_count * sizeof(CvMat*));

  // ERROR check allocation
  if (matches == NULL) {
    fatal_error("allocating memory for matches");
  }

  // Calculate homographies
  memset(matches, 0, image_count*image_count*sizeof(CvMat*));  
  match_count = enumerateMatches(images, image_count, matches);

  // ************************TIME CHECKPOINT 5: Calculating Norms of Homographies************
  (void) time(&t5);
  printf("Starting Checkpoint 5 - calculating norms of homography...\n");

  // The confidence is a measurement of similarity between images (i.e. norm of homography)
  double* confidences = (double*) calloc(match_count, sizeof(double));

  // ERROR check allocation
  if (confidences == NULL) {
    fatal_error("allocating memory for confidences");
  }
  
  calculateNorms(confidences, matches, image_count, match_count);

  // ************************TIME CHECKPOINT 6: Generating Best Homographies************
  (void) time(&t6);
  printf("Starting Checkpoint 6 - Generating best homographies...\n");

  // Variables
  int* bestMatchedIndex = (int*) calloc(image_count, sizeof(int));
  generateBestHomographies(bestMatchedIndex, image_count, confidences);

  // ************************Render Checkpoint: Rendering Scene****************************
  (void) time(&renderTime);
  printf("Starting Render Checkpoint - Rendering...\n");

  // Variables
  IplImage* viewport;
  clock_t currtime;
  int key;
  
  myScene = (struct scene*) malloc(sizeof(struct scene));
  initScene(0, myScene, poses, image_count);

  cvNamedWindow("Scene", 1);
  cvMoveWindow("Scene", 560, 200);

  rightClicked = 0;
  leftClicked = 0;
  middleClicked = 0;
  mousePosX = 0;
  mousePosY = 0;
  cvSetMouseCallback("Scene", mouseHandler, 0);
  viewport = cvCreateImage(cvSize(WINDOW_WIDTH, WINDOW_HEIGHT), IPL_DEPTH_8U, 3);
  
  while(1) {
    currtime = clock();

    if (!OPTION) {
      cvSetZero(viewport);
      //cvReleaseImage(&viewport);
      //viewport = cvCreateImage(cvSize(WINDOW_WIDTH, WINDOW_HEIGHT), IPL_DEPTH_8U, 3);
    }

    renderScene(images, matches, bestMatchedIndex, viewport, poses);
    cvShowImage("Scene", viewport);

    key = cvWaitKey(10);
    if (key == 'e' || key == 'E' || key == 27) {
      printf("EXITING!!!\n");
      break;
    } else if (key == 'r' || key == 'R') {
      initScene(0, myScene, poses, image_count);
    }else if (OPTION) {
      updateSceneKey(key);
    } 

    //if (DEBUG) 
      printf("time for frame: %.2lf\n", (((double) 1.0*clock()) - currtime) / CLOCKS_PER_SEC);
  }
  cvReleaseImage(&viewport);
  
  // ************************DEALLOCATION CHECKPOINT: Deallocating Memory******************
  (void) time(&deallocateTime);
  printf("Starting Deallocation Checkpoint - Deallocating...\n");

  // free allocated data 
  // free CHECKPOINT 7
  free(myScene);

  // free CHECKPOINT 6
  free(bestMatchedIndex);

  // free CHECKPOINT 5
  free(confidences);

  // free CHECKPOINT 3 and 4
  for (i = 0; i < match_count; i++) {
    cvReleaseMat(&matches[i]);
  }
  free(matches);  
  
  // free CHECKPOINT 2
  for (i = 0; i < image_count; i++) {
    cvReleaseImage(&images[i]);
  }
  free(images);

  // free CHECKPOINT 1
  free(poses);
 
  // Print Statistics
  printf("\n");
  printf("Number of images: %d\n", image_count);
  printf("Time for initialization: %.2lf\n", difftime(t2,t1));
  printf("Time to load data: %.2lf\n", difftime(t3,t2));
  printf("Time to generate features: %.2lf\n", difftime(t4,t3));
  printf("Time to generate matches: %.2lf\n", difftime(t5,t4));
  printf("Time to generate confidence/norms: %.2lf\n", difftime(t6, t5));
  printf("Time to search for optimal homographies: %.2lf\n", difftime(renderTime, t6));
  printf("Time for rendering: %.2lf\n", difftime(deallocateTime,renderTime));
  printf("Total Time: %.2lf\n", difftime(deallocateTime, t1));
     
  return 0;

}
Ejemplo n.º 5
0
void ICFDetector::train(const vector<string>& image_filenames,
                        const vector< vector<Rect> >& labelling,
                        ICFDetectorParams params)
{
    Size model_size(params.model_n_cols, params.model_n_rows);

    vector<Mat> samples; /* positive samples + negative samples */
    Mat sample, resized_sample;
    int pos_count = 0;
    for( size_t i = 0; i < image_filenames.size(); ++i, ++pos_count )
    {
        Mat img = imread(String(image_filenames[i].c_str()));
        for( size_t j = 0; j < labelling[i].size(); ++j )
        {
            Rect r = labelling[i][j];
            if( r.x > img.cols || r.y > img.rows )
                continue;

            sample = img.colRange(max(r.x, 0), min(r.width, img.cols))
                        .rowRange(max(r.y, 0), min(r.height, img.rows));

            resize(sample, resized_sample, model_size);

            samples.push_back(resized_sample);
        }
    }

    int neg_count = 0;
    RNG rng;
    for( size_t i = 0; i < image_filenames.size(); ++i )
    {
        Mat img = imread(String(image_filenames[i].c_str()));
        for( int j = 0; j < (int)(pos_count / image_filenames.size() + 1); )
        {
            Rect r;
            r.x = rng.uniform(0, img.cols);
            r.width = rng.uniform(r.x + 1, img.cols);
            r.y = rng.uniform(0, img.rows);
            r.height = rng.uniform(r.y + 1, img.rows);

            if( !overlap(r, labelling[i]) )
            {
                sample = img.colRange(r.x, r.width).rowRange(r.y, r.height);
                //resize(sample, resized_sample);
                samples.push_back(resized_sample);
                ++neg_count;
                ++j;
            }
        }
    }

    Mat_<int> labels(1, pos_count + neg_count);
    for( int i = 0; i < pos_count; ++i)
        labels(0, i) = 1;
    for( int i = pos_count; i < pos_count + neg_count; ++i )
        labels(0, i) = -1;

    vector<Point3i> features = generateFeatures(model_size);
    Ptr<ACFFeatureEvaluator> feature_evaluator = createACFFeatureEvaluator(features);

    Mat_<int> data((int)features.size(), (int)samples.size());
    Mat_<int> feature_col;

    vector<Mat> channels;
    for( int i = 0; i < (int)samples.size(); ++i )
    {
        computeChannels(samples[i], channels);
        feature_evaluator->setChannels(channels);
        feature_evaluator->evaluateAll(feature_col);
        for( int j = 0; j < feature_col.rows; ++j )
            data(i, j) = feature_col(0, j);
    }

    WaldBoostParams wparams;
    wparams.weak_count = params.weak_count;
    wparams.alpha = 0.001f;

    Ptr<WaldBoost> waldboost = createWaldBoost(wparams);
    waldboost->train(data, labels);
}