Esempio n. 1
0
int main(int argc, char* argv[])
{
    if (argc < 6) {
        std::cout << "Usage: " << argv[0] << " in out-image out-csv threshold [sort-mode]\n";
        std::cout << "  sort-mode: sort-by-color, sort-by-count\n";
        return 0;
    }

    cv::Mat Z;
    std::string f = argv[1];
    if (fs::is_directory(f)) {
        Z = loadThumbnails(f);
    } else if (fs::path(f).extension() == ".png" || fs::path(f).extension() == ".jpg") {
        Z = loadImage(f);
    } else {
        Z = loadVideo(f);
    }

    std::cout << "Clustering...\n";
    std::vector<Cluster> centers;
    if (std::string(argv[4]).find(".") == std::string::npos) {
        cluster(Z, (int)atoi(argv[4]), centers);
    } else {
        cluster(Z, (float)atof(argv[4]), centers);
    }

    std::string sort_mode = argc >= 6 ? argv[5] : "sort-by-count";
    if (sort_mode == "sort-by-color") {
        sortCentersByColor(centers);
    } else {
        sortCentersByCount(centers);
    }
    exportCenters(centers, argv[2], argv[3]);
}
Esempio n. 2
0
/**
 * \fn void createTrainingMeans(std::string stipFile, int dim, int maxPts, int k, std::string meansFile)
 * \brief Import HOG and HOF from a file and compute KMeans algorithm to create
 * the file training.means.
 * 
 * \param[in] stipFile The file containing the STIPs.
 * \param[in] dim Points and centers's dimension.
 * \param[in] maxPts The maximum number of data we can compute
 * \param[in] k The number of centers
 * \param[out] meansFile The file in wich we will save the KMeans centers.
 */
void createTrainingMeans(std::string stipFile,
			 int dim,
			 int maxPts,
			 int k,
                         std::string meansFile
                         ){

  KMdata dataPts(dim,maxPts);
  int nPts = importSTIPs(stipFile, dim, maxPts, &dataPts);
  dataPts.setNPts(nPts);
  dataPts.buildKcTree();
  
  KMfilterCenters ctrs(k, dataPts);    
  
  int ic = 3;
  kmIvanAlgorithm(ic, dim, dataPts, k, ctrs);  
  
  exportCenters(meansFile, dim, k, ctrs);
}
Esempio n. 3
0
int im_create_specifics_training_means(IMbdd bdd,
				       const std::vector<std::string>& trainingPeople 
				       //std::vector <std::string> rejects
				       ){
  std::string path2bdd(bdd.getFolder());
  int dim = bdd.getDim();
  int maxPts = bdd.getMaxPts();
  
  std::vector <std::string> activities = bdd.getActivities();
  int nr_class = activities.size();
  
  int nr_people = trainingPeople.size();
  
  // The total number of centers
  int k = bdd.getK();
  int subK = k/nr_class;
  if(k%nr_class != 0){
    std::cerr << "K is no divisible by the number of activities !!" << std::endl;
    exit(EXIT_FAILURE);
  }
  double ***vDataPts = new double**[nr_class];
  int nrFP[nr_class]; // number of feature points for each class
  int currentActivity = 0;
  // For each activity
  for(std::vector<std::string>::iterator activity = activities.begin() ;
      activity != activities.end() ;
      ++activity){
    nrFP[currentActivity] = 0;
    
    // We concatenate all the training people
    int nrFPpP[nr_people]; // number of feature points per person
    double*** activityDataPts = new double**[nr_people];
    int currentPerson = 0;
    for(std::vector<std::string>::const_iterator person = trainingPeople.begin() ;
	person != trainingPeople.end() ;
	++person){
      nrFPpP[currentPerson] = 0;
      std::string rep(path2bdd + "/" + *person + "/" + *activity);
      DIR * repertoire = opendir(rep.c_str());
      if (!repertoire){
	std::cerr << "Impossible to open the feature points directory!" << std::endl;
	exit(EXIT_FAILURE);
      }
      
      // Checking that the file concatenate.<activity>.fp exists
      struct dirent * ent = readdir(repertoire);
      std::string file(ent->d_name);
      while (ent && (file.compare("concatenate." + *activity + ".fp")) != 0){
	ent = readdir(repertoire);
	file = ent->d_name;
      }
      if(!ent){
	std::cerr << "No file concatenate.<activity>.fp" << std::endl;
	exit(EXIT_FAILURE);
      }
      
      // Importing the feature points
      std::string path2FP(rep + "/" + file);
      KMdata kmData(dim,maxPts);
      nrFPpP[currentPerson] = importSTIPs(path2FP, dim, maxPts, &kmData);
      if(nrFPpP[currentPerson] != 0){
	activityDataPts[currentPerson] = new double*[nrFPpP[currentPerson]];
	for(int n = 0 ; n<nrFPpP[currentPerson] ; n++){
	  activityDataPts[currentPerson][n] = new double [dim];
	  for(int d = 0 ; d<dim ; d++)
	    activityDataPts[currentPerson][n][d] = kmData[n][d];
	}
      } // else the current person does not participate in this activity
      nrFP[currentActivity] += nrFPpP[currentPerson];
      currentPerson++;
    } // ++person
    
    // Saving people in vDataPts
    vDataPts[currentActivity] = new double*[nrFP[currentActivity]];
    int index=0;
    for(int p=0 ; p<nr_people ; p++){
      for(int fp=0 ; fp<nrFPpP[p] ; fp++){
	vDataPts[currentActivity][index] = new double[dim];
	for(int d=0 ; d<dim ; d++){
	  vDataPts[currentActivity][index][d] = activityDataPts[p][fp][d];
	}
	index++;
      }
    } // index must be equal to nrFP[currentActivity] - 1
    
    // Deleting activityDataPts
    for(int p=0 ; p<nr_people ; p++){
      for(int fp=0 ; fp < nrFPpP[p] ; fp++)
	delete[] activityDataPts[p][fp];
      delete[] activityDataPts[p];
    }
    delete[] activityDataPts;
    currentActivity++;
  } // ++activity
  
  // Total number of feature points
  int ttFP = 0;
  for(int a=0 ; a<nr_class ; a++){
    ttFP += nrFP[a];
  }
  
  // Memory allocation of the centers
  double** vCtrs = new double*[k];
  for(int i=0 ; i<k ; i++){
    vCtrs[i] = new double[dim];
  }
  
  // Doing the KMeans algorithm for each activities
  int ic = 3; // the iteration coefficient (Ivan's algorithm)
  int currCenter = 0;
  for(int i=0 ; i<nr_class ; i++){
    KMdata kmData(dim,nrFP[i]);
    for(int n=0 ; n<nrFP[i]; n++)
      for(int d=0 ; d<dim ; d++)
	kmData[n][d] = vDataPts[i][n][d];
    kmData.setNPts(nrFP[i]);
    kmData.buildKcTree();
    KMfilterCenters kmCtrs(subK,kmData);
    kmIvanAlgorithm(ic, dim, kmData, subK, kmCtrs);
    for(int n=0 ; n<subK ; n++){
      for(int d=0 ; d<dim ; d++){
	vCtrs[currCenter][d] = kmCtrs[n][d];
      }
      currCenter++;
    }
  }
  
  std::cout << "Concatenate KMdata" << std::endl;
  // Concatenate all the KMdata
  /* In reality it is not necessary
     but the objectif KMfilterCenters must be initialized with
     the KMdata */
  KMdata dataPts(dim,ttFP);
  int nPts = 0;
  for (int i=0 ; i<nr_class ; i++){
    for(int n=0 ; n<nrFP[i]; n++){
      for(int d=0 ; d<dim ; d++){
	dataPts[nPts][d] = vDataPts[i][n][d];
      }
      nPts++;
    }
  }
  // Releasing vDataPts
  for(int i=0 ; i<nr_class ; i++){
    for(int n=0 ; n<nrFP[i] ; n++)
      delete [] vDataPts[i][n];
    delete [] vDataPts[i];
  }
  delete[] vDataPts;
  
  dataPts.buildKcTree();
  // Returning the true centers
  KMfilterCenters ctrs(k,dataPts);
  for(int n=0 ; n<k ; n++){
    for(int d=0 ; d<dim ; d++){
      ctrs[n][d] = vCtrs[n][d];
    }
  }
  
  // Releasing vCtrs
  for(int i=0 ; i<k ; i++)
    delete [] vCtrs[i];
  delete[]vCtrs;
  
  exportCenters(bdd.getFolder() + "/" + bdd.getKMeansFile(),
		dim, k, ctrs);
  
  return k;
}