contrib/brl/bseg/bbgm/pro/processes/bbgm_measure_process.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/pro/processes/bbgm_measure_process.cxx
00002 #define MEASURE_BKGROUND
00003 //:
00004 // \file
00005 #include <bprb/bprb_func_process.h>
00006 #include <bprb/bprb_parameters.h>
00007 #include <vcl_iostream.h>
00008 #include <bbgm/bbgm_image_of.h>
00009 #include <bbgm/bbgm_image_sptr.h>
00010 #include <bbgm/bbgm_update.h>
00011 #include <bsta/bsta_attributes.h>
00012 #include <bsta/bsta_gauss_if3.h>
00013 #include <bsta/bsta_gauss_sf1.h>
00014 #if 0
00015 #include <bsta/bsta_parzen_sphere.h>
00016 #endif
00017 #include <bsta/bsta_mixture.h>
00018 #include <bsta/bsta_basic_functors.h>
00019 #include <bsta/algo/bsta_adaptive_updater.h>
00020 #include <brdb/brdb_value.h>
00021 #include <vbl/io/vbl_io_smart_ptr.h>
00022 #include <vil/vil_image_view.h>
00023 #include <vil/vil_convert.h>
00024 #include <vil/vil_math.h>
00025 #include <bsta/bsta_histogram.h>
00026 #include <bbgm/bbgm_measure.h>
00027 #include <brip/brip_vil_float_ops.h>
00028 #include <vcl_cstddef.h> // for std::size_t
00029 #include <vcl_string.h> // for std::string::npos
00030 
00031 bool bbgm_measure_process_cons(bprb_func_process& pro)
00032 {
00033   vcl_vector<vcl_string> in_types(4), out_types(1);
00034   in_types[0]= "bbgm_image_sptr"; //background image
00035   in_types[1]= "vil_image_view_base_sptr"; //test image
00036   in_types[2]= "vcl_string"; //what to measure
00037   in_types[3]= "float"; // measure tolerance
00038   pro.set_input_types(in_types);
00039   out_types[0]= "vil_image_view_base_sptr"; //the display image
00040   pro.set_output_types(out_types);
00041   return true;
00042 }
00043 
00044 //: Execute the process function
00045 bool bbgm_measure_process(bprb_func_process& pro)
00046 {
00047   // Sanity check
00048   if (!pro.verify_inputs()) {
00049     vcl_cerr << "In bbgm_measure_process::execute() -"
00050              << " invalid inputs\n";
00051     return false;
00052   }
00053 
00054   // Retrieve background image
00055 
00056   bbgm_image_sptr bgm = pro.get_input<bbgm_image_sptr>(0);
00057   if (!bgm) {
00058     vcl_cerr << "In bbgm_measure_process::execute() -"
00059              << " null distribution image\n";
00060     return false;
00061   }
00062   vcl_string image_type = bgm->is_a();
00063   //for now just check for parzen_sphere in the string
00064   vcl_size_t str_indx = image_type.find("parzen_sphere");
00065   bool parzen = str_indx != vcl_string::npos;
00066 
00067   vil_image_view_base_sptr img_ptr =
00068     pro.get_input<vil_image_view_base_sptr>(1);
00069   if (!img_ptr) {
00070     vcl_cerr << "In bbgm_measure_process::execute() -"
00071              << " null measurement input image\n";
00072     return false;
00073   }
00074   vil_image_view<float> image = *vil_convert_cast(float(), img_ptr);
00075   if (img_ptr->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
00076     vil_math_scale_values(image,1.0/255.0);
00077 
00078   unsigned np = image.nplanes();
00079 
00080   //Retrieve attribute to measure, e.g. probability
00081   vcl_string attr = pro.get_input<vcl_string>(2);
00082 
00083   //Retrieve measure tolerance
00084   float tolerance = pro.get_input<float>(3);
00085 
00086   vil_image_view<float> result;
00087   if (np ==1) {
00088     typedef bsta_gauss_sf1 bsta_gauss1_t;
00089     typedef bsta_num_obs<bsta_gauss1_t> gauss_type1;
00090     typedef bsta_mixture<gauss_type1> mix_gauss_type1;
00091     typedef bsta_num_obs<mix_gauss_type1> obs_mix_gauss_type1;
00092     bbgm_image_of<obs_mix_gauss_type1> *model =
00093       static_cast<bbgm_image_of<obs_mix_gauss_type1>*>(bgm.ptr());
00094 
00095     if (attr=="probability") {
00096       bsta_probability_functor<mix_gauss_type1> functor_;
00097       measure(*model, image, result, functor_, tolerance);
00098     }
00099 #ifdef MEASURE_BKGROUND
00100     else if (attr=="prob_background") {
00101       bsta_prob_density_functor<mix_gauss_type1> functor_;
00102       measure_bkground(*model, image, result, functor_, tolerance);
00103     }
00104 #endif // MEASURE_BKGROUND
00105     else {
00106       vcl_cout << "In bbgm_measure_process::execute() -"
00107                << " measurement not available\n";
00108       return false;
00109     }
00110   }
00111   if (np ==3)
00112   {
00113     typedef bsta_gauss_if3 bsta_gauss3_t;
00114     typedef bsta_gauss3_t::vector_type vector3_;
00115     typedef bsta_num_obs<bsta_gauss3_t> gauss_type3;
00116     typedef bsta_mixture<gauss_type3> mix_gauss_type3;
00117     typedef bsta_num_obs<mix_gauss_type3> obs_mix_gauss_type3;
00118 #if 0
00119     typedef bsta_parzen_sphere<float, 3> parzen_f3_t;
00120     typedef parzen_f3_t::vector_type pvtype_;
00121 #endif
00122     if (attr=="probability") {
00123       if (!parzen) {
00124         bbgm_image_of<obs_mix_gauss_type3> *model =
00125           static_cast<bbgm_image_of<obs_mix_gauss_type3>*>(bgm.ptr());
00126         bsta_probability_functor<mix_gauss_type3> functor_;
00127         measure(*model, image, result, functor_, tolerance);
00128       }
00129     }
00130 #ifdef MEASURE_BKGROUND
00131     else if (attr=="prob_background") {
00132       if (!parzen) {
00133         bbgm_image_of<obs_mix_gauss_type3> *model =
00134           static_cast<bbgm_image_of<obs_mix_gauss_type3>*>(bgm.ptr());
00135         bsta_prob_density_functor<mix_gauss_type3> functor_;
00136         measure_bkground(*model, image, result, functor_, tolerance);
00137       }
00138     }
00139 #endif // MEASURE_BKGROUND
00140     else {
00141       vcl_cout << "In bbgm_measure_process::execute() -"
00142                << " measurement not available\n";
00143       return false;
00144     }
00145   }
00146   vcl_vector<vcl_string> output_types(1);
00147   output_types[0]= "vil_image_view_base_sptr";
00148   pro.set_output_types(output_types);
00149   brdb_value_sptr output =
00150     new brdb_value_t<vil_image_view_base_sptr>(new vil_image_view<float>(result));
00151   pro.set_output(0, output);
00152   return true;
00153 }
00154