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