00001
00002 #include "strk_info_model_tracker.h"
00003
00004
00005 #include <vcl_algorithm.h>
00006 #include <vul/vul_timer.h>
00007 #include <vil1/vil1_memory_image_of.h>
00008 #include <vsol/vsol_point_2d.h>
00009 #include <btol/btol_face_algs.h>
00010 #include <brip/brip_vil1_float_ops.h>
00011 #include <strk/strk_art_info_model.h>
00012 #include <strk/strk_tracking_face_2d.h>
00013
00014
00015
00016 static bool info_compare(strk_art_info_model_sptr const m1,
00017 strk_art_info_model_sptr const m2)
00018 {
00019 return m1->total_model_info() > m2->total_model_info();
00020 }
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 strk_info_model_tracker::strk_info_model_tracker(strk_info_model_tracker_params& tp)
00031 : strk_info_model_tracker_params(tp)
00032 {
00033 }
00034
00035
00036 strk_info_model_tracker::~strk_info_model_tracker()
00037 {
00038 }
00039
00040
00041
00042
00043 void strk_info_model_tracker::set_image_0(vil1_image& image)
00044 {
00045 if (!image)
00046 {
00047 vcl_cout <<"In strk_info_model_tracker::set_image_i(.) - null input\n";
00048 return;
00049 }
00050
00051 vil1_memory_image_of<float> flt=brip_vil1_float_ops::convert_to_float(image);
00052
00053 image_0_= brip_vil1_float_ops::gaussian(flt, sigma_);
00054
00055 int w = image_0_.width(), h = image_0_.height();
00056 Ix_0_.resize(w,h);
00057 Iy_0_.resize(w,h);
00058 brip_vil1_float_ops::gradient_3x3(image_0_, Ix_0_, Iy_0_);
00059 }
00060
00061
00062
00063
00064 void strk_info_model_tracker::set_image_i(vil1_image& image)
00065 {
00066 if (!image)
00067 {
00068 vcl_cout <<"In strk_info_model_tracker::set_image_i(.) - null input\n";
00069 return;
00070 }
00071
00072 vil1_memory_image_of<float> flt=brip_vil1_float_ops::convert_to_float(image);
00073
00074 image_i_ = brip_vil1_float_ops::gaussian(flt, sigma_);
00075 int w = image_i_.width(), h = image_i_.height();
00076 Ix_i_.resize(w,h);
00077 Iy_i_.resize(w,h);
00078 brip_vil1_float_ops::gradient_3x3(image_i_, Ix_i_, Iy_i_);
00079 }
00080
00081
00082
00083
00084 void strk_info_model_tracker::init()
00085 {
00086 if (!image_0_)
00087 return;
00088 if (!initial_model_.size())
00089 return;
00090
00091 vsol_point_2d_sptr pivot = btol_face_algs::centroid(initial_model_[0]);
00092 strk_art_info_model_sptr mod;
00093 if (gradient_info_)
00094 mod = new strk_art_info_model(initial_model_,pivot, image_0_, Ix_0_, Iy_0_);
00095 else
00096 mod = new strk_art_info_model(initial_model_,pivot, image_0_);
00097 current_samples_.push_back(mod);
00098 }
00099
00100 static float rand_val(float range)
00101 {
00102 return float((2.0f*range)*(rand()/(RAND_MAX+1.0f)) - range);
00103 }
00104
00105
00106
00107 strk_art_info_model_sptr
00108 strk_info_model_tracker::generate_model(strk_art_info_model_sptr const& seed)
00109 {
00110 if (!seed)
00111 return 0;
00112
00113 float stem_tx = rand_val(stem_trans_radius_);
00114 float stem_ty = rand_val(stem_trans_radius_);
00115 float stem_angle = rand_val(stem_angle_range_);
00116 float long_arm_angle = rand_val(long_arm_angle_range_);
00117 float short_arm_angle = rand_val(short_arm_angle_range_);
00118 float long_arm_tip_angle = rand_val(long_arm_tip_angle_range_);
00119 float short_arm_tip_angle = rand_val(short_arm_tip_angle_range_);
00120 strk_art_info_model_sptr mod = new strk_art_info_model(seed);
00121 if (!mod->transform(stem_tx, stem_ty, stem_angle, long_arm_angle,
00122 short_arm_angle, long_arm_tip_angle, short_arm_tip_angle))
00123 return (strk_art_info_model*)0;
00124 else
00125 return mod;
00126 }
00127
00128
00129
00130 void strk_info_model_tracker::generate_samples()
00131 {
00132 vul_timer t;
00133 for (vcl_vector<strk_art_info_model_sptr>::iterator mit =
00134 current_samples_.begin(); mit != current_samples_.end(); mit++)
00135 for (int i = 0; i<n_samples_; i++)
00136 {
00137 strk_art_info_model_sptr mod =
00138 this->generate_model(*mit);
00139 if (!mod)
00140 {
00141 vcl_cout << "In strk_info_model_tracker::generate_samples() -"
00142 << " generate_model failed\n";
00143 continue;
00144 }
00145 if (gradient_info_)
00146 mod->compute_mutual_information(image_i_, Ix_i_, Iy_i_);
00147 else
00148 mod->compute_mutual_information(image_i_);
00149 hypothesized_samples_.push_back(mod);
00150 }
00151
00152
00153 vcl_sort(hypothesized_samples_.begin(),
00154 hypothesized_samples_.end(), info_compare);
00155 }
00156
00157
00158
00159 void strk_info_model_tracker::cull_samples()
00160 {
00161 current_samples_.clear();
00162 for (int i =0; i<n_samples_; i++)
00163 {
00164 current_samples_.push_back(hypothesized_samples_[i]);
00165 }
00166 if (verbose_)
00167 vcl_cout << "Total Inf = " << hypothesized_samples_[0]->total_model_info()
00168 << ")\n";
00169 hypothesized_samples_.clear();
00170 }
00171
00172
00173
00174 strk_art_info_model_sptr strk_info_model_tracker::get_best_sample()
00175 {
00176 if (!current_samples_.size())
00177 return 0;
00178 return current_samples_[0];
00179 }
00180
00181
00182
00183 void strk_info_model_tracker::get_samples(vcl_vector<strk_art_info_model_sptr>& samples)
00184 {
00185 samples.clear();
00186 for (vcl_vector<strk_art_info_model_sptr>::iterator
00187 mit = current_samples_.begin(); mit != current_samples_.end(); mit++)
00188 samples.push_back(*mit);
00189 }
00190
00191
00192
00193 void strk_info_model_tracker::track()
00194 {
00195 vul_timer t;
00196 this->generate_samples();
00197 if (verbose_)
00198 vcl_cout << "Samples generated " << t.real() << " msecs.\n";
00199 this->cull_samples();
00200 }
00201
00202 void strk_info_model_tracker::clear()
00203 {
00204 current_samples_.clear();
00205 hypothesized_samples_.clear();
00206 }