contrib/brl/bseg/bbgm/pro/processes/bbgm_display_dist_image_process.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/pro/processes/bbgm_display_dist_image_process.cxx
00002 
00003 //:
00004 // \file
00005 #include <bprb/bprb_func_process.h>
00006 #include <vcl_iostream.h>
00007 #include <bbgm/bbgm_image_of.h>
00008 #include <bbgm/bbgm_image_sptr.h>
00009 #include <bbgm/bbgm_update.h>
00010 #include <bsta/bsta_attributes.h>
00011 #include <bsta/bsta_gauss_if3.h>
00012 #include <bsta/bsta_gauss_sf1.h>
00013 #include <bsta/bsta_mixture.h>
00014 #include <bsta/bsta_basic_functors.h>
00015 #include <bsta/algo/bsta_adaptive_updater.h>
00016 #include <brdb/brdb_value.h>
00017 #include <vbl/io/vbl_io_smart_ptr.h>
00018 #include <vil/vil_image_view.h>
00019 #include <vil/vil_convert.h>
00020 #include <vil/vil_math.h>
00021 #include <bbgm/bbgm_viewer.h>
00022 #include <bbgm/bbgm_viewer_sptr.h>
00023 
00024 
00025 namespace type_definitions {
00026   //: mixture of independent gaussian's of 3 dimensions
00027   typedef bsta_num_obs<bsta_mixture<bsta_num_obs<bsta_gauss_if3> > > mix_gauss_type_if3;
00028   typedef bsta_num_obs<bsta_mixture_fixed<bsta_num_obs<bsta_gauss_sf1>, 3> > mix_fixed_gauss_type_sf1;
00029   typedef bsta_num_obs<bsta_mixture<bsta_num_obs<bsta_gauss_sf1> > > mix_gauss_type_sf1;
00030 }
00031 
00032 //: if the application needs to display different types than the ones already registered, then add them to this function
00033 void register_mean_viewers()
00034 {
00035   using namespace type_definitions;
00036   bbgm_mean_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_if3, bsta_mean_functor<mix_gauss_type_if3> >);
00037   bbgm_mean_viewer::register_view_maker(new bbgm_view_maker<mix_fixed_gauss_type_sf1, bsta_mean_functor<mix_fixed_gauss_type_sf1> >);
00038   bbgm_mean_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_sf1, bsta_mean_functor<mix_gauss_type_sf1> >);
00039 }
00040 
00041 void register_variance_viewers()
00042 {
00043   using namespace type_definitions;
00044   bbgm_variance_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_if3, bsta_diag_covar_functor<mix_gauss_type_if3> >);
00045   bbgm_variance_viewer::register_view_maker(new bbgm_view_maker<mix_fixed_gauss_type_sf1, bsta_var_functor<mix_fixed_gauss_type_sf1> >);
00046   bbgm_variance_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_sf1, bsta_var_functor<mix_gauss_type_sf1> >);
00047 }
00048 
00049 void register_weight_viewers()
00050 {
00051   using namespace type_definitions;
00052   bbgm_weight_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_if3, bsta_weight_functor<mix_gauss_type_if3> >);
00053   bbgm_weight_viewer::register_view_maker(new bbgm_view_maker<mix_fixed_gauss_type_sf1, bsta_weight_functor<mix_fixed_gauss_type_sf1> >);
00054   bbgm_weight_viewer::register_view_maker(new bbgm_view_maker<mix_gauss_type_sf1, bsta_weight_functor<mix_gauss_type_sf1> >);
00055 }
00056 
00057 
00058 // constructor function
00059 bool bbgm_display_dist_image_process_cons(bprb_func_process& pro)
00060 {
00061   //input
00062   vcl_vector<vcl_string> in_types(4), out_types(1);
00063   in_types[0]= "bbgm_image_sptr"; //background image
00064   in_types[1]= "vcl_string"; //what to display, e.g. mean, variance etc.
00065   in_types[2]= "int"; //the component to display
00066   in_types[3]= "bool"; //scale the output to byte range
00067   pro.set_input_types(in_types);
00068 
00069   //output
00070   out_types[0]= "vil_image_view_base_sptr";
00071   pro.set_output_types(out_types);
00072   return true;
00073 }
00074 
00075 //: Execute the process function
00076 bool bbgm_display_dist_image_process(bprb_func_process& pro)
00077 {
00078   // Sanity check
00079   if (!pro.verify_inputs()){
00080     vcl_cerr << "In bbgm_display_dist_image_process::execute() -"
00081              << " invalid inputs\n";
00082     return false;
00083   }
00084 
00085   // Retrieve background image
00086   bbgm_image_sptr bgm = pro.get_input<bbgm_image_sptr>(0);
00087   if (!bgm){
00088     vcl_cerr << "In bbgm_display_dist_image_process::execute() -"
00089              << " null distribution image\n";
00090     return false;
00091   }
00092 
00093   //Retrieve attribute to display, e.g. mean
00094   vcl_string attr = pro.get_input<vcl_string>(1);
00095 
00096   //Retrieve component index
00097   int comp_index = pro.get_input<int>(2);
00098 
00099   //Retrieve scale switch
00100   bool scale = pro.get_input<bool>(3);
00101 
00102   vcl_vector<vcl_string> output_types(1);
00103   output_types[0]= "vil_image_view_base_sptr";
00104   pro.set_output_types(output_types);
00105 
00106   bbgm_viewer_sptr viewer;
00107   if (attr=="mean") {
00108     viewer = new bbgm_mean_viewer();
00109     register_mean_viewers();
00110   }
00111   else if (attr == "variance"||attr == "std_dev") {
00112     viewer = new bbgm_variance_viewer();
00113     register_variance_viewers();
00114   }
00115   else if (attr == "weight") {
00116     viewer = new bbgm_weight_viewer();
00117     register_weight_viewers();
00118   }
00119   else {
00120     vcl_cout << "In bbgm_display_dist_image_process::execute() -"
00121              << " display attribute not available\n";
00122     return false;
00123   }
00124   if (!viewer->probe(bgm)){
00125     vcl_cout << "In bbgm_display_dist_image_process::execute() -"
00126              << " displayer cannot process distribution image type\n"
00127              << bgm->is_a() << '\n';
00128     return false;
00129   }
00130   viewer->set_active_component(comp_index);
00131   vil_image_view<double> d_image;
00132   if (!viewer->apply(bgm, d_image)){
00133     vcl_cout << "In bbgm_display_dist_image_process::execute() -"
00134              << " extract view (apply) failed\n";
00135     return false;
00136   }
00137   brdb_value_sptr output0;
00138   //convert to a byte image for display
00139   vil_image_view<vxl_byte> byte_image;
00140   if (attr == "variance"||attr == "std_dev"){
00141     if (attr == "std_dev") vil_math_sqrt(d_image);
00142     if (scale){
00143       double dmin, dmax;
00144       vil_math_value_range(d_image, dmin, dmax);
00145       vil_convert_stretch_range_limited(d_image, byte_image, dmin, dmax);
00146       output0 =
00147         new brdb_value_t<vil_image_view_base_sptr>(new vil_image_view<vxl_byte>(byte_image));
00148     }
00149     else{
00150     vil_image_view<float> fimg;
00151     vil_convert_cast(d_image, fimg);
00152     output0 =
00153     new brdb_value_t<vil_image_view_base_sptr>(new vil_image_view<float>(fimg));
00154     }
00155   }
00156   else if (scale){
00157     vil_convert_stretch_range_limited(d_image, byte_image, 0.0, 1.0);
00158     output0 =
00159     new brdb_value_t<vil_image_view_base_sptr>(new vil_image_view<vxl_byte>(byte_image));
00160   }
00161   else{
00162     vil_image_view<float> fimg;
00163     vil_convert_cast(d_image, fimg);
00164     output0 =
00165     new brdb_value_t<vil_image_view_base_sptr>(new vil_image_view<float>(fimg));
00166   }
00167   pro.set_output(0, output0);
00168   return true;
00169 }
00170