00001
00002 #ifndef bbgm_features_h_
00003 #define bbgm_features_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <brip/brip_rect_mask.h>
00016 #include <vgl/vgl_box_2d.h>
00017 #include <vgl/vgl_polygon.h>
00018 #include <vsl/vsl_binary_io.h>
00019 #include <vsl/vsl_set_io.h>
00020 #include <vcl_vector.h>
00021 #include <vcl_set.h>
00022 #include <vcl_iosfwd.h>
00023
00024
00025
00026
00027
00028
00029 class bbgm_mask_feature
00030 {
00031 public:
00032 bbgm_mask_feature():
00033 mid_(static_cast<unsigned char>(brip_rect_mask::ntypes)),
00034 aid_(static_cast<unsigned char>(brip_rect_mask::nangles)), p_(0.0f)
00035 {id_ = uid_++;}
00036
00037 bbgm_mask_feature(brip_rect_mask::mask_id mid, brip_rect_mask::ang_id aid):
00038 mid_(mid), aid_(aid), p_(0.0f)
00039 {id_ = uid_++;}
00040
00041
00042 void set_prob(brip_rect_mask::mask_id mid, brip_rect_mask::ang_id aid, float p)
00043 {
00044 if (p>p_) {
00045 mid_ = static_cast<unsigned char>(mid);
00046 aid_ = static_cast<unsigned char>(aid);
00047 p_=p;
00048 }
00049 }
00050
00051
00052 void force_prob(float p) {p_ = p;}
00053
00054
00055 brip_rect_mask::mask_id mask_id() const
00056 {return static_cast<brip_rect_mask::mask_id>(mid_);}
00057
00058
00059 brip_rect_mask::ang_id ang_id() const
00060 {return static_cast<brip_rect_mask::ang_id>(aid_);}
00061
00062 unsigned ni() const;
00063
00064 unsigned nj() const;
00065
00066
00067 float operator () () const { return p_;}
00068
00069
00070 unsigned id() const {return id_;}
00071
00072
00073 vcl_vector<vgl_point_2d<unsigned short> > pixels(unsigned i, unsigned j);
00074
00075
00076
00077
00078
00079
00080
00081 virtual vcl_string is_a() const;
00082
00083 virtual bbgm_mask_feature* clone() const;
00084
00085
00086 short version() const;
00087
00088
00089 virtual void b_write(vsl_b_ostream &os) const;
00090
00091
00092 virtual void b_read(vsl_b_istream &is);
00093
00094 protected:
00095 static unsigned uid_;
00096 unsigned id_;
00097 unsigned char mid_;
00098 unsigned char aid_;
00099 float p_;
00100 };
00101
00102
00103 void vsl_print_summary(vcl_ostream& os,
00104 const bbgm_mask_feature& b);
00105
00106
00107
00108 void vsl_b_write(vsl_b_ostream &os, const bbgm_mask_feature& b);
00109
00110
00111
00112 void vsl_b_read(vsl_b_istream &is, bbgm_mask_feature& b);
00113
00114
00115
00116
00117
00118
00119
00120 class bbgm_mask_pair_feature
00121 {
00122 public:
00123 bbgm_mask_pair_feature(): mid_(brip_rect_mask::ntypes),
00124 ang0_(brip_rect_mask::nangles), ang1_(brip_rect_mask::nangles),
00125 i0_(0),j0_(0),i1_(0),j1_(0),p_(0.0f) { id_ = uid_++; }
00126
00127 void set_prob(unsigned short i0, unsigned short j0,
00128 unsigned short i1, unsigned short j1, float p)
00129 {
00130 i0_ = i0; j0_ = j0;
00131 i1_ = i1; j1_ = j1;
00132 p_=p;
00133 }
00134 void set_mask_type(brip_rect_mask::mask_id mid) { mid_ = mid; }
00135 void set_angles(brip_rect_mask::ang_id ang0,
00136 brip_rect_mask::ang_id ang1)
00137 {ang0_ = ang0; ang1_ = ang1;}
00138
00139
00140 void set_ids( unsigned id0, unsigned id1) { id0_ = id0; id1_ = id1; }
00141
00142
00143 void x0(unsigned short& i0, unsigned short& j0) const { i0 = i0_; j0 = j0_; }
00144 void x1(unsigned short& i1, unsigned short& j1) const { i1 = i1_; j1 = j1_; }
00145
00146
00147 brip_rect_mask::mask_id mask_id() const {return mid_;}
00148
00149
00150 brip_rect_mask::ang_id ang0() const {return ang0_;}
00151
00152
00153 brip_rect_mask::ang_id ang1() const {return ang1_;}
00154
00155
00156 unsigned id0() const {return id0_;}
00157
00158
00159 unsigned id1() const {return id1_;}
00160
00161 void center(unsigned short& ic, unsigned short& jc) const
00162 {
00163 ic = (unsigned short)((i0_+i1_)/2);
00164 jc = (unsigned short)((j0_+j1_)/2);
00165 }
00166
00167 float operator () () const { return p_;}
00168
00169
00170 unsigned id() const {return id_;}
00171
00172
00173 vcl_vector<vgl_point_2d<unsigned short> > pixels();
00174
00175
00176
00177
00178
00179
00180
00181 virtual vcl_string is_a() const;
00182
00183 virtual bbgm_mask_pair_feature* clone() const;
00184
00185
00186 short version() const;
00187
00188
00189 virtual void b_write(vsl_b_ostream &os) const;
00190
00191
00192 virtual void b_read(vsl_b_istream &is);
00193
00194 protected:
00195 static unsigned uid_;
00196 unsigned id_;
00197 brip_rect_mask::mask_id mid_;
00198 brip_rect_mask::ang_id ang0_;
00199 brip_rect_mask::ang_id ang1_;
00200 unsigned short i0_, j0_;
00201 unsigned short i1_, j1_;
00202 unsigned id0_, id1_;
00203 float p_;
00204 };
00205
00206
00207 void vsl_print_summary(vcl_ostream& os,
00208 const bbgm_mask_pair_feature& b);
00209
00210
00211
00212 void vsl_b_write(vsl_b_ostream &os, const bbgm_mask_pair_feature& b);
00213
00214
00215
00216 void vsl_b_read(vsl_b_istream &is, bbgm_mask_pair_feature& b);
00217
00218
00219
00220 bool pair_intersect(bbgm_mask_pair_feature const& mp0,
00221 bbgm_mask_pair_feature const& mp1,
00222 bool plus_intersect_only = true);
00223
00224
00225
00226
00227
00228
00229 class fless
00230 {
00231 public:
00232 fless() {}
00233 bool operator ()(bbgm_mask_pair_feature const& fa,
00234 bbgm_mask_pair_feature const& fb) {
00235 unsigned short ica, jca, icb, jcb;
00236 fa.center(ica, jca);
00237 fb.center(icb, jcb);
00238 if (ica!=icb)
00239 return ica<icb;
00240 return jca<jcb;
00241 }
00242 };
00243
00244
00245
00246 class bbgm_pair_group_feature
00247 {
00248 public:
00249 bbgm_pair_group_feature(): mid_(brip_rect_mask::ntypes), ci_(0), cj_(0), p_(0) { id_ = uid_++; }
00250
00251 bbgm_pair_group_feature(bbgm_mask_pair_feature const& mp);
00252
00253 void set_prob(vcl_set<bbgm_mask_pair_feature, fless >const& pairs, float p)
00254 {
00255 pairs_ = pairs;
00256 vcl_set<bbgm_mask_pair_feature, fless >::const_iterator pit = pairs.begin();
00257 float cif = 0.0f, cjf = 0.0f;
00258 unsigned np = 0;
00259 for (; pit != pairs.end(); ++pit, ++np)
00260 {
00261 unsigned short ci, cj;
00262 pit->center(ci, cj);
00263 cif += (float)ci;
00264 cjf += (float)cj;
00265 }
00266 if (!np) return;
00267 ci_ = static_cast<unsigned short>(cif/(float)np);
00268 cj_ = static_cast<unsigned short>(cjf/(float)np);
00269 p_=p;
00270 }
00271
00272 void set_prob(float p) { p_ = p; }
00273
00274
00275 void set_mask_id(brip_rect_mask::mask_id mid) { mid_ = mid; }
00276
00277 unsigned n_pairs() const {return pairs_.size();}
00278
00279 const vcl_set<bbgm_mask_pair_feature, fless>& pairs() const
00280 { return pairs_; }
00281
00282
00283 float operator () () const { return p_;}
00284
00285 unsigned id() const {return id_;}
00286
00287
00288 brip_rect_mask::mask_id mask_id() const {return mid_;}
00289
00290 void center(unsigned short& ci, unsigned short& cj) const
00291 { ci = ci_; cj = cj_; }
00292
00293
00294 vgl_box_2d<unsigned> bounding_box() const;
00295
00296
00297 vgl_polygon<double> convex_hull() const;
00298
00299
00300 vcl_vector<vgl_point_2d<unsigned short> > pixels();
00301
00302
00303
00304
00305
00306
00307
00308 virtual vcl_string is_a() const;
00309
00310 virtual bbgm_pair_group_feature* clone() const;
00311
00312
00313 short version() const;
00314
00315
00316 virtual void b_write(vsl_b_ostream &os) const;
00317
00318
00319 virtual void b_read(vsl_b_istream &is);
00320
00321 virtual ~bbgm_pair_group_feature() {}
00322 protected:
00323 static unsigned uid_;
00324 unsigned id_;
00325 brip_rect_mask::mask_id mid_;
00326 unsigned short ci_;
00327 unsigned short cj_;
00328 vcl_set<bbgm_mask_pair_feature, fless > pairs_;
00329 float p_;
00330 };
00331
00332
00333 void vsl_print_summary(vcl_ostream& os,
00334 const bbgm_pair_group_feature& b);
00335
00336
00337
00338 void vsl_b_write(vsl_b_ostream &os, const bbgm_pair_group_feature& b);
00339
00340
00341
00342 void vsl_b_read(vsl_b_istream &is, bbgm_pair_group_feature& b);
00343
00344
00345
00346 bbgm_pair_group_feature pair_group_merge(bbgm_pair_group_feature const& pg0,
00347 bbgm_pair_group_feature const& pg1,
00348 float p_path = 1.0f);
00349
00350 bbgm_pair_group_feature
00351 pair_group_max_union(bbgm_pair_group_feature const& pg0,
00352 bbgm_pair_group_feature const& pg1);
00353
00354
00355 #endif // bbgm_features to bbgm_features_h_