1 // This is brl/bseg/brec/brec_part_hierarchy.h
2 #ifndef brec_part_hierarchy_h_
3 #define brec_part_hierarchy_h_
4 //:
5 // \file
6 // \brief class to represent a hierarchy of composable parts for recognition
7 //
8 // \author Ozge C Ozcanli (ozge@lems.brown.edu)
9 // \date Oct 16, 2008
10 //
11 // \verbatim
12 //  Modifications
13 //  <none yet>
14 // \endverbatim
15 
16 #include <iostream>
17 #include <bgrl2/bgrl2_graph.h>
18 #include "brec_part_base.h"
19 #include "brec_hierarchy_edge.h"
20 #include "brec_part_base_sptr.h"
21 #include "brec_hierarchy_edge_sptr.h"
22 
23 #include <vil/vil_image_view.h>
24 #ifdef _MSC_VER
25 #  include <vcl_msvc_warnings.h>
26 #endif
27 #include <vsl/vsl_binary_io.h>
28 
29 class brec_part_hierarchy : public bgrl2_graph<brec_part_base , brec_hierarchy_edge>
30 {
31  public:
32 
33   brec_part_base_sptr get_node(unsigned layer, unsigned type);
34   brec_part_instance_sptr get_node_instance(unsigned layer, unsigned type);
35 
36   static void generate_map(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<float>& map, vil_image_view<unsigned>& type_map);
37   static void generate_map(std::vector<brec_part_instance_sptr>& extracted_parts, std::vector<std::vector<brec_part_instance_sptr> >& map);
38   //: generate a float map with normalized strengths and receptive fields marked
39   static void generate_output_map(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<float>& map);
40 
41   //: generate a float map with various posterior
42   static void generate_output_map_posterior(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<float>& map, unsigned type = brec_posterior_types::CLASS_FOREGROUND);
43   static void generate_output_map_posterior_centers(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<float>& map, unsigned type = brec_posterior_types::CLASS_FOREGROUND);
44 
45   //: stretch the values to be used for imaging
46   static void generate_output_map3(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<float>& map);
47 
48   //: output_img needs to have 3 planes
49   static void generate_output_img(std::vector<brec_part_instance_sptr>& extracted_parts, vil_image_view<vxl_byte>& input_img, vil_image_view<vxl_byte>& output_img, unsigned posterior_type);
50 
51   //: check for existence of upper_p with central_p as its central part and map will tell if all the other parts exist
52   brec_part_instance_sptr exists(const brec_part_base_sptr& upper_p, const brec_part_instance_sptr& central_p, vil_image_view<float>& map, vil_image_view<unsigned>& type_map, std::vector<std::vector<brec_part_instance_sptr> >& part_map, float det_threshold);
53 
54   //: given a set of detected lower level parts, create a set of instance detections for one layer above in the hierarchy
55   void extract_upper_layer(std::vector<brec_part_instance_sptr>& extracted_parts,
56                            unsigned ni, unsigned nj, std::vector<brec_part_instance_sptr>& extracted_upper_parts);
57 
add_dummy_primitive_instance(brec_part_instance_sptr p)58   void add_dummy_primitive_instance(brec_part_instance_sptr p) { dummy_primitive_instances_.push_back(p); }
get_dummy_primitive_instances()59   std::vector<brec_part_instance_sptr>& get_dummy_primitive_instances() { return dummy_primitive_instances_; }
60 
61 #if 0 // Ozge TODO: adapt to different posterior types
62   //: compute the probabilistic existence score for a given part (upper_p) if this one or equivalent ones exist in this hierarchy
63   //  Equivalence is determined by the types of the primitive layer parts
64   //  (i.e. all layer 1 parts with alpha and alpha_prime as primitive parts are equivalent)
65   bool get_score(brec_part_instance_sptr upper_p, std::vector<double>& scores);
66   bool get_score_helper(brec_part_instance_sptr ins_p, brec_part_base_sptr p, double& score);
67 #endif
68 
69   unsigned highest_layer_id();
70   //: number of vertices in the layer
71   unsigned layer_cnt(unsigned layer);
72 
73   //: name will be used to create training directories
set_name(std::string name)74   void set_name(std::string name) { name_ = name; }
name()75   std::string name() const { return name_; }
76 
set_model_dir(std::string dir)77   void set_model_dir(std::string dir) { model_dir_ = dir; }
model_dir()78   std::string model_dir() const { return model_dir_; }
79 
80   void write_xml(std::ostream& os);
81   bool read_xml(std::istream& is);
82 
83   //: draw a ps image with sampled parts, draw N samples from the distributions
84   bool draw_to_ps(unsigned N, const std::string& output_img, float drawing_radius);
85 
86   //: draw the nodes of the given layer side by side to the output image
87   bool draw_to_image(unsigned N, unsigned layer_id, float drawing_radius, const std::string& output_img);
88 
89  public:
90   //: a map to store dummy instances of primitive parts, so that they could be extracted properly for a constructed hierarchy
91   std::vector<brec_part_instance_sptr> dummy_primitive_instances_;
92 
93   std::string name_;
94 
95   //: after constructing the background and foreground response models for the primitive instances, the name of the directory to load these models should be saved in the hierarchy
96   std::string model_dir_;
97 };
98 
99 // Binary io, NOT IMPLEMENTED, signatures defined to use brec_part_hierarchy as a brdb_value
100 void vsl_b_write(vsl_b_ostream & os, brec_part_hierarchy const &ph);
101 void vsl_b_read(vsl_b_istream & is, brec_part_hierarchy &ph);
102 void vsl_b_read(vsl_b_istream& is, brec_part_hierarchy* ph);
103 void vsl_b_write(vsl_b_ostream& os, const brec_part_hierarchy* &ph);
104 
105 #endif  //brec_part_hierarchy_h_
106