Go to the documentation of this file.00001
00002 #include "strk_info_tracker_process.h"
00003
00004
00005 #include <vcl_fstream.h>
00006 #include <vcl_iostream.h>
00007 #include <vcl_vector.h>
00008 #include <vtol/vtol_topology_object.h>
00009 #include <vtol/vtol_vertex_sptr.h>
00010 #include <vtol/vtol_edge.h>
00011 #include <vtol/vtol_face_2d.h>
00012 #include <vil1/vil1_image.h>
00013 #include "strk_tracker.h"
00014 #include "strk_tracking_face_2d.h"
00015 #include "strk_io.h"
00016
00017 strk_info_tracker_process::strk_info_tracker_process(strk_info_tracker_params & tp)
00018 : tracker_(tp)
00019 {
00020 n_verts_ = 0;
00021 start_frame_ = 0;
00022 write_tracked_faces_=false;
00023 write_tracked_hist_=false;
00024 failure_ = false;
00025 first_frame_ = true;
00026
00027 unsigned int ibins = tracker_.intensity_hist_bins_;
00028 unsigned int gbins = tracker_.gradient_dir_hist_bins_;
00029 unsigned int cbins = tracker_.color_hist_bins_;
00030 unsigned int nbins = ibins + gbins + cbins;
00031 color_index_.resize(nbins);
00032 for (unsigned int c = 0;c<nbins; c++)
00033 if (c<ibins)
00034 color_index_[c]=0;
00035 else
00036 if (c>=ibins&&c<(ibins+gbins))
00037 color_index_[c]=1;
00038 else
00039 color_index_[c]=2;
00040 }
00041
00042 strk_info_tracker_process::~strk_info_tracker_process()
00043 {
00044 tracker_.clear();
00045 }
00046
00047 bool strk_info_tracker_process::execute()
00048 {
00049 if (failure_)
00050 {
00051 vcl_cout << "In strk_info_tracker_process::execute() - process failed\n";
00052 return false;
00053 }
00054 if (this->get_N_input_images()!=1)
00055 {
00056 vcl_cout << "In strk_info_tracker_process::execute() -"
00057 << " not exactly one input image\n";
00058 failure_ = true;
00059 return false;
00060 }
00061 output_topo_objs_.clear();
00062
00063 vil1_image img = vpro_video_process::get_input_image(0);
00064 input_images_.clear();
00065 if (first_frame_)
00066 {
00067 tracker_.set_image_0(img);
00068 int nto = get_N_input_topo_objs();
00069 if (!nto)
00070 {
00071 vcl_cout << "In strk_info_tracker_process::execute() -"
00072 << " no input correlation face\n";
00073 failure_ = true;
00074 return false;
00075 }
00076 vtol_topology_object_sptr to = input_topo_objs_[0];
00077 vtol_face_sptr f = to->cast_to_face();
00078 vtol_face_2d_sptr f2d = f->cast_to_face_2d();
00079 if (!f2d)
00080 {
00081 vcl_cout << "In strk_info_tracker_process::execute() -"
00082 << " input is not a vtol_face_2d\n";
00083 failure_ = true;
00084 return false;
00085 }
00086 vcl_vector<vtol_vertex_sptr> verts;
00087 f2d->vertices(verts);
00088 n_verts_ = verts.size();
00089 start_frame_ = this->frame_index();
00090 tracked_faces_.clear();
00091 tracked_hist_.clear();
00092 tracked_faces_.push_back(f2d);
00093 tracker_.set_initial_model(f2d);
00094 tracker_.init();
00095 if (tracker_.use_background_)
00096 if (!tracker_.construct_background_faces(f2d, true))
00097 {
00098 vcl_cout << "Warning - In strk_info_tracker_process::execute() -"
00099 << " could not construct background faces\n";
00100 failure_ = true;
00101 return false;
00102 }
00103 vcl_vector<vtol_edge_sptr> edges_2d;
00104 f2d->edges(edges_2d);
00105 for (vcl_vector<vtol_edge_sptr>::iterator eit = edges_2d.begin();
00106 eit != edges_2d.end(); eit++)
00107 {
00108 vtol_topology_object_sptr to = (*eit)->cast_to_edge();
00109 output_topo_objs_.push_back(to);
00110 }
00111 first_frame_ = false;
00112 return true;
00113 }
00114
00115 tracker_.set_image_i(img);
00116 tracker_.track();
00117 #if 0
00118 vcl_vector<vtol_face_2d_sptr> samples;
00119 tracker_.get_samples(samples);
00120 for (vcl_vector<vtol_face_2d_sptr>::iterator fit = samples.begin();
00121 fit != samples.end(); fit++)
00122 {
00123 vtol_topology_object_sptr to =
00124 (vtol_topology_object*)((*fit)->cast_to_face());
00125 output_topo_objs_.push_back(to);
00126 }
00127 #endif
00128 #if 1
00129
00130 vcl_vector<vtol_topology_object_sptr> points;
00131
00132 for (vcl_vector<vtol_topology_object_sptr>::iterator pit = points.begin();
00133 pit != points.end(); ++pit)
00134 output_topo_objs_.push_back(*pit);
00135
00136 vtol_face_2d_sptr f = tracker_.get_best_sample();
00137 tracked_faces_.push_back(f);
00138 vcl_vector<vtol_edge_sptr> edges;
00139 f->edges(edges);
00140 for (vcl_vector<vtol_edge_sptr>::iterator eit = edges.begin();
00141 eit != edges.end(); eit++)
00142 {
00143 vtol_topology_object_sptr to = (*eit)->cast_to_edge();
00144 output_topo_objs_.push_back(to);
00145 }
00146 if (tracker_.use_background_)
00147 {
00148 vcl_vector<vtol_face_2d_sptr> background_faces;
00149 if (tracker_.get_background_faces(background_faces))
00150 for (vcl_vector<vtol_face_2d_sptr>::iterator fit =
00151 background_faces.begin(); fit != background_faces.end(); fit++)
00152 {
00153 vtol_face_2d_sptr fb = *fit;
00154 vcl_vector<vtol_edge_sptr> bedges;
00155 fb->edges(bedges);
00156 for (vcl_vector<vtol_edge_sptr>::iterator eit = bedges.begin();
00157 eit != bedges.end(); eit++)
00158 {
00159 vtol_topology_object_sptr to = (*eit)->cast_to_edge();
00160 output_topo_objs_.push_back(to);
00161 }
00162 }
00163 }
00164
00165 vpro_video_process::set_graph(tracker_.histograms());
00166 vpro_video_process::set_graph_flag();
00167 tracked_hist_.push_back(tracker_.histograms());
00168 #endif
00169 return true;
00170 }
00171
00172 bool strk_info_tracker_process::finish()
00173 {
00174 if (write_tracked_faces_)
00175 {
00176 vcl_ofstream strm(track_file_.c_str());
00177 if (!strk_io::write_track_data(start_frame_, tracked_faces_, strm))
00178 return false;
00179 }
00180 if (write_tracked_hist_)
00181 {
00182 vcl_ofstream strm(hist_file_.c_str());
00183 strk_tracking_face_2d_sptr itf = tracker_.initial_tf();
00184 if (!strk_io::write_histogram_data(start_frame_,
00185 itf->face()->Npix(),
00186 itf->face()->Diameter(),
00187 itf->face()->AspectRatio(),
00188 tracker_.intensity_hist_bins_,
00189 tracker_.gradient_dir_hist_bins_,
00190 tracker_.color_hist_bins_,
00191 tracked_hist_, strm))
00192 return false;
00193 }
00194 return true;
00195 }
00196
00197 bool strk_info_tracker_process::set_track_output_file(vcl_string const& file_name)
00198 {
00199 write_tracked_faces_ = true;
00200 track_file_ = file_name;
00201 vcl_ofstream track_stream(track_file_.c_str());
00202 if (!track_stream)
00203 {
00204 vcl_cout << "In strk_info_tracker_process::set_output_file() -"
00205 << " could not open file " << track_file_ << '\n';
00206 return false;
00207 }
00208 track_stream.close();
00209 return true;
00210 }
00211
00212 bool strk_info_tracker_process::set_hist_output_file(vcl_string const& file_name)
00213 {
00214 write_tracked_hist_ = true;
00215 hist_file_ = file_name;
00216 vcl_ofstream hist_stream(hist_file_.c_str());
00217 if (!hist_stream)
00218 {
00219 vcl_cout << "In strk_info_tracker_process::set_output_file() -"
00220 << " could not open file " << hist_file_ << '\n';
00221 return false;
00222 }
00223 hist_stream.close();
00224 return true;
00225 }
00226