00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00050 string path = featurespath;
00051
00052 ifstream file_features;
00053 file_features.open(path.c_str());
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];
00065 float reg_w[4];
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
00109 cout << "print integral image matrix:" << endl;
00110 for (int i = 0; i < HEIGHT; i++){
00111 for (int j = 0; j < WIDTH; j++){
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
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
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
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];
00154
00155 if (( wgt == 0.0 ) or
00156 ( x2 < x1 ) or
00157 ( y2 < y1 ) or
00158 (x2 == 0 and y2 == 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 {
00165
00166 float r[3];
00167 r[0] = r[1] = r[2] = 0;
00168 for (int nr_reg = 0; nr_reg <3; nr_reg++){
00169 bool grasp_ok = true;
00170
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];
00176
00177 if (( wgt == 0.0 ) or
00178 ( x2 < x1 ) or
00179 ( y2 < y1 ) or
00180 (x2 == 0 and y2 == 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]);
00189 } else {
00190 returnval = -1.0;
00191 }
00192 }
00193 int cnt = 0;
00194 if (false ){
00195 this->print_heights();
00196 cout << "cnt, feature, returnval: " << cnt++ << "\t"<< nr_feat << " " << returnval << endl;
00197 }
00198 return returnval;
00199 }
00200
00201