VAT  3.0
Video Analysis Tool
blob.h
1 #ifndef BLOB_H
2 #define BLOB_H
3 
4 #include <QSharedPointer>
5 #include <iostream>
6 #include <map>
7 #include <deque>
8 #include <list>
9 #include <vector>
10 //#include <values.h> values.h? //never heard of it before
11 #include <cfloat>
12 #include "parallelpiped.h"
13 
14 //Forward declaration
15 class sceneModel;
16 
17 #define BL_INFINITY DBL_MAX
18 
19 class densityData {
20  public:
21  densityData();
22  ~densityData();
23 
24  int TP, TN, FP, FN;
25  int npixels;
26  double hrate, vrate;
27  //Added for tracking
28  int ncols;
29 };
30 
31 class Blob;
32 
33 //Structure to associate to a blob type probability and reliability estimators on parallelepiped shape for an object type
34 class Shape3DData {
35  public:
36  Shape3DData();
37  ~Shape3DData();
38  Shape3DData *copy();
39  Shape3DData& operator=(const Shape3DData& s);
40  void copy(Shape3DData *copy_here);
41  void setNull(Blob *blob);
42  void setNull(ObjectType type, ObjectSubtype subtype, DetectionProblemType dptype, Rectangle<int> *bbox);
43  void copyBlobToShape3DData(Blob *blob);
44  void copyNoLists(Shape3DData *copy_here);
45  void copyShape3DDataOnlyInfo(Shape3DData *copy_here);
46  void copyShape3DDataToBlob(Blob *blob);
47  double horizontalDistanceToPoint(double X2Dfpoint);
48  double verticalDistanceToPoint(double Y2Dfpoint);
49  static std::map<ObjectType, Shape3DData>* copyList(std::map<ObjectType, Shape3DData>* list);
50  friend std::ostream& operator<<(std::ostream&,const Shape3DData&);
51 
52  ObjectType type; // blob type
53  ObjectSubtype subtype; // subtype for postural objects added by marcos
54  DetectionProblemType dp_type; // detection problem type */
55 
56  BoundingBox bbox; // 2D bounding box dimensions possibly modified by detection problem resolution */
57 
58  Parallelpiped _3Dbbox; // 3D bounding box */
59  densityData ddata; //Data associated to fitness of 3D bounding box according to moving pixels.
60  double w, l, h; //Dimensions for the 3d bounding box.
61  Position3D position3D; // 3D position of blob base center for a given model type*/
62  double Rw, Rl, Rh; //Reliability measures for each dimension.
63  double Pw, Pl, Ph; //Model probability for each dimension.
64  double alpha; // Orientation angle (radians) between 3D projection on height 0 of BOTTOM line
65  // of 2D bbox and line formed by points (x[1],y[1]) and (x[3],y[3]) of 3D bbox.*/
66  double P; //Basic Probability that the blob belongs to the class
67  double R; //Global Reliability of the 3d dimensions of the blob
68  double PR; //Probability Reliability Measure
69  double DP; //Dimensional Probability Measure
70  //Max Distance Factor
71  //maxDistanceFactor is a measure of separability of visual evidences (for merging).
72  //maxDistanceFactor = 1.0 if visual evidence are intersecting each other.
73  //maxDistanceFactor --> 0.0 if visual evidences are too far between them.
74  double maxDistanceFactor;
75 
76  std::map<ObjectSubtype, Shape3DData> *subtypesList; //List of subtypes for current type.
77 };
78 
79 class Blob {
80  public:
81  Blob();
82  Blob(Blob *);
83  Blob(int i_xleft, int i_xright, int i_top, int y_bottom);
84  ~Blob();
85  void clean();
86  Blob *copy();
87  Blob *copyWithLists();
88  void setRectangle(int i_xleft, int i_xright, int i_ytop, int i_ybottom);
89  Blob& operator=(const Blob& b);
90  static void copyBlobList(std::vector<Blob>& list, std::vector<Blob>& new_list);
91  static Blob *absorbBlob(Blob *blob1, Blob *blob2);
92  Blob *verticalSplitBlob(int x2d);
93  Blob *horizontalSplitBlob(int y2d);
94  static Blob *mergeBlob(Blob *blob1, Blob *blob2);
95  void mergeBlob(Blob *blob);
96  static Blob *getBiggest2D(std::vector<Blob>& blobs);
97  static ObjectType getTypeFromName(std::string name);
98  static std::string getNameFromType(ObjectType type);
99  static ObjectSubtype getSubtypeFromName(std::string name);
100  static std::string getNameFromSubtype(ObjectSubtype subtype);
101  static DetectionProblemType getDPTypeFromName(std::string name);
102  static std::string getNameFromDPType(DetectionProblemType octype);
103  static std::string getDPNameFromTypeDetailed(DetectionProblemType octype);
104  static double computeOverlapRatio(Blob *b1, Blob *b2);
105  static double computeIntersectRatio(Blob *b1, Blob *b2);
106  static double computeOverlapRatioStrict(Blob *b1, Blob *b2);
107  static bool isBlob1OverlapBlob2(Blob *b1, Blob *b2);
108  bool isPixBelongsToBlob(int x, int y);
109  static void whichBlobsPixelBelongsTo(std::list<Blob *>& list, std::vector<Blob>& blobs, int x, int y);
110  bool isBlobOccluded();
111  bool isBlobTopOccluded();
112  bool isBlobBottomOccluded();
113  int get3DBBox(SceneModel *smodel, double h, double alpha);
114  int compute3DBBoxNormalCase(SceneModel *smodel, double h, double alpha,
115  double V[4][4], int limits[4][4],
116  int eq_relations[4][4], int in_height[4]);
117 
118  bool set3DBBox(SceneModel *smodel, double *X, double *Y, double h);
119  int compute3DBBoxSpecialCases(SceneModel *smodel, double h, double alpha,
120  double V[4][4], int limits[4][4], int nlimits[4],
121  int eq_relations[4][4], int in_height[4]);
122 
123  void setInHeightGivenPosition(int position, int *in_height);
124  int getPositionRelativeToCamera(SceneModel *smodel);
125  int getPositionRelativeToCameraOld(SceneModel *smodel);
126  void setReal3DPosition();
127  void setEstimated3DPosition(SceneModel* smodel);
128  void setPositionAtCenterBottom(SceneModel* smodel);
129  static bool isBlob1InsideBlob2(Blob *b1, Blob *b2);
130  static bool same2DBlob(Blob *b1, Blob *b2);
131  static void set2Dand3Dbboxes(SceneModel *smodel, double *x, double *y,
132  Rectangle<int> *bb2D, Parallelpiped *bb3D, double h);
133  static void orderByProximityToPoint(std::vector<Blob>& blobs, double x, double y);
134  double horizontalDistanceToPoint(double X2Dfpoint);
135  double verticalDistanceToPoint(double Y2Dfpoint);
136  static void orderBlobsAscending(double *values, std::vector<Blob>& blobs, int top, int bottom);
137  static int getMiddle(double *values, std::vector<Blob>& blobs, int top, int bottom);
138  Shape3DData *getBestS3D(bool &lastUnknown, ObjectType best_type, double classifThreshold);
139  static void copyBlobData(Blob *blob1, Blob *blob2);
140 
141  friend std::ostream& operator<<(std::ostream&,const Blob&);
142 
143  //Variables
144  int id; // identifier
145  int component_label; //label assigned at connected components stage.
146  ObjectType type; // blob type
147  ObjectSubtype subtype; // Subtype for postural object types
148  DetectionProblemType dp_type; // detection problem type
149  BoundingBox bbox; // bounding box
150  Parallelpiped _3Dbbox; // bounding box
151  densityData ddata; // Data associated to fitness of 3D bounding box according to moving pixels.
152  char *moving_pixels; // List that could contain the moving pixels of a blob.
153  // It is an array represented as a list, with moving pixels = 1, and background pixels = 0.
154  // Used in tracking for difered pixel analysis.
155  double w, l, h; // 3D width
156  double alpha; // Orientation angle (radians) between 3D X Axis and line formed by
157  // points (x[1],y[1]) and (x[3],y[3]) of 3D bbox.
158  int position;
159  /* Positions: (Position 4 is Middle-Center and correspond when blob is absolutely between focal-point and ground)
160  Left-Front(0) Middle-Front(1) Right-Front(2)
161  \ | /
162  \ | /
163  \ | /
164  Left-Center(3)<-----Focal Point----->Right-Center(5)
165  / | \
166  / | \
167  / | \
168  Left-Back(6) Middle-Back(7) Right-Back(8)
169  */
170 
171  bool occlusion; // true if contained solution in blob corresponds to a solution of the occlusion list
172  // false if contained solution in blob corresponds to a solution of the normal list
173  double beta; // Angle (radians) between 3D projection on height 0 of
174  // CENTER 2D Y of blob line and 3D X axis.
175  int beta_direction; // +1: if positive growth of angle in image plane implies positive growth of angle
176  // in world xy plane.
177  // -1: if positive growth of angle in image plane implies negative growth of angle
178  // in world xy plane.
179 
180  double gP; // Global probability of belonging to the class.
181  double gR; // Mean reliability of stimated 3D model.
182  double gPR; //Probability Reliability Measure
183  double gDP; //Dimensional Probability Measure
184 
185  // Dimensions Order: [width3d, height3d, length3d].
186  double R[3]; // Reliability measures for each dimension.
187  double P[3]; // Model probability for each dimension.
188 
189  Position3D position3D; // 3D position of blob
190 
191  // list of best 3d associations for each model.
192  std::map<ObjectType, Shape3DData> *normal_data;
193  // list of best 3d associations for each model, taking into account the posibility of occlusion.
194  std::map<ObjectType, Shape3DData> *occ_data;
195  // pointer to best 3d model association
196  Shape3DData *best_data;
197 
198  bool reduced_size; // true IF BLOB TO LITTLE TO FIT WITH ANY OF THE EXISTING PRE-DEFINED MODELS, ELSE false
199  bool excesive_size; // true IF BLOB TO LITTLE TO FIT WITH ANY OF THE EXISTING PRE-DEFINED MODELS, ELSE false
200 
201  int frameNumber; // Frame Number where blob was processed.
202 
203  // Time difference in milliseconds between previous frame and frame where blob was processed.
204  int currentTimeDiffMilliSeconds;
205  //Max Distance Factor
206  //maxDistanceFactor is a measure of separability of visual evidences (for merging).
207  //maxDistanceFactor = 1.0 if visual evidence are intersecting each other.
208  //maxDistanceFactor --> 0.0 if visual evidences are too far between them.
209 
210  double maxDistanceFactor;
211 };
212 
213 typedef QSharedPointer<Blob> SpBlob;
214 
215 // macros for getting blob attributes.
216 #define BLOB_IDENT(_bl) ((_bl)->id)
217 
218 #define BLOB_FRAME_NUMBER(_bl) ((_bl)->frameNumber)
219 #define BLOB_TIME_DIFF_MSEC(_bl) ((_bl)->currentTimeDiffMilliSeconds)
220 
221 #define BLOB_IS_REDUCED_SIZE(_bl) ((_bl)->reduced_size)
222 #define BLOB_IS_EXCESIVE_SIZE(_bl) ((_bl)->excesive_size)
223 
224 #define BLOB_BBOX(_bl) (&((_bl)->bbox))
225 #define BLOB_3DBBOX(_bl) (&((_bl)->_3Dbbox))
226 #define BLOB_DDATA(_bl) (&((_bl)->ddata))
227 #define BLOB_MOVING_PIXELS(_bl) ((_bl)->moving_pixels)
228 
229 #define DDATA_TP(_dd) (_dd)->TP
230 #define DDATA_TN(_dd) (_dd)->TN
231 #define DDATA_FP(_dd) (_dd)->FP
232 #define DDATA_FN(_dd) (_dd)->FN
233 #define DDATA_NPIX(_dd) (_dd)->npixels
234 #define DDATA_HRATE(_dd) (_dd)->hrate
235 #define DDATA_VRATE(_dd) (_dd)->vrate
236 #define DDATA_COLS_NUM(_dd) (_dd)->ncols
237 
238 #define BLOB_3DBBOX_H(_bl) (PARALL_H(BLOB_3DBBOX(_bl)))
239 #define BLOB_3DBBOX_X_i(_bl, _i) (PARALL_X_i(BLOB_3DBBOX(_bl), _i))
240 #define BLOB_3DBBOX_Y_i(_bl, _i) (PARALL_Y_i(BLOB_3DBBOX(_bl), _i))
241 #define BLOB_3DBBOX_POINT_i(_bl, _i) (PARALL_POINT_i(BLOB_3DBBOX(_bl), _i))
242 
243 #define BLOB_3DBBOX_X2D_BASE_i(_bl, _i) (PARALL_X2D_BASE_i(BLOB_3DBBOX(_bl), _i))
244 #define BLOB_3DBBOX_Y2D_BASE_i(_bl, _i) (PARALL_Y2D_BASE_i(BLOB_3DBBOX(_bl), _i))
245 #define BLOB_3DBBOX_2D_BASE_POINT_i(_bl, _i) (PARALL_2D_BASE_POINT_i(BLOB_3DBBOX(_bl), _i))
246 
247 #define BLOB_3DBBOX_X2D_H_i(_bl, _i) (PARALL_X2D_H_i(BLOB_3DBBOX(_bl), _i))
248 #define BLOB_3DBBOX_Y2D_H_i(_bl, _i) (PARALL_Y2D_H_i(BLOB_3DBBOX(_bl), _i))
249 #define BLOB_3DBBOX_2D_H_POINT_i(_bl, _i) (PARALL_2D_H_POINT_i(BLOB_3DBBOX(_bl), _i))
250 
251 #define BLOB_POSITION(_bl) ((_bl)->position)
252 
253 #define BLOB_XLEFT(_bl) (RECT_XLEFT(BLOB_BBOX(_bl)))
254 #define BLOB_X(_bl) (BLOB_XLEFT(_bl))
255 #define BLOB_XRIGHT(_bl) (RECT_XRIGHT(BLOB_BBOX(_bl)))
256 #define BLOB_YTOP(_bl) (RECT_YTOP(BLOB_BBOX(_bl)))
257 #define BLOB_Y(_bl) (BLOB_YTOP(_bl))
258 #define BLOB_YBOTTOM(_bl) (RECT_YBOTTOM(BLOB_BBOX(_bl)))
259 
260 #define BLOB_WIDTH(_bl) (RECT_WIDTH(BLOB_BBOX(_bl)))
261 #define BLOB_HEIGHT(_bl) (RECT_HEIGHT(BLOB_BBOX(_bl)))
262 
263 #define BLOB_XCENTER(_bl) (BLOB_XLEFT(_bl) + BLOB_WIDTH(_bl)/2)
264 #define BLOB_YCENTER(_bl) (BLOB_YTOP(_bl) + BLOB_HEIGHT(_bl)/2)
265 
266 #define BLOB_TYPE(_bl) ((_bl)->type)
267 #define BLOB_SUBTYPE(_bl) ((_bl)->subtype)
268 #define BLOB_DP_TYPE(_bl) ((_bl)->dp_type)
269 
270 #define BLOB_3D_WIDTH(_bl) ((_bl)->w)
271 #define BLOB_3D_HEIGHT(_bl) ((_bl)->h)
272 #define BLOB_3D_LENGTH(_bl) ((_bl)->l)
273 
274 #define BLOB_PW(_bl) ((_bl)->P[0])
275 #define BLOB_PH(_bl) ((_bl)->P[1])
276 #define BLOB_PL(_bl) ((_bl)->P[2])
277 #define BLOB_RW(_bl) ((_bl)->R[0])
278 #define BLOB_RH(_bl) ((_bl)->R[1])
279 #define BLOB_RL(_bl) ((_bl)->R[2])
280 
281 #define BLOB_ALPHA(_bl) ((_bl)->alpha)
282 #define BLOB_BETA(_bl) ((_bl)->beta)
283 #define BLOB_BETA_DIRECTION(_bl) ((_bl)->beta_direction)
284 
285 #define BLOB_P(_bl) ((_bl)->gP)
286 #define BLOB_R(_bl) ((_bl)->gR)
287 #define BLOB_PR(_bl) ((_bl)->gPR)
288 #define BLOB_DP(_bl) ((_bl)->gDP)
289 
290 #define BLOB_OCCLUSION(_bl) ((_bl)->occlusion)
291 
292 #define BLOB_NORMAL_3DDATA(_bl) ((_bl)->normal_data)
293 #define BLOB_OCC_3DDATA(_bl) ((_bl)->occ_data)
294 #define BLOB_BEST_3DDATA(_bl) ((_bl)->best_data)
295 
296 #define BLOB_3D_X(_bl) ((_bl)->position3D.x)
297 #define BLOB_3D_Y(_bl) ((_bl)->position3D.y)
298 #define BLOB_3D_Z(_bl) ((_bl)->position3D.z)
299 #define BLOB_3D_POSITION(_bl) (&(_bl)->position3D)
300 
302 #define S3D_TYPE(_s3d) ((_s3d)->type)
303 #define S3D_SUBTYPE(_s3d) ((_s3d)->subtype)
304 #define S3D_DP_TYPE(_s3d) ((_s3d)->dp_type)
305 
306 #define S3D_BBOX(_s3d) (&((_s3d)->bbox))
307 #define S3D_3DBBOX(_s3d) (&((_s3d)->_3Dbbox))
308 #define S3D_DDATA(_s3d) (&((_s3d)->ddata))
309 
310 #define S3D_XLEFT(_s3d) (RECT_XLEFT(S3D_BBOX(_s3d)))
311 #define S3D_X(_s3d) (S3D_XLEFT(_s3d))
312 #define S3D_XRIGHT(_s3d) (RECT_XRIGHT(S3D_BBOX(_s3d)))
313 #define S3D_YTOP(_s3d) (RECT_YTOP(S3D_BBOX(_s3d)))
314 #define S3D_Y(_s3d) (S3D_YTOP(_s3d))
315 #define S3D_YBOTTOM(_s3d) (RECT_YBOTTOM(S3D_BBOX(_s3d)))
316 #define S3D_WIDTH(_s3d) (RECT_WIDTH(S3D_BBOX(_s3d)))
317 #define S3D_HEIGHT(_s3d) (RECT_HEIGHT(S3D_BBOX(_s3d)))
318 #define S3D_XCENTER(_s3d) (S3D_XLEFT(_s3d) + S3D_WIDTH(_s3d)/2)
319 #define S3D_YCENTER(_s3d) (S3D_YTOP(_s3d) + S3D_HEIGHT(_s3d)/2)
320 
321 #define S3D_3DBBOX_H(_s3d) (PARALL_H(S3D_3DBBOX(_s3d)))
322 #define S3D_3DBBOX_X_i(_s3d, _i) (PARALL_X_i(S3D_3DBBOX(_s3d), _i))
323 #define S3D_3DBBOX_Y_i(_s3d, _i) (PARALL_Y_i(S3D_3DBBOX(_s3d), _i))
324 #define S3D_3DBBOX_POINT_i(_s3d, _i) (PARALL_POINT_i(S3D_3DBBOX(_s3d), _i))
325 
326 #define S3D_3DBBOX_X2D_BASE_i(_s3d, _i) (PARALL_X2D_BASE_i(S3D_3DBBOX(_s3d), _i))
327 #define S3D_3DBBOX_Y2D_BASE_i(_s3d, _i) (PARALL_Y2D_BASE_i(S3D_3DBBOX(_s3d), _i))
328 #define S3D_3DBBOX_2D_BASE_POINT_i(_s3d, _i) (PARALL_2D_BASE_POINT_i(S3D_3DBBOX(_s3d), _i))
329 
330 #define S3D_3DBBOX_X2D_H_i(_s3d, _i) (PARALL_X2D_H_i(S3D_3DBBOX(_s3d), _i))
331 #define S3D_3DBBOX_Y2D_H_i(_s3d, _i) (PARALL_Y2D_H_i(S3D_3DBBOX(_s3d), _i))
332 #define S3D_3DBBOX_2D_H_POINT_i(_s3d, _i) (PARALL_2D_H_POINT_i(S3D_3DBBOX(_s3d), _i))
333 
334 #define S3D_3D_X(_s3d) ((_s3d)->position3D.x)
335 #define S3D_3D_Y(_s3d) ((_s3d)->position3D.y)
336 #define S3D_3D_Z(_s3d) ((_s3d)->position3D.z)
337 #define S3D_3D_POSITION(_s3d) (&(_s3d)->position3D)
338 
339 #define S3D_W(_s3d) ((_s3d)->w)
340 #define S3D_H(_s3d) ((_s3d)->h)
341 #define S3D_L(_s3d) ((_s3d)->l)
342 
343 #define S3D_PW(_s3d) ((_s3d)->Pw)
344 #define S3D_PH(_s3d) ((_s3d)->Ph)
345 #define S3D_PL(_s3d) ((_s3d)->Pl)
346 
347 #define S3D_RW(_s3d) ((_s3d)->Rw)
348 #define S3D_RH(_s3d) ((_s3d)->Rh)
349 #define S3D_RL(_s3d) ((_s3d)->Rl)
350 
351 #define S3D_R(_s3d) ((_s3d)->R)
352 #define S3D_P(_s3d) ((_s3d)->P)
353 #define S3D_PR(_s3d) ((_s3d)->PR)
354 #define S3D_DP(_s3d) ((_s3d)->DP)
355 
356 #define S3D_ALPHA(_s3d) ((_s3d)->alpha)
357 
358 #define S3D_SUBTYPES_LIST(_s3d) ((_s3d)->subtypesList)
359 
360 typedef Shape3DData *s3ddata_t;
361 typedef densityData *ddata_t;
362 
363 
364 class blobBuffer {
365  private:
366  unsigned int maxLength;
367 
368  public:
369  std::deque<SpBlob> trackedBlobs;
370  blobBuffer(int);
371  ~blobBuffer();
372  std::deque<SpBlob>::iterator begin();
373  std::deque<SpBlob>::iterator end();
374  std::deque<SpBlob>::reverse_iterator rbegin();
375  std::deque<SpBlob>::reverse_iterator rend();
376  SpBlob operator[](int);
377  unsigned int size();
378  SpBlob back();
379  void clear();
380  void insert(Blob *);
381  void resetMaxLength(int);
382  void copyBlobs(blobBuffer *);
383  friend std::ostream& operator<<(std::ostream&,const blobBuffer&);
384 };
385 
386 
387 #endif // BLOB_H
Definition: blob.h:364
Definition: parallelpiped.h:8
Definition: blob.h:34
Definition: calibration.h:51
Definition: blob.h:19
Definition: blob.h:79