CIntImage_to_Featurevec.cpp
Go to the documentation of this file.
00001 /*
00002  * David Fischinger -TUW
00003  * 18.11.2011
00004  *
00005  * CLASS for calculating Feature Vector from Integral Images
00006  *
00007  * OUTDATED!!!!
00008  *
00009  * USAGE:
00010  *      - generate ListOfIIFilenames.txt in correct folder
00011  *      - change all Parameters
00012  *      - execute
00013  *
00014  *
00015  * input:
00016  *   Filenames (generated with: "ls pcd* -1 > ListOfIIFilenames.txt"
00017  *   executed in IntegralImages folder)
00018  *   with integral images and filename with features
00019  *
00020  * output:
00021  *   One file for each integral images including all feature values (1 value for each feature) is saved in the given output folder.
00022  *
00023  *   PARAMETERS:
00024  *
00025  *    HEIGHT 15 (14+1)
00026  *    WIDTH 15 (14+1)
00027  *    goodgps           indicates if features for good or bad GPs are calculated => label +1/-1 in output .txt
00028  */
00029 
00030 #include <CIntImage_to_Featurevec.h>
00031 #include <CHaarFeature.h>
00032 #include <iostream>
00033 #include <iomanip>
00034 
00035 #define HEIGHT 15
00036 #define WIDTH 15
00037 
00038 using namespace std;
00039 using namespace cv;
00040 
00041 
00042 CIntImage_to_Featurevec::CIntImage_to_Featurevec(){
00043         currentfeature = new CHaarFeature(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.1,1.0,1.0,2.0);
00044 }
00045 
00046 
00047 void CIntImage_to_Featurevec::read_features(string featurespath)
00048 {
00049         //PARAMETERS
00050         string path = featurespath;
00051 
00052         ifstream file_features;
00053         file_features.open(path.c_str());       //file_features: file with all features
00054         if (!file_features){
00055                 cout << "\n PROBLEM opening feature file: " << path.c_str() << endl;
00056         }
00057 
00058         string line;
00059         int id_x = -1;
00060         getline(file_features, line);
00061         while (file_features.good())
00062         {
00063                 int start = 0, end = 0;
00064                 int reg_c[16];  //region_corners
00065                 float reg_w[4]; //region_weights
00066                 id_x++;
00067                 for (int i = 0; i < 16; i++){
00068                         end = line.find("\t", start);
00069                         reg_c[i] = atoi(line.substr(start,end-start).c_str()) ;
00070                         start = end+1;
00071                 }
00072                 for (int j = 0; j < 4; j++){
00073                         end = line.find("\t", start);
00074                         reg_w[j] = atof(line.substr(start,end-start).c_str()) ;
00075                         start = end+1;
00076                 }
00077 
00078                 CHaarFeature * tmp_feature = new CHaarFeature(reg_c[0],reg_c[1],reg_c[2],reg_c[3], reg_c[4],reg_c[5],reg_c[6],reg_c[7],reg_c[8],reg_c[9],reg_c[10],reg_c[11],reg_c[12],reg_c[13],reg_c[14],reg_c[15],reg_w[0],reg_w[1],reg_w[2],reg_w[3]);
00079                 this->allfeatures.push_back(*tmp_feature);
00080 
00081                 getline(file_features, line);
00082         }
00083         file_features.close();
00084 }
00085 
00086 
00087 void CIntImage_to_Featurevec::print_features()
00088 {
00089 
00090         for (int i = 0; i < this->allfeatures.size(); i++)
00091         {
00092                 CHaarFeature curfeature = this->allfeatures.at(i);
00093                 for (int j = 0; j < 16; j++){
00094                         int reg_num = curfeature.regions.at(j);
00095                         cout << reg_num << "\t";
00096                 }
00097                 for (int j = 0; j < 4; j++){
00098                                         float reg_w = curfeature.weights.at(j);
00099                                         cout << reg_w << "\t";
00100                                 }
00101                 cout << i << "\n" ;
00102         }
00103 }
00104 
00105 
00106 void CIntImage_to_Featurevec::print_heights()
00107 {
00108     //print heights matrix
00109         cout << "print integral image matrix:" << endl;
00110         for (int i = 0; i < HEIGHT; i++){  //rows
00111                 for (int j = 0; j < WIDTH; j++){  //cols
00112                         cout << this->intimagemat[i][j] << "\t";
00113                 }
00114                 cout << "\n";
00115         }
00116         cout << "\n david: ";
00117         cout << this->currentfeature->nr_reg << endl;
00118 }
00119 
00120 
00121 
00122 void CIntImage_to_Featurevec::write_featurevector(string outputpath, int nr_features_without_shaf)
00123 {
00124     //open output file
00125         ofstream output_fv_file(outputpath.c_str(), fstream::app);
00126         if (this->goodgps)
00127                 output_fv_file << "+1";
00128         else
00129                 output_fv_file << "-1";
00130         //for all features
00131         for (int nr_feat = 0; nr_feat < this->allfeatures.size(); nr_feat++){
00132                 float featureval = calc_featurevalue(nr_feat, nr_features_without_shaf);
00133                 output_fv_file << " " << nr_feat+1 << ":" << setprecision(4) << featureval;
00134         }
00135         output_fv_file << "\n";
00136         output_fv_file.close();
00137 }
00138 
00139 
00140 
00141 float CIntImage_to_Featurevec::calc_featurevalue(int nr_feat, int nr_features_without_shaf)
00142 {
00143         float returnval = 0;
00144         this->currentfeature = &this->allfeatures.at(nr_feat);
00145         if (nr_feat < nr_features_without_shaf)
00146         {
00147                 for (int nr_reg = 0; nr_reg <4; nr_reg++){
00148                         //corners of region
00149                         int x1 = currentfeature->regions[nr_reg*4];
00150                         int x2 = currentfeature->regions[nr_reg*4+1];
00151                         int y1 = currentfeature->regions[nr_reg*4+2];
00152                         int y2 = currentfeature->regions[nr_reg*4+3];
00153                         float wgt = currentfeature->weights[nr_reg];    //weight of region
00154 
00155                         if (( wgt == 0.0 ) or           //region not used (weight equal 0)
00156                                 ( x2 < x1 ) or                  //r.x2 < r.x1
00157                                 ( y2 < y1 ) or                  //r.y2 < r.y1
00158                                 (x2 == 0 and y2 == 0))  //region corners are (0,0,0,0)
00159                                 continue;
00160 
00161                         returnval += wgt*(this->intimagemat[x2+1][y2+1] - this->intimagemat[x1][y2+1] -
00162                                                   this->intimagemat[x2+1][y1] + this->intimagemat[x1][y1]);
00163                 }
00164         } else {//new (symmetry) features
00165                 //new features to check if top and bottom of grasp area are symmetric
00166                 float r[3];     //r[0]: top field, r[1] middle, r[2]: bottom/down
00167                 r[0] = r[1] = r[2] = 0;
00168                 for (int nr_reg = 0; nr_reg <3; nr_reg++){ //assuming 3 regions
00169                         bool grasp_ok = true;
00170                         //corners of region
00171                         int x1 = currentfeature->regions[nr_reg*4];
00172                         int x2 = currentfeature->regions[nr_reg*4+1];
00173                         int y1 = currentfeature->regions[nr_reg*4+2];
00174                         int y2 = currentfeature->regions[nr_reg*4+3];
00175                         float wgt = currentfeature->weights[nr_reg];    //weight of region
00176 
00177                         if (( wgt == 0.0 ) or           //region not used (weight equal 0)
00178                                 ( x2 < x1 ) or                  //r.x2 < r.x1
00179                                 ( y2 < y1 ) or                  //r.y2 < r.y1
00180                                 (x2 == 0 and y2 == 0))  //region corners are (0,0,0,0)
00181                                 continue;
00182 
00183                         r[nr_reg] = wgt*(this->intimagemat[x2+1][y2+1] - this->intimagemat[x1][y2+1] -
00184                                                              this->intimagemat[x2+1][y1] + this->intimagemat[x1][y1]);
00185                 }
00186 
00187                 if (r[1] > r[0] and r[1] > r[2]){
00188                         returnval = min(r[1]-r[0], r[1]-r[2]);  //must be possitiv then
00189                 } else {
00190                         returnval = -1.0;
00191                 }
00192         }
00193         int cnt = 0;
00194         if (false /*nr_feat == 1*/){
00195                 this->print_heights();
00196                 cout << "cnt, feature, returnval: " << cnt++ << "\t"<< nr_feat << "  " << returnval << endl;
00197         }
00198         return returnval;
00199 }
00200 
00201 


haf_grasping
Author(s): David Fischinger
autogenerated on Thu Jun 6 2019 18:35:09