contrib/brl/bseg/bbgm/pro/processes/bbgm_update_dist_image_stream_process.cxx
Go to the documentation of this file.
00001 // This is brl/bseg/bbgm/pro/processes/bbgm_update_dist_image_stream_process.cxx
00002 #include <bprb/bprb_func_process.h>
00003 //:
00004 // \file
00005 
00006 #include <bbgm/bbgm_image_of.h>
00007 #include <bbgm/bbgm_image_sptr.h>
00008 #include <bbgm/bbgm_update.h>
00009 #include <bsta/bsta_attributes.h>
00010 #include <bsta/bsta_gauss_if3.h>
00011 #include <bsta/bsta_mixture.h>
00012 #include <bsta/algo/bsta_adaptive_updater.h>
00013 #include <brdb/brdb_value.h>
00014 #include <vbl/io/vbl_io_smart_ptr.h>
00015 #include <vil/vil_math.h>
00016 #include <vil/vil_convert.h>
00017 #include <vidl/vidl_istream_sptr.h>
00018 #include <vidl/vidl_frame.h>
00019 #include <vidl/vidl_convert.h>
00020 #include <vcl_iostream.h>
00021 
00022 namespace {
00023   vidl_istream_sptr istr = 0;
00024   unsigned ni = 0;
00025   unsigned nj = 0;
00026 };
00027 
00028 bool bbgm_update_dist_image_stream_process_cons(bprb_func_process& pro)
00029 {
00030   vcl_vector<vcl_string> in_types(9), out_types(1);
00031   in_types[0]= "bbgm_image_sptr";//pointer to initial distribution image (typically null)
00032   in_types[1]= "vidl_istream_sptr";//the video stream
00033   in_types[2]= "int"; //max_components
00034   in_types[3]= "int"; //window size
00035   in_types[4]= "float"; //initial_variance
00036   in_types[5]= "float"; //g_thresh
00037   in_types[6]= "float"; //min_stdev
00038   in_types[7]= "int"; // start frame number
00039   in_types[8]= "int"; // end frame number
00040   pro.set_input_types(in_types);
00041 
00042   out_types[0]="bbgm_image_sptr";// the updated distribution image
00043   pro.set_output_types(out_types);
00044   return true;
00045 }
00046 
00047 bool bbgm_update_dist_image_stream_process_init(bprb_func_process& pro)
00048 {
00049   //extract the stream
00050   istr = pro.get_input<vidl_istream_sptr>(1);
00051   if (!(istr && istr->is_open())) {
00052     vcl_cerr << "In bbgm_update_dist_image_stream_process::init() -"
00053              << " invalid input stream\n";
00054     return false;
00055   }
00056   if (istr->is_seekable())
00057     istr->seek_frame(0);
00058   vidl_frame_sptr f = istr->current_frame();
00059   if (!f) {
00060     vcl_cerr << "In bbgm_update_dist_image_stream_process::init() -"
00061              << " invalid initial frame\n";
00062     return false;
00063   }
00064   ni = f->ni(); nj = f->nj();
00065   vcl_cout << " initialized, stream frame size: " << ni << ", " << nj << ", stream at frame # " << istr->frame_number() << vcl_endl;
00066   vcl_cout.flush();
00067 
00068   pro.set_input(0, new brdb_value_t<bbgm_image_sptr>(0));
00069 
00070   return true;
00071 }
00072 
00073 //: Execute the process function
00074 bool bbgm_update_dist_image_stream_process(bprb_func_process& pro)
00075 {
00076   // Sanity check
00077   if (!pro.verify_inputs()) {
00078     vcl_cerr << "In bbgm_update_dist_image_stream_process::execute() -"
00079              << " invalid inputs\n";
00080     return false;
00081   }
00082 
00083   // Retrieve background image
00084   bbgm_image_sptr bgm = pro.get_input<bbgm_image_sptr>(0);
00085 
00086   //Retrieve max components
00087   int max_components = pro.get_input<int>(2);
00088 
00089   //Retrieve window_size
00090   int window_size = pro.get_input<int>(3);
00091 
00092   //Retrieve initial_variance
00093   float initial_variance = pro.get_input<float>(4);
00094 
00095   //Retrieve g_thresh
00096   float g_thresh = pro.get_input<float>(5);
00097 
00098   //Retrieve min_stdev
00099   float min_stdev = pro.get_input<float>(6);
00100 
00101   //Retrieve start frame number
00102   int start_frame = pro.get_input<int>(7);
00103 
00104   //Retrieve end frame number
00105   int end_frame = pro.get_input<int>(8);
00106 
00107   vcl_cout << " will start at frame # " << start_frame << " will end at frame # " << end_frame << vcl_endl;
00108 
00109   typedef bsta_gauss_if3 bsta_gauss_t;
00110   typedef bsta_gauss_t::vector_type vector_;
00111   typedef bsta_num_obs<bsta_gauss_if3> gauss_type;
00112   typedef bsta_mixture<gauss_type> mix_gauss_type;
00113   typedef bsta_num_obs<mix_gauss_type> obs_mix_gauss_type;
00114 
00115   // get the templated mixture model
00116   bbgm_image_sptr model_sptr;
00117   if (!bgm) {
00118     model_sptr = new bbgm_image_of<obs_mix_gauss_type>(ni, nj, obs_mix_gauss_type());
00119     vcl_cout << " Initialized the bbgm image\n";
00120     vcl_cout.flush();
00121   }
00122   else model_sptr = bgm;
00123   bbgm_image_of<obs_mix_gauss_type> *model =
00124     static_cast<bbgm_image_of<obs_mix_gauss_type>*>(model_sptr.ptr());
00125 
00126   bsta_gauss_t init_gauss(vector_(0.0f), vector_(initial_variance) );
00127 
00128 #if 0
00129   bsta_mg_window_updater<mix_gauss_type> updater( init_gauss,
00130                                                   max_components);
00131 #endif
00132   bsta_mg_grimson_window_updater<mix_gauss_type> updater( init_gauss,
00133                                                           max_components,
00134                                                           g_thresh,
00135                                                           min_stdev,
00136                                                           window_size);
00137 
00138 
00139   while (istr->advance() && (end_frame<0||(int)(istr->frame_number()) <= end_frame)) {
00140     // get frame from stream
00141     if ((int)(istr->frame_number()) >= start_frame) {
00142       vidl_frame_sptr f = istr->current_frame();
00143       vil_image_view_base_sptr fb = vidl_convert_wrap_in_view(*f);
00144       if (!fb)
00145         return false;
00146       vil_image_view<float> frame = *vil_convert_cast(float(), fb);
00147       if (fb->pixel_format() == VIL_PIXEL_FORMAT_BYTE)
00148         vil_math_scale_values(frame,1.0/255.0);
00149 
00150       update(*model,frame,updater);
00151       vcl_cout << "updated frame # "<< istr->frame_number()
00152                << " format " << fb->pixel_format() << " nplanes "
00153                << fb->nplanes()<< '\n';
00154       vcl_cout.flush();
00155     }
00156   }
00157 
00158   brdb_value_sptr output = new brdb_value_t<bbgm_image_sptr>(model_sptr);
00159   pro.set_output(0, output);
00160   return true;
00161 }
00162