contrib/brl/bseg/bmdl/pro/processes/bmdl_classify_process.cxx
Go to the documentation of this file.
00001 #include <bprb/bprb_func_process.h>
00002 //:
00003 // \file
00004 
00005 #include <vil/vil_image_view.h>
00006 #include <vil/vil_pixel_format.h>
00007 #include <bprb/bprb_parameters.h>
00008 #include <bmdl/bmdl_classify.h>
00009 
00010 #define PARAM_GROUND_THRESH "ground_threshold"
00011 #define PARAM_VEG_THRESH    "vegetation_threshold"
00012 #define PARAM_AREA_THRESH   "area_threshold"
00013 #define PARAM_HEIGHT_RES    "height_resolution"
00014 
00015 template vil_image_view_base_sptr bprb_func_process::get_input(unsigned);
00016 
00017 template <class T>
00018 bool classify(const vil_image_view<T>& lidar_first,
00019               const vil_image_view<T>& lidar_last,
00020               const vil_image_view<T>& ground,
00021               vil_image_view<unsigned int>& label_img,
00022               vil_image_view<T>& height_img,
00023               T gthresh, T vthresh, T athresh, T hres)
00024 {
00025   bmdl_classify<T> classifier((unsigned)(athresh+0.5), hres, gthresh, vthresh);
00026   classifier.set_lidar_data(lidar_first,lidar_last);
00027   classifier.set_bare_earth(ground);
00028   classifier.label_lidar();
00029   label_img = classifier.labels();
00030   height_img = classifier.heights();
00031   return true;
00032 }
00033 
00034 
00035 bool classify(vil_image_view_base_sptr lidar_first,
00036               vil_image_view_base_sptr lidar_last,
00037               vil_image_view_base_sptr ground,
00038               vil_image_view_base_sptr& label_img,
00039               vil_image_view_base_sptr& height_img,
00040               float gthresh, float vthresh, float athresh, float hres)
00041 {
00042   label_img = new vil_image_view<unsigned int>();
00043 
00044   // use the float version
00045   if (lidar_first->pixel_format() == VIL_PIXEL_FORMAT_FLOAT)
00046   {
00047     if (lidar_last->pixel_format() == VIL_PIXEL_FORMAT_FLOAT)
00048     {
00049       if (ground->pixel_format() == VIL_PIXEL_FORMAT_FLOAT) {
00050         height_img = new vil_image_view<float>();
00051         return classify<float>(lidar_first, lidar_last, ground,
00052                                (vil_image_view<unsigned int>&)(*label_img),
00053                                (vil_image_view<float>&)(*height_img), gthresh, vthresh, athresh, hres);
00054       }
00055     }
00056     else
00057     {
00058       vcl_cout << "input images have different bit depths" << vcl_endl;
00059       return false;
00060     }
00061   }
00062 
00063   // use the double version
00064   else if (lidar_first->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE)
00065   {
00066     if (lidar_last->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE)
00067     {
00068       if (ground->pixel_format() == VIL_PIXEL_FORMAT_DOUBLE) {
00069         height_img = new vil_image_view<double>();
00070         return classify<double>(lidar_first, lidar_last, ground,
00071                                 (vil_image_view<unsigned int>&)(*label_img),
00072                                 (vil_image_view<double>&)(*height_img),
00073                                 (double) gthresh, (double) vthresh, (double) athresh, (double) hres);
00074       }
00075     }
00076     else
00077     {
00078       vcl_cout << "input images have different bit depths" << vcl_endl;
00079       return false;
00080     }
00081   }
00082 
00083   return false;
00084 }
00085 
00086 bool bmdl_classify_process(bprb_func_process& pro)
00087 {
00088   if (pro.n_inputs()< 3) {
00089     vcl_cout << "lidar_roi_process: The input number should be 3" << vcl_endl;
00090     return false;
00091   }
00092 
00093   // get the inputs:
00094   unsigned int i=0;
00095   vil_image_view_base_sptr first_ret = pro.get_input<vil_image_view_base_sptr>(i++);
00096   vil_image_view_base_sptr last_ret = pro.get_input<vil_image_view_base_sptr>(i++);
00097   vil_image_view_base_sptr ground = pro.get_input<vil_image_view_base_sptr>(i++);
00098 
00099   //pro.set_input_types
00100   // check first return's validity
00101   if (!first_ret) {
00102     vcl_cout << "bmdl_classify_process -- First return image is not valid!\n";
00103     return false;
00104   }
00105 
00106   // check last return's validity
00107   if (!last_ret) {
00108     vcl_cout << "bmdl_classify_process -- Last return image is not valid!\n";
00109     return false;
00110   }
00111 
00112   // check last return's validity
00113   if (!ground) {
00114     vcl_cout << "bmdl_classify_process -- Ground image is not valid!\n";
00115     return false;
00116   }
00117 
00118   // read the parameters
00119   float gthresh=0.0f, vthresh = 0.0f, hres=0.0f; // dummy initialization, to avoid compiler warning
00120   float athresh=0.0f;
00121 
00122   if (!pro.parameters()->get_value(PARAM_GROUND_THRESH, gthresh) ||
00123       !pro.parameters()->get_value(PARAM_VEG_THRESH, vthresh) ||
00124       !pro.parameters()->get_value(PARAM_AREA_THRESH, athresh) ||
00125       !pro.parameters()->get_value(PARAM_HEIGHT_RES, hres)) {
00126     vcl_cout << "bmdl_classify_process -- problem getting the parameters!\n";
00127     return false;
00128   }
00129 
00130   vil_image_view_base_sptr label_img=0, height_img=0;
00131   if (!classify(first_ret, last_ret, ground, label_img, height_img, gthresh, vthresh, athresh, hres)) {
00132     vcl_cout << "bmdl_classify_process -- The process has failed!\n";
00133     return false;
00134   }
00135   i=0;
00136   pro.set_output_val<vil_image_view_base_sptr>(i, label_img);
00137   pro.set_output_val<vil_image_view_base_sptr>(i++, height_img);
00138   return true;
00139 }
00140 
00141 bool bmdl_classify_process_cons(bprb_func_process& pro)
00142 {
00143   bool ok=false;
00144   vcl_vector<vcl_string> input_types;
00145   input_types.push_back("vil_image_view_base_sptr");
00146   input_types.push_back("vil_image_view_base_sptr");
00147   input_types.push_back("vil_image_view_base_sptr");
00148   ok = pro.set_input_types(input_types);
00149   if (!ok) return ok;
00150 
00151   vcl_vector<vcl_string> output_types;
00152   output_types.push_back("vil_image_view_base_sptr");  // label image
00153   output_types.push_back("vil_image_view_base_sptr");  // height image
00154   ok = pro.set_output_types(output_types);
00155   if (!ok) return ok;
00156 
00157   return true;
00158 }