Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

strk_io.cxx

Go to the documentation of this file.
00001 // This is brl/bseg/strk/strk_io.cxx
00002 // not used? #include <vcl_iostream.h>
00003 #include <vcl_string.h>
00004 #include <vnl/vnl_matrix.h>
00005 #include <vnl/vnl_matlab_print2.h>
00006 #include <vgl/vgl_point_2d.h>
00007 #include <vtol/vtol_vertex.h>
00008 #include <vtol/vtol_vertex_2d.h>
00009 #include <vtol/vtol_face_2d.h>
00010 #include <strk/strk_tracking_face_2d.h>
00011 #include "strk_io.h"
00012 
00013 bool strk_io::write_frame_data(const unsigned int start_frame,
00014                                const unsigned int n_frames,
00015                                vcl_ofstream& strm)
00016 {
00017   if (!strm)
00018     return false;
00019   strm << "START_FRAME: " << start_frame << '\n'
00020        << "N_FRAMES: " << n_frames << '\n';
00021   return true;
00022 }
00023 
00024 bool strk_io::write_region_data(const unsigned int n_pix, const float diameter,
00025                                 const float aspect_ratio,
00026                                 vcl_ofstream& strm)
00027 {
00028  if (!strm)
00029     return false;
00030 
00031  strm << "N_PIXELS: " << n_pix << '\n'
00032       << "DIAMETER: " << diameter << '\n'
00033       << "ASPECT_RATIO: " << aspect_ratio << '\n';
00034  return true;
00035 }
00036 
00037 bool strk_io::write_histogram_data(const unsigned int start_frame,
00038                                    const unsigned int n_pixels,
00039                                    const float diameter,
00040                                    const float aspect_ratio,
00041                                    const unsigned int n_int_bins,
00042                                    const unsigned int n_grad_dir_bins,
00043                                    const unsigned int n_color_bins,
00044                                    vcl_vector<vcl_vector<float> >const& data, 
00045                                    vcl_ofstream& strm)
00046 {
00047   unsigned int n_frames = data.size();
00048   if (!n_frames)
00049     return false;
00050   //checks stream
00051   if (!strk_io::write_frame_data(start_frame, n_frames, strm))
00052     return false;
00053   if (!strk_io::write_region_data(n_pixels, diameter, aspect_ratio, strm))
00054     return false;
00055   strm << "N_INTENSITY_BINS: " << n_int_bins << '\n'
00056        << "N_GRADIENT_DIR_BINS: " << n_grad_dir_bins << '\n'
00057        << "N_COLOR_BINS: " << n_color_bins << '\n'
00058        << "HISTOGRAMS: " << '\n';
00059   unsigned int nbins = n_int_bins + n_grad_dir_bins + n_color_bins;
00060   vnl_matrix<float> temp(n_frames, nbins);
00061   for (unsigned int i = 0;i<n_frames; ++i)
00062     for (unsigned int j = 0; j<nbins; ++j)
00063       temp[i][j]=data[i][j];
00064   strm << temp << '\n';
00065   return true;
00066 }
00067 
00068 bool strk_io::
00069 write_track_data(const unsigned int start_frame,
00070                  vcl_vector<vtol_face_2d_sptr> const& tracked_faces,
00071                  vcl_ofstream& strm)
00072 {
00073   unsigned int n_frames = tracked_faces.size();
00074   if (!n_frames)
00075     return false;
00076   //this call checks the stream
00077   if (!write_frame_data(start_frame, n_frames, strm))
00078     return false;
00079   vcl_vector<vtol_vertex_sptr> verts;
00080   tracked_faces[0]->vertices(verts);
00081   unsigned int n_verts = verts.size();
00082   if (!n_verts)
00083     return false;
00084   strm << "N_VERTS: " << n_verts << '\n';
00085   vnl_matrix<double> cog(n_frames, 2);
00086   vnl_matrix<double> X(n_frames, n_verts);
00087   vnl_matrix<double> Y(n_frames, n_verts);
00088   for (unsigned int i = 0; i<n_frames; i++)
00089   {
00090     verts.clear();
00091     double cog_x = 0, cog_y = 0;
00092     tracked_faces[i]->vertices(verts);
00093     for (unsigned int j = 0; j<n_verts; j++)
00094     {
00095       vtol_vertex_2d_sptr v = verts[j]->cast_to_vertex_2d();
00096       if (!v)
00097         continue;
00098       cog_x += v->x(); cog_y += v->y();
00099       X.put(i,j,v->x());
00100       Y.put(i,j,v->y());
00101     }
00102     cog_x/=n_verts; cog_y/=n_verts;
00103     cog.put(i, 0, cog_x); cog.put(i, 1, cog_y);
00104   }
00105   strm << "COG:\n";
00106   vnl_matlab_print(strm, cog);
00107   strm << "X:\n";
00108   vnl_matlab_print(strm, X);
00109   strm << "Y:\n";
00110   vnl_matlab_print(strm, Y);
00111   return true;
00112 }
00113 
00114 bool strk_io::read_frame_data(vcl_ifstream& strm,
00115                               unsigned int& start_frame, unsigned int& n_frames)
00116 {
00117   if (!strm)
00118     return false;
00119   vcl_string s;
00120   strm >> s;
00121   if (s!="START_FRAME:")
00122     return false;
00123   strm >> start_frame;
00124   strm >> s;
00125   if (s!="N_FRAMES:")
00126     return false;
00127   strm >> n_frames;
00128   return true;
00129 }
00130 
00131 bool strk_io::read_region_data(vcl_ifstream& strm,
00132                                unsigned int& n_pix, float& diameter,
00133                                float& aspect_ratio)
00134 {
00135   if (!strm)
00136     return false;
00137   vcl_string s;
00138   strm >> s;
00139   if (s!="N_PIXELS:")
00140     return false;
00141   strm >> n_pix;
00142   strm >> s;
00143   if (s!="DIAMETER:")
00144     return false;
00145   strm >> diameter;
00146   strm >> s;
00147   if (s!="ASPECT_RATIO:")
00148     return false;
00149   strm >> aspect_ratio;
00150   return true;
00151 }
00152 
00153 bool strk_io::read_histogram_data(vcl_ifstream& strm, 
00154                                   unsigned int& start_frame,
00155                                   unsigned int& n_frames,
00156                                   unsigned int& n_pixels,
00157                                   float& diameter,
00158                                   float& aspect_ratio,
00159                                   unsigned int& n_int_bins,
00160                                   unsigned int& n_grad_dir_bins,
00161                                   unsigned int& n_color_bins,
00162                                   vnl_matrix<float>& hist_data)
00163 {
00164   //checks stream
00165   if (!strk_io::read_frame_data(strm, start_frame, n_frames))
00166     return false;
00167   if (!strk_io::read_region_data(strm, n_pixels, diameter, aspect_ratio))
00168     return false;
00169   vcl_string s;
00170   strm >> s;
00171   if (s!="N_INTENSITY_BINS:")
00172     return false;
00173   strm >> n_int_bins;
00174   strm >> s;
00175   if (s!="N_GRADIENT_DIR_BINS:")
00176     return false;
00177   strm >> n_grad_dir_bins;
00178   strm >> s;
00179   if (s!="N_COLOR_BINS:")
00180     return false;
00181   strm >> n_color_bins;
00182   strm >> s;
00183   if (s!="HISTOGRAMS:")
00184     return false;
00185   unsigned int nbins = n_int_bins + n_grad_dir_bins + n_color_bins;
00186   hist_data = vnl_matrix<float>(n_frames,nbins);
00187   strm >> hist_data;
00188   return true;
00189 }
00190 
00191 bool strk_io::read_track_data(vcl_ifstream& strm,
00192                               unsigned int& start_frame,
00193                               unsigned int& n_frames,
00194                               vcl_vector<vgl_point_2d<double> >& cogs,
00195                               vcl_vector<vtol_face_2d_sptr>& tracked_faces)
00196 {
00197   //checks stream as well
00198   if (!strk_io::read_frame_data(strm, start_frame, n_frames))
00199     return false;
00200   vcl_string s;
00201   strm >> s;
00202   if (s!="N_VERTS:")
00203     return false;
00204   unsigned int n_verts=0;
00205   strm >> n_verts;
00206   cogs.clear();
00207   tracked_faces.clear();
00208   vnl_matrix<double> cog(n_frames, 2);
00209   vnl_matrix<double> X(n_frames, n_verts);
00210   vnl_matrix<double> Y(n_frames, n_verts);
00211   vcl_string cs, x, y;
00212   strm>> cs;
00213   if (cs!="COG:")
00214     return false;
00215   strm>> cog;
00216   strm>> x;
00217   if (x!="X:")
00218     return false;
00219   strm>> X;
00220   strm>> y;
00221   if (y!="Y:")
00222     return false;
00223   strm>> Y;
00224   for (unsigned int i = 0; i<n_frames; i++)
00225   {
00226     vcl_vector<vtol_vertex_sptr> verts;
00227     for (unsigned int j = 0; j<n_verts; j++)
00228     {
00229       vtol_vertex* v2d  = new vtol_vertex_2d(X[i][j], Y[i][j]);
00230       verts.push_back(v2d);
00231     }
00232     vgl_point_2d<double> p(cog[i][0], cog[i][1]);
00233     cogs.push_back(p);
00234     tracked_faces.push_back(new vtol_face_2d(verts));
00235   }
00236   return true;
00237 }

Generated on Thu Jan 10 14:53:19 2008 for contrib/brl/bseg/strk by  doxygen 1.4.4