changeset 20:ef0d7caf8e91

draft code for feature saving (UBC format)
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Sat, 28 Nov 2009 01:56:46 -0500
parents 5a21d2cfee44
children 3c4629550f5f
files c/track-features.cpp c/utils.cpp include/utils.hpp
diffstat 3 files changed, 75 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/c/track-features.cpp	Fri Nov 27 19:16:12 2009 -0500
+++ b/c/track-features.cpp	Sat Nov 28 01:56:46 2009 -0500
@@ -142,25 +142,36 @@
   fnameout = "features.txt";//"features-"%s"-"%d"-"%d"-"%d".txt", sequenceFile, nFeatures, tc->mindist, tc->window_width);
   //KLTWriteFeatureTable(ft, fnameout, "%5.1f");
 
-  out = fopen(fnameout.c_str(),"w");
-  if(out!=NULL){
-    fprintf(out,"%%StartFrame: 0\n");
-    fprintf(out,"%%NumFrames: %d\n",nFrames);
+  int precision = 2;
+  ofstream out;
+  openWriteScientific(out, fnameout, precision);//out = fopen(fnameout.c_str(),"w");
+
+  //fprintf(out,"%%StartFrame: 0\n");
+  //fprintf(out,"%%NumFrames: %d\n",nFrames);
     
-    // 1 feature / line
-    // x1 y1 val1 x2 y2 val2 ... (1 2... frame numbers)
-    for (j = 0 ; j < ft->nFeatures ; j++)  {
-      fprintf(out, "\n");
-      for (i = 0 ; i < ft->nFrames ; i++){
-        feature = ft->feature[j][i];
-	fprintf(out,"%.2f %.2f %d ",(float)feature->x,(float)feature->y,FEAT_VAL(feature->val));
-      }
-    }      
+  // 1 feature / line
+  // x1 y1 val1 x2 y2 val2 ... (1 2... frame numbers)
+  for (j = 0 ; j < ft->nFeatures ; j++)  {
+    int firstFrameNum = startFrame;
+    stringstream lx, ly;
+    for (i = 0 ; i < ft->nFrames ; i++){
+      feature = ft->feature[j][i];
+      //fprintf(out,"%.2f %.2f %d ",(float)feature->x,(float)feature->y,FEAT_VAL(feature->val));
+      lx << feature->x << " ";
+      ly << feature->y << " ";
+      if (feature->val <= 0) {
+	// print the feature
+	out << firstFrameNum <<  " " << startFrame+i << endl;
+	out << lx << endl;
+	out << ly << endl;
+	// velocity
+	out << "%" << endl;
+	firstFrameNum = i+1;
+	lx.clear();
+	ly.clear();
+    }
     
-    fclose(out);
-  }        
-  else
-    printf("\nError opening feature file\n");
+    //fclose(out);
 
   cvReleaseCapture(&sequence);
 
--- a/c/utils.cpp	Fri Nov 27 19:16:12 2009 -0500
+++ b/c/utils.cpp	Sat Nov 28 01:56:46 2009 -0500
@@ -1,1 +1,33 @@
-// 
+#include "utils.hpp"
+
+#include <iostream>
+#include <fstream>
+
+using namespace std;
+
+void openWriteScientificPrecision(ofstream& out, const string& filename, const int& precision) {
+  ::openWritePrecision(out, filename, precision);
+  out.setf(ios::scientific);
+}
+
+void openWritePrecision(ofstream& out, const string& filename, const int& precision) {
+  out.open(filename.c_str(), ios::binary);
+  ::checkFileOpening(out, filename, "openWritePrecision");
+  out.precision(precision);
+}
+
+bool openCheck(ifstream& f, const string& filename, const string& callingFunctionName) {
+  if (!f.is_open()) {
+    cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl;
+    return false;
+  } else
+    return true;
+}
+
+bool openCheck(ofstream& f, const string& filename, const string& callingFunctionName) {
+  if (!f.is_open()) {
+    cerr << "Pb opening file " << filename << " in " << callingFunctionName << endl;
+    return false;
+  } else
+    return true;
+}
--- a/include/utils.hpp	Fri Nov 27 19:16:12 2009 -0500
+++ b/include/utils.hpp	Sat Nov 28 01:56:46 2009 -0500
@@ -1,8 +1,22 @@
 #ifndef UTILS_HPP
 #define UTILS_HPP
 
+#include <iofwd>
+
 static const double pi = 3.14159265358979323846;
 
+/** Opens file for writing with fixed scientific precision. */
+void openWriteScientific(ofstream& out, const string& filename, const int& precision);
+
+void openWritePrecision(ofstream& out, const string& filename, const int& precision);
+
+/** Opens files and checks how it went. */
+bool openCheck(ifstream& f, const string& filename, const string& callingFunctionName);
+bool openCheck(ofstream& f, const string& filename, const string& callingFunctionName);
+
+
+// inline 
+
 inline double square(const int& a) { return a*a;}
 
 /** Implements key bindings, for example for cvWaitKey(). */