1 #ifndef sdet_region_classifier_h_
2 #define sdet_region_classifier_h_
3 //---------------------------------------------------------------------
4 //:
5 // \file
6 // \brief A classifier for regions found by selective search
7 //
8 //
9 // \author
10 //  J.L. Mundy - December 26, 2016
11 //
12 // \verbatim
13 // Modifications
14 //  None
15 // \endverbatim
16 //
17 #include <iostream>
18 #include <map>
19 #include <queue>
20 #include <set>
21 #ifdef _MSC_VER
22 #  include <vcl_msvc_warnings.h>
23 #endif
24 #include <vil/vil_image_view.h>
25 #include "sdet_region.h"
26 #include "sdet_region_classifier_params.h"
27 #include "sdet_selective_search.h"
28 #include <bsta/bsta_histogram.h>
29 #include <vgl/vgl_box_2d.h>
30 #include <vgl/vgl_intersection.h>
31 #include <vgl/algo/vgl_rtree.h>
32 #include <vgl/algo/vgl_rtree_c.h>
33 #include <vgl/vgl_area.h>
34 // C must have the following (static method) signatures :
35 // \code
36 //   void  C::init  (B &, V const &);
37 //   void  C::update(B &, V const &);
38 //   void  C::update(B &, B const &);
39 //   bool  C::meet  (B const &, V const &);
40 //   bool  C::meet  (B const &, B const &);
41 //   float C::volume(B const &);
42 // \endcode
43 template <class T>
44 class sdet_rtree_region_box_2d
45 {
46   // only static methods
47   sdet_rtree_region_box_2d() = delete;
48   ~sdet_rtree_region_box_2d() = delete;
49  public:
50   typedef sdet_region_sptr v_type;
51   typedef vgl_box_2d<T> b_type;
52 
53   // Operations------
init(vgl_box_2d<T> & b,sdet_region_sptr const & r)54   static void  init  (vgl_box_2d<T>& b, sdet_region_sptr const& r)
55   { b = r->bbox();}
56 
update(vgl_box_2d<T> & b,sdet_region_sptr const & r)57   static void  update(vgl_box_2d<T>& b, sdet_region_sptr const& r)
58   { b.add(r->bbox());}
59 
update(vgl_box_2d<T> & b0,vgl_box_2d<T> const & b1)60   static void  update(vgl_box_2d<T>& b0, vgl_box_2d<T> const &b1)
61   { b0.add(b1.min_point());  b0.add(b1.max_point()); }
62 
meet(vgl_box_2d<T> const & b,sdet_region_sptr const & r)63   static bool  meet(vgl_box_2d<T> const& b, sdet_region_sptr const& r){
64     return sdet_rtree_region_box_2d::meet(b, r->bbox());
65   }
meet(vgl_box_2d<T> const & b0,vgl_box_2d<T> const & b1)66   static bool  meet(vgl_box_2d<T> const& b0, vgl_box_2d<T> const& b1) {
67     vgl_box_2d<float> intr = vgl_intersection<float>(b0, b1);
68     return !intr.is_empty();
69   }
volume(vgl_box_2d<T> const & b)70   static float volume(vgl_box_2d<T> const& b)
71   { return static_cast<float>(vgl_area(b)); }
72 
73   // point meets for a polygon, used by generic rtree probe
meets(sdet_region_sptr const & r,vgl_polygon<T> poly)74   static bool meets(sdet_region_sptr const& r, vgl_polygon<T> poly)
75   { return vgl_intersection<T>(r->bbox(), poly);}
76 
77   // box meets for a polygon, used by generic rtree probe
meets(vgl_box_2d<T> const & b,vgl_polygon<T> poly)78   static bool meets(vgl_box_2d<T> const& b, vgl_polygon<T> poly)
79   { return vgl_intersection<T>(b, poly); }
80 };
81 
82 class sdet_region_classifier : public sdet_region_classifier_params
83 {
84  public:
85   typedef sdet_rtree_region_box_2d<float> C_; // the helper class
86   typedef C_::v_type V_; // the contained object type
87   typedef C_::b_type B_; // the bounding object type
88   //Constructors/destructor
89   //:use default parameters
90   sdet_region_classifier()= default;
91   //:specify different parameters
sdet_region_classifier(sdet_region_classifier_params & rpp)92  sdet_region_classifier(sdet_region_classifier_params& rpp):sdet_region_classifier_params(rpp){}
93   ~sdet_region_classifier() override= default;;
set_diverse_regions(const std::map<unsigned,sdet_region_sptr> & regions)94   void set_diverse_regions(const std::map<unsigned, sdet_region_sptr >& regions){diverse_regions_ = regions;}
diverse_regions()95   const std::map<unsigned, sdet_region_sptr>& diverse_regions(){return diverse_regions_;}
set_diverse_hists(const std::map<unsigned,bsta_histogram<float>> & hists)96   void set_diverse_hists(const std::map<unsigned, bsta_histogram<float> >& hists){diverse_hists_ = hists;}
diverse_hists()97   const std::map<unsigned, bsta_histogram<float> >& diverse_hists() {return diverse_hists_;}
neighbors_hists()98   const std::map<unsigned, bsta_histogram<float> >& neighbors_hists() {return neighbors_hists_;}
iou_clusters()99   const std::map< unsigned, std::map<unsigned, float> >& iou_clusters(){return iou_clusters_;}
cluster_sim()100   const std::map< unsigned, std::map<unsigned, region_sim> >& cluster_sim(){return cluster_similarity_;}
101   void find_iou_clusters(const std::map<unsigned, sdet_region_sptr >& regions);
102   void remove_diverse_region(unsigned label);
103   void compute_iou_cluster_similarity();
104   float compute_partition_quality(std::map< unsigned, std::map<unsigned, region_sim> > const& cluster_sim);
105   bool merge_similarity_map(std::map< unsigned, std::map<unsigned, region_sim> > const& sim_before,
106                             std::map< unsigned, std::map<unsigned, region_sim> >& sim_after,
107                             unsigned labi, unsigned labj, unsigned new_label);
108   void compute_hist_of_nbrs();
109   void compute_bright_regions();
bright_regions()110   const std::set<unsigned>& bright_regions(){return bright_regions_;}
process()111   void process(){
112   }
113     //: io functions
114  private:
115   std::map<unsigned, sdet_region_sptr> diverse_regions_;
116   std::map<unsigned, bsta_histogram<float> >  diverse_hists_;
117   std::map<unsigned, bsta_histogram<float> >  neighbors_hists_;
118   std::set<unsigned> bright_regions_;
119   std::map< unsigned, std::map<unsigned, float> > iou_clusters_;
120   std::map< unsigned, std::map<unsigned, region_sim> > cluster_similarity_;
121 };
122 #endif // sdet_region_classifier_h_
123