00001
00002 #include "strk_region_info.h"
00003
00004
00005 #include <vil1/vil1_memory_image_of.h>
00006 #include <vgl/algo/vgl_h_matrix_2d_compute_4point.h>
00007 #include <vtol/vtol_vertex_sptr.h>
00008 #include <vtol/vtol_vertex_2d.h>
00009 #include <brip/brip_vil1_float_ops.h>
00010 #include <strk/strk_tracking_face_2d.h>
00011
00012 #if 0 // unused static function
00013
00014 static bool info_compare(strk_tracking_face_2d_sptr const f1,
00015 strk_tracking_face_2d_sptr const f2)
00016 {
00017 return f1->total_info() > f2->total_info();
00018 }
00019 #endif // 0
00020
00021
00022
00023
00024
00025
00026
00027
00028 strk_region_info::strk_region_info(strk_region_info_params& tp)
00029 : strk_region_info_params(tp)
00030 {
00031 }
00032
00033
00034 strk_region_info::~strk_region_info()
00035 {
00036 }
00037
00038
00039
00040
00041 void strk_region_info::set_image_0(vil1_image& image)
00042 {
00043 if (!image)
00044 {
00045 vcl_cout <<"In strk_region_info::set_image_i(.) - null input\n";
00046 return;
00047 }
00048
00049 vil1_memory_image_of<float> in, hue, sat;
00050 int w = image.width(), h = image.height();
00051
00052 if (!color_info_||image.components()==1)
00053 {
00054 vil1_memory_image_of<float> in=brip_vil1_float_ops::convert_to_float(image);
00055 image_0_= brip_vil1_float_ops::gaussian(in, sigma_);
00056 }
00057
00058 if (color_info_&&image.components()==3)
00059 {
00060 vil1_memory_image_of<vil1_rgb<unsigned char> > temp(image);
00061 brip_vil1_float_ops::convert_to_IHS(temp, in, hue, sat);
00062 image_0_= brip_vil1_float_ops::gaussian(in, sigma_);
00063 hue_0_ = brip_vil1_float_ops::gaussian(hue, sigma_);
00064 sat_0_ = brip_vil1_float_ops::gaussian(sat, sigma_);
00065 }
00066
00067 if (gradient_info_)
00068 {
00069 Ix_0_.resize(w,h);
00070 Iy_0_.resize(w,h);
00071 brip_vil1_float_ops::gradient_3x3(image_0_, Ix_0_, Iy_0_);
00072 }
00073 }
00074
00075
00076
00077
00078 void strk_region_info::set_image_i(vil1_image& image)
00079 {
00080 if (!image)
00081 {
00082 vcl_cout <<"In strk_region_info::set_image_i(.) - null input\n";
00083 return;
00084 }
00085 vgl_h_matrix_2d<double> H;
00086 bool mapi = this->map_i_to_0(H);
00087
00088 int w = image.width(), h = image.height();
00089
00090 if (!color_info_||image.components()==1)
00091 {
00092 vil1_memory_image_of<float> in=brip_vil1_float_ops::convert_to_float(image);
00093 vil1_memory_image_of<float> out(w, h);
00094 if (mapi)
00095 brip_vil1_float_ops::homography(in, H, out, true);
00096 else
00097 out = in;
00098 image_i_= brip_vil1_float_ops::gaussian(out, sigma_);
00099 }
00100
00101 if (color_info_&&image.components()==3)
00102 {
00103 vil1_memory_image_of<vil1_rgb<unsigned char> > temp(image);
00104 vil1_memory_image_of<float> in, hue, sat;
00105 brip_vil1_float_ops::convert_to_IHS(temp, in, hue, sat);
00106 vil1_memory_image_of<float> in_out(w, h), hue_out(w,h), sat_out(w,h);
00107 if (mapi)
00108 {
00109 brip_vil1_float_ops::homography(in, H, in_out, true);
00110 brip_vil1_float_ops::homography(hue, H, hue_out, true);
00111 brip_vil1_float_ops::homography(sat, H, sat_out, true);
00112 }
00113 else
00114 {
00115 in_out = in;
00116 hue_out = hue;
00117 sat_out = sat;
00118 }
00119 image_i_= brip_vil1_float_ops::gaussian(in_out, sigma_);
00120 hue_i_ = brip_vil1_float_ops::gaussian(hue_out, sigma_);
00121 sat_i_ = brip_vil1_float_ops::gaussian(sat_out, sigma_);
00122 }
00123
00124 if (gradient_info_)
00125 {
00126 Ix_i_.resize(w,h);
00127 Iy_i_.resize(w,h);
00128 brip_vil1_float_ops::gradient_3x3(image_i_, Ix_i_, Iy_i_);
00129 }
00130 }
00131
00132
00133
00134 void strk_region_info::set_face_0(vtol_face_2d_sptr const& face)
00135 {
00136 face_0_ = face;
00137 }
00138
00139
00140
00141 void strk_region_info::set_face_i(vtol_face_2d_sptr const& face)
00142 {
00143 face_i_ = face;
00144 }
00145
00146
00147 void strk_region_info::set_background_face(vtol_face_2d_sptr const& face)
00148 {
00149 background_face_ = face;
00150 }
00151
00152 static vtol_face_2d_sptr image_face(vil1_memory_image_of<float> const& image)
00153 {
00154 int w = image.width()-1, h = image.height()-1;
00155 vtol_vertex_sptr v0 = new vtol_vertex_2d(0, 0);
00156 vtol_vertex_sptr v1 = new vtol_vertex_2d(w, 0);
00157 vtol_vertex_sptr v2 = new vtol_vertex_2d(w, h);
00158 vtol_vertex_sptr v3 = new vtol_vertex_2d(0, h);
00159 vcl_vector<vtol_vertex_sptr> verts;
00160 verts.push_back(v0); verts.push_back(v1);
00161 verts.push_back(v2); verts.push_back(v3);
00162 vtol_face_2d_sptr f = new vtol_face_2d(verts);
00163 return f;
00164 }
00165
00166
00167
00168 void strk_region_info::evaluate_info()
00169 {
00170 if (!image_0_||!image_i_)
00171 return;
00172 vtol_face_2d_sptr f = face_0_;
00173 if (!f)
00174 f = image_face(image_0_);
00175
00176 strk_tracking_face_2d_sptr tf
00177 = new strk_tracking_face_2d(f, image_0_,
00178 Ix_0_, Iy_0_, hue_0_, sat_0_,
00179 min_gradient_,
00180 parzen_sigma_,0,0,0);
00181 if (!tf->compute_mutual_information(image_i_, Ix_i_, Iy_i_, hue_i_, sat_i_))
00182 return;
00183
00184 if (verbose_)
00185 vcl_cout << "Total Inf = " << tf->total_info()
00186 << " = IntInfo(" << tf->int_mutual_info()
00187 << ") + GradInfo(" << tf->grad_mutual_info()
00188 << ") + ColorInfo(" << tf->color_mutual_info()
00189 << ")\n" << vcl_flush;
00190
00191 if (debug_)
00192 vcl_cout << "model_intensity_entropy = " << tf->model_intensity_entropy()
00193 << "\nintensity_entropy = " << tf->intensity_entropy()
00194 << "\nintensity_joint_entropy = " <<tf->intensity_joint_entropy()
00195 << "\n\nmodel_gradient_entropy = " << tf->model_gradient_entropy()
00196 << "\ngradient_entropy = " << tf->gradient_entropy()
00197 << "\ngradient_joint_entropy = " << tf->gradient_joint_entropy()
00198 << "\n\nmodel_color_entropy = " << tf->model_color_entropy()
00199 << "\ncolor_entropy = " << tf->color_entropy()
00200 << "\ncolor_joint_entropy = " << tf->color_joint_entropy()
00201 << "\n\n\n"<< vcl_flush;
00202 }
00203
00204
00205
00206 void strk_region_info::evaluate_background_info()
00207 {
00208 if (!image_0_ || !face_0_)
00209 return;
00210
00211 strk_tracking_face_2d_sptr tf
00212 = new strk_tracking_face_2d(face_0_, image_0_,
00213 Ix_0_, Iy_0_, hue_0_, sat_0_,
00214 min_gradient_,
00215 parzen_sigma_,0,0,0);
00216 tf->print_intensity_histograms(image_0_);
00217
00218 if (!background_face_)
00219 return;
00220
00221 strk_tracking_face_2d_sptr bf
00222 = new strk_tracking_face_2d(background_face_, image_0_,
00223 Ix_0_, Iy_0_, hue_0_, sat_0_,
00224 min_gradient_,
00225 parzen_sigma_,0,0,0);
00226 bf->print_intensity_histograms(image_0_);
00227 float x = tf->intensity_mutual_info_diff(bf, image_0_, true);
00228 vcl_cout << "intensity_mutual_info_diff = " << x << vcl_endl;
00229 }
00230
00231
00232
00233
00234 bool strk_region_info::map_i_to_0(vgl_h_matrix_2d<double>& H)
00235 {
00236 if (!face_0_||!face_i_)
00237 return false;
00238
00239 vcl_vector<vtol_vertex_sptr> verts0, vertsi;
00240 face_0_->vertices(verts0);
00241 face_i_->vertices(vertsi);
00242 int n0 = verts0.size(), ni = vertsi.size();
00243 if (n0!=4||ni!=4)
00244 return false;
00245
00246 vcl_vector<vgl_homg_point_2d<double> > pts_0, pts_i;
00247 for (int k = 0; k<4; k++)
00248 {
00249 vtol_vertex_2d_sptr v0 = verts0[k]->cast_to_vertex_2d(),
00250 vi = vertsi[k]->cast_to_vertex_2d();
00251 vgl_homg_point_2d<double> hp0(v0->x(), v0->y()), hpi(vi->x(), vi->y());
00252 pts_0.push_back(hp0); pts_i.push_back(hpi);
00253 }
00254
00255 vgl_h_matrix_2d_compute_4point hc;
00256 hc.compute(pts_i, pts_0, H);
00257 return true;
00258 }
00259
00260 vil1_memory_image_of<unsigned char> strk_region_info::image_0()
00261 {
00262 return brip_vil1_float_ops::convert_to_byte(image_0_, 0, 255);
00263 }
00264
00265 vil1_memory_image_of<unsigned char> strk_region_info::image_i()
00266 {
00267 return brip_vil1_float_ops::convert_to_byte(image_i_, 0, 255);
00268 }