1 //This is brl/bseg/bvxm/pro/processes/bvxm_create_mog_image_process.cxx
2 #include <iostream>
3 #include <string>
4 #include "bvxm_create_mog_image_process.h"
5 //:
6 // \file
7 
8 #include <bprb/bprb_parameters.h>
9 #include <bvxm/pro/processes/bvxm_normalization_util.h>
10 
11 #include "vil/vil_image_view_base.h"
12 #include "vil/vil_image_view.h"
13 
14 #include <bvxm/bvxm_image_metadata.h>
15 #include <bvxm/bvxm_voxel_world.h>
16 
17 #ifdef _MSC_VER
18 #  include "vcl_msvc_warnings.h"
19 #endif
20 
21 #include <brdb/brdb_value.h>
22 #include <brip/brip_vil_float_ops.h>
23 #include "vpgl/vpgl_camera_double_sptr.h"
24 
bvxm_create_mog_image_process_cons(bprb_func_process & pro)25 bool bvxm_create_mog_image_process_cons(bprb_func_process& pro)
26 {
27   //inputs
28 
29   //0: The voxel world
30   //1: The appearance model type
31   //2: The illumination bin index nplanes_
32   //3: The scale
33   //4: The camera
34   //5: ni, the size of the output MOG image
35   //6: nj
36   std::vector<std::string> input_types_;
37 
38   input_types_.emplace_back("bvxm_voxel_world_sptr");
39   input_types_.emplace_back("vcl_string");
40   input_types_.emplace_back("unsigned");
41   input_types_.emplace_back("unsigned");
42   input_types_.emplace_back("vpgl_camera_double_sptr");
43   input_types_.emplace_back("unsigned");
44   input_types_.emplace_back("unsigned");
45 
46   if (!pro.set_input_types(input_types_))
47     return false;
48 
49   //output
50   std::vector<std::string> output_types_;
51   output_types_.emplace_back("bvxm_voxel_slab_base_sptr");
52   return pro.set_output_types(output_types_);
53 }
54 
bvxm_create_mog_image_process(bprb_func_process & pro)55 bool bvxm_create_mog_image_process(bprb_func_process& pro)
56 {
57    //get the inputs
58   unsigned i = 0;
59   bvxm_voxel_world_sptr world = pro.get_input<bvxm_voxel_world_sptr>(i++);
60   std::string voxel_type = pro.get_input<std::string>(i++);
61   auto bin_index = pro.get_input<unsigned>(i++);;
62   auto scale_index = pro.get_input<unsigned>(i++);
63 
64   vpgl_camera_double_sptr camera = pro.get_input<vpgl_camera_double_sptr>(i++);
65 
66   auto ni = pro.get_input<unsigned>(i++);
67   auto nj = pro.get_input<unsigned>(i++);
68 
69   if (!camera) {
70     std::cout << pro.name() <<" :--  Input 1  is not valid!\n";
71     return false;
72   }
73   if (!world) {
74     std::cout << pro.name() <<" :--  Input 2  is not valid!\n";
75     return false;
76   }
77 
78   //get parameters and overwrite global values
79   std::string param_mog_method_ = "mog_method";
80   const std::string param_nsamples_ = "n_samples";
81 
82   unsigned n_samples = 10;
83   unsigned mog_creation_method_ = bvxm_mog_image_creation_methods::MOST_PROBABLE_MODE;
84 
85   //: assumes an xml file sets these values if defaults won't be used
86   pro.parameters()->get_value(param_mog_method_, mog_creation_method_);
87   pro.parameters()->get_value(param_nsamples_, n_samples);
88   bool verbose_ = false;
89   pro.parameters()->get_value("verbose", verbose_);
90 
91   //: the image in the observation is only used to determine \p ni and \p nj for the output MOG.
92   //  so create a dummy image
93   vil_image_view_base_sptr dummy_img = new vil_image_view<vxl_byte>(ni, nj, 1);
94   bvxm_image_metadata observation(dummy_img,camera);
95 
96   // if the world is not updated yet, we just return 0 as voxel slab base (the processes which use this slab should check whether its zero
97   unsigned num_observations = 0;
98   if (voxel_type == "apm_mog_grey")
99     num_observations = world->num_observations<APM_MOG_GREY>(bin_index,scale_index);
100   else if (voxel_type == "apm_mog_mc_3_3")
101     num_observations = world->num_observations<APM_MOG_MC_3_3>(bin_index,scale_index);
102   else if (voxel_type == "apm_mog_mc_4_3")
103     num_observations = world->num_observations<APM_MOG_MC_4_3>(bin_index,scale_index);
104   else{
105     std::cout << "In bvxm_normalize_image_process::execute() -- input appearance model: " << voxel_type << " is not supported\n";
106     return false;
107   }
108 
109   bvxm_voxel_slab_base_sptr mog_image;
110 
111   if (num_observations == 0)
112   {
113     pro.set_output_val<bvxm_voxel_slab_base_sptr>(0,mog_image); // returns empty pointer
114     return true;
115   }
116 
117   if (verbose_) {
118     switch (mog_creation_method_) {
119       case bvxm_mog_image_creation_methods::MOST_PROBABLE_MODE:
120       { std::cout << "using most probable modes' colors to create mog image "; } break;
121       case bvxm_mog_image_creation_methods::EXPECTED_VALUE:
122       { std::cout << "using expected colors to create mog image "; } break;
123       case bvxm_mog_image_creation_methods::SAMPLING:
124       { std::cout << "using random sampling to create mog image "; } break;
125       default:
126       { std::cout << "In bvxm_normalize_image_process::norm_parameters() - unrecognized option: " << mog_creation_method_ << " to create mog image\n"; return false; }
127     }
128   }
129 
130   bool good = false;
131   if (voxel_type == "apm_mog_grey")
132     good = mix_gaussian<APM_MOG_GREY>(world,mog_creation_method_,bin_index,scale_index,n_samples,observation,mog_image);
133   else if (voxel_type == "apm_mog_mc_3_3")
134     good = mix_gaussian<APM_MOG_MC_3_3>(world,mog_creation_method_,bin_index,scale_index,n_samples,observation,mog_image);
135   else if (voxel_type == "apm_mog_mc_4_3")
136     good = mix_gaussian<APM_MOG_MC_4_3>(world,mog_creation_method_, bin_index,scale_index,n_samples,observation,mog_image);
137   else{
138     std::cout << "In bvxm_normalize_image_process::execute() -- input appearance model: " << voxel_type << " is not supported\n";
139     return false;
140   }
141 
142   if (!good)
143     return false;
144 
145   unsigned j=0;
146   pro.set_output_val<bvxm_voxel_slab_base_sptr>(j++,mog_image);
147   return true;
148 }
149