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