1 #ifndef bwm_observer_img_h_
2 #define bwm_observer_img_h_
3 //:
4 // \file
5 
6 #include <vector>
7 #include <iostream>
8 #include <map>
9 #ifdef _MSC_VER
10 #  include <vcl_msvc_warnings.h>
11 #endif
12 
13 #include <bwm/algo/bwm_soview2D_vertex.h>
14 
15 #include <vgui/vgui_viewer2D_tableau.h>
16 
17 #include <vil/vil_image_view_base.h>
18 #include <bgui/bgui_image_tableau.h>
19 #include <bgui/bgui_vsol2D_tableau.h>
20 #include <bgui/bgui_vsol_soview2D.h>
21 
22 #include <vsol/vsol_point_2d_sptr.h>
23 #include <vsol/vsol_box_2d_sptr.h>
24 #include <vsol/vsol_polygon_2d_sptr.h>
25 #include <vsol/vsol_polyline_2d_sptr.h>
26 #include <vsol/vsol_digital_curve_2d_sptr.h>
27 #include <vsol/vsol_line_2d_sptr.h>
28 
29 #include <bvgl/bvgl_change_obj_sptr.h>
30 #include <bvgl/bvgl_changes_sptr.h>
31 
32 #define VERTEX_TYPE "bwm_soview2D_vertex"
33 #define LINE_SEG_TYPE "bgui_vsol_soview2D_line_seg"
34 #define POLYLINE_TYPE "bgui_vsol_soview2D_polyline"
35 #define POLYGON_TYPE "bgui_vsol_soview2D_polygon"
36 #define POINT_TYPE "bgui_vsol_soview2D_point"
37 
38 class bwm_observer_img : public bgui_vsol2D_tableau
39 {
40  public:
41 
42   enum BWM_2D_DRAW_MODE {MODE_2D_POLY, MODE_2D_EDGE, MODE_2D_VERTEX, MODE_2D_UNDEF};
43 
44   typedef bgui_vsol2D_tableau base;
45 
46   bwm_observer_img(bgui_image_tableau_sptr const& img, std::string name,
47                    std::string image_path, bool display_image_path);
48 
bwm_observer_img(bgui_image_tableau_sptr const & img)49   bwm_observer_img(bgui_image_tableau_sptr const& img)
50   : bgui_vsol2D_tableau(img), lock_vgui_status_(false), vgui_status_on_(false), draw_mode_(MODE_2D_POLY),
51     img_tab_(img), viewer_(nullptr), mask_(nullptr), change_type_("change"), show_image_path_(false),
52     start_x_(0), start_y_(0), moving_p_(nullptr), moving_v_(nullptr), moving_vertex_(false), moving_polygon_(false),
53     in_jog_mode_(false), row_(0), col_(0) {}
54 
55   virtual ~bwm_observer_img();
56 
image_tableau()57   bgui_image_tableau_sptr image_tableau() { return img_tab_; }
58 
ni()59   unsigned ni(){if(img_tab_)return img_tab_->width(); return 0;}
nj()60   unsigned nj(){if(img_tab_)return img_tab_->height(); return 0;}
61 
set_viewer(vgui_viewer2D_tableau_sptr viewer)62   void set_viewer(vgui_viewer2D_tableau_sptr viewer) { viewer_ = viewer; }
63 
set_tab_name(std::string name)64   virtual void set_tab_name(std::string name) { tab_name_.assign(name); }
65 
tab_name()66   std::string tab_name() const { return tab_name_; }
67 
68   bool handle(const vgui_event &);
69 
type_name()70   virtual std::string type_name() const { return "bwm_observer_img"; }
71 
72   unsigned create_box(vsol_box_2d_sptr);
73 
74   virtual unsigned create_polygon(vsol_polygon_2d_sptr);
75 
76   unsigned create_polyline(vsol_polyline_2d_sptr);
77 
78   unsigned create_point(vsol_point_2d_sptr);
79 
add_obj(bgui_vsol_soview2D * obj)80   void add_obj(bgui_vsol_soview2D* obj){
81     if(!obj) return;
82     obj_list[obj->get_id()] = obj;
83   }
84   void copy();
85 
86   void paste(float x, float y);
87 
88   void clear_objects();
89   //: various types of selected objects
90   bool get_selected_line(bgui_vsol_soview2D_line_seg* &line);
91   void print_selected_line();
92   bool get_selected_box(bgui_vsol_soview2D_polygon* &box);
93   bool get_selected_poly(bgui_vsol_soview2D_polygon* &poly);
94 
95   //: clears the edges inside a selected box
96   void clear_box();
97 
98   void delete_selected();
99 
100   virtual void delete_all();
101 
102   void hist_plot();
103   void hist_plot_in_poly();
104 
105   void intensity_profile(float start_col, float start_row,
106                          float end_col, float end_row);
107 
108   void range_map();
109 
110   void step_edges_vd();
111 
112   void lines_vd();
113 
114   void recover_edges();
115 
116   void recover_lines();
117 
118   bool crop_image(vil_image_resource_sptr& chip);
119 
120   //:moves to a specified position
121   void move_to_point(float x, float y);
122 
123   //: zoom out the image to fit the grid cell
124   void zoom_to_fit();
125 
126   //: for menu operation, calls move_to_point
127   void scroll_to_point();
128 
129   void toggle_show_image_path();
130 
set_grid_location(unsigned col,unsigned row)131   void set_grid_location(unsigned col, unsigned row){col_=col; row_ = row;}
row()132   unsigned row() const {return row_;}
col()133   unsigned col() const {return col_;}
134 
edges(unsigned id)135   std::vector<vsol_digital_curve_2d_sptr> edges(unsigned id)
136     {return edge_list[id];}
137 
138   void display_reg_seg(std::vector<vsol_digital_curve_2d_sptr> const& search_edges,
139                        std::vector<vsol_digital_curve_2d_sptr> const& model_edges);
140   void clear_reg_segmentation();
141 
image_path()142   std::string image_path() const {return img_tab_->file_name();}
143 
144   virtual void init_mask();
145   //: sets the change type for ground truth areas
146   void set_change_type();
147   virtual void add_poly_to_mask();
148   //void add_dontcare_poly_to_mask();
149   virtual void remove_poly_from_mask();
150   //void create_mask();
151   vil_image_view_base_sptr mask();
152   virtual bool save_changes_binary();
153   virtual bool load_changes_binary();
154 
155   //: lock/unlock the status display
lock_vgui_status(bool lock)156   void lock_vgui_status(bool lock){lock_vgui_status_ = lock;}
157   //: is status locked?
vgui_status_locked()158   bool vgui_status_locked() const {return lock_vgui_status_;}
159 
160   //is vgui status being displayed by *this observer?
vgui_status_on()161   bool vgui_status_on() const {return vgui_status_on_;}
162 
set_vgui_status_on(bool status_on)163   void set_vgui_status_on(bool status_on){vgui_status_on_ = status_on;}
164 
165   //: returns a list of all the existing spatial objects
166   std::vector<vsol_spatial_object_2d_sptr> get_spatial_objects_2d();
167 
168   //: set the draw mode to either polygon or vertex
169   void set_draw_mode(BWM_2D_DRAW_MODE mode);
170 
171  protected:
172   //:flags to indicate vgui status displays by observers
173 
174   //: lock status display when drawing or other motion tracking
175   bool lock_vgui_status_;
176 
177   //: observer status is being displayed,
178   //  so image pixel values should be blocked
179   bool vgui_status_on_;
180 
181   enum BWM_2D_DRAW_MODE draw_mode_;
182 
bwm_observer_img()183   bwm_observer_img() {}
184 
185   bgui_image_tableau_sptr img_tab_;
186 
187   vgui_viewer2D_tableau_sptr viewer_;
188 
189   vil_image_view_base_sptr mask_;
190 
191   //: change areas, mapped to the soviewID for easy deletion
192   std::map<unsigned int, bvgl_change_obj_sptr> change_polys_;
193 
194   //: ground truth areas
195   bvgl_changes_sptr ground_truth_;
196 
197   //: the current ground truth change type
198   std::string change_type_;
199 
200   bool show_image_path_;
201 
202   // polygons are mapped soview ID
203   std::map<unsigned, bgui_vsol_soview2D*> obj_list;
204 
205   // vector of vertices are mapped soview ID for each polygon
206   std::map<unsigned, std::vector<bwm_soview2D_vertex* > > vert_list;
207 
208   // maps for box segmentations
209   std::map<unsigned, std::vector<vsol_digital_curve_2d_sptr > > edge_list;
210   std::map<unsigned, std::vector<vsol_line_2d_sptr > > line_list;
211   std::map<unsigned, std::vector<bgui_vsol_soview2D*> > seg_views;
212   // storage for registration edge views
213   std::vector<bgui_vsol_soview2D*> reg_seg_views_;
214 
215   float start_x_, start_y_;
216   bgui_vsol_soview2D* moving_p_;
217   bwm_soview2D_vertex* moving_v_;
218   bool moving_vertex_, moving_polygon_;
219   bool in_jog_mode_;
220 
221   //: polygon selected to copy
222   vgui_soview2D* copy_obj_;
223 
224   virtual void delete_polygon(vgui_soview* obj);
225   void delete_vertex(vgui_soview* vertex);
226 
227   //: returns the type of selected object iff there is one object. If warn is true, then issue a warning if the object is not found.
228   vgui_soview2D* get_selected_object(std::string type, bool warn = false);
229 
230   //: returns a list of selected object from the given type
231   std::vector<vgui_soview2D*> get_selected_objects(std::string type);
232 
233   std::string tab_name_;
234 
235   std::vector<std::string> change_choices_;
236 
237   unsigned row_; //location of observer in grid
238   unsigned col_;
239 };
240 
241 #endif
242