VAT  3.0
Video Analysis Tool
ReliabilityPatchTrackingModel.h
1 #ifndef RELIABILITYPATCHTRACKINGMODEL_H
2 #define RELIABILITYPATCHTRACKINGMODEL_H
3 
4 #include <QDomNode>
5 #include <QImage>
6 #include <vector>
7 #include <QSharedPointer>
8 
9 #include <opencv2/core.hpp>
10 #include <opencv2/features2d.hpp>
11 #include <opencv2/xfeatures2d.hpp>
12 
13 #include "reliabilitysinglemodelinterface.h"
14 #include "src/blob.h"
15 
16 class Datapool;
17 
18 typedef int (*getSegmentation)(Datapool*, BoundingBox&, cv::Mat&, int);
19 
21 public:
22  enum Bound {
23  B_LEFT,
24  B_RIGHT,
25  B_TOP,
26  B_BOTTOM,
27  };
28 
30  public:
31  double Rs; //Illumination reliability (TO IMPLEMENT)
32  double Rc; //Contrast reliability
33  double R; //Global patch reliability
34  enum Bound origin;
35  int x, y;
36  int dx, dy; //displacement w.r.t. previous frame
37  uchar Y, U, V;
38  cv::Mat UVhisto;
39  cv::Mat Yhisto;
40  int num_foreground;
41  double Rcoloured;
42  double hm, vm; //horizontal and vertical moments
43  int num_seen;
44  cv::KeyPoint kp;
45  double score; //FAST score;
46  cv::Mat descriptor; //FREAK descriptor
47  double dscore; //descriptor score;
48  uchar T; //Threshold used for segmentation
49  friend std::ostream& operator<<(std::ostream&, ReliabilityPatch&);
50  };
51 
53  public:
55  ReliabilityPatchBuffer(int size);
57  std::deque<ReliabilityPatch>::iterator begin();
58  std::deque<ReliabilityPatch>::iterator end();
59  std::deque<ReliabilityPatch>::reverse_iterator rbegin();
60  std::deque<ReliabilityPatch>::reverse_iterator rend();
61  ReliabilityPatch &operator[](int);
62  unsigned int size();
63  ReliabilityPatch &back();
64  ReliabilityPatch &front();
65  void clear();
66  void insert(ReliabilityPatch *);
67  void insert(ReliabilityPatch &pack);
68  void resetMaxLength(int);
69  void copyModels(ReliabilityPatchBuffer *);
70 
71  friend std::ostream& operator<<(std::ostream&,const ReliabilityPatchBuffer&);
72 
73  std::deque<ReliabilityPatch> instances;
74  unsigned int maxLength;
75  };
76 
78  public:
80  CentroidBuffer(int size);
81  ~CentroidBuffer();
82  std::deque<point2D<double> >::iterator begin();
83  std::deque<point2D<double> >::iterator end();
84  std::deque<point2D<double> >::reverse_iterator rbegin();
85  std::deque<point2D<double> >::reverse_iterator rend();
86  point2D<double> &operator[](int);
87  unsigned int size();
88  point2D<double> &back();
89  point2D<double> &front();
90  void clear();
91  void insert(point2D<double> *);
92  void insert(point2D<double> &pack);
93  void resetMaxLength(int);
94  void copyModels(CentroidBuffer *);
95 
96  friend std::ostream& operator<<(std::ostream&,const point2D<double> &);
97 
98  std::deque<point2D<double> > points;
99  unsigned int maxLength;
100  };
101 
102 
104  public:
105  unsigned int id;
106  ReliabilityPatchBuffer buffer;
107  ReliabilityPatch bestInRc;
108  double bestRc;
109  double Rpos; //Position reliability (coherence with previous positions)
110  double Rpos_p; //Previous position reliability
111  double Rg; //Global match reliability
112  double Rc; //Buffer contrast reliability
113  double R; //Integrated Patch Tracking Reliability
114  bool structural;
115  bool reliable;
116  bool highly;
117  int dx_diff; //Differences with bbox center
118  int dy_diff;
119  bool toErase;
120 
121  void setGlobalReliability();
122  double getGlobalDisplacement(int &dx, int &dy);
123  friend std::ostream& operator<<(std::ostream&, ReliabilityPatchTracking&);
124  };
125 
126  ReliabilityPatchTrackingModel(int buffer_size);
127 
129 
130  //IMPLEMENT!! Initializes the model attributes for the interface.
131  //The function is called by the interface constructor
132  virtual void initInstanceAttributes();
133 
134  //IMPLEMENT!! Initializes the dynamics model attributes for the interface.
135  //The function is called by the interface constructor
136  virtual void initDynamicsAttributes();
137 
138  //IMPLEMENT!! Copy function for specific data from the instance (general_copy calls it after copying base info)
139  virtual void copy(SpReliabilitySingleModelInterface);
140 
141  //IMPLEMENT!! Copy general structures and parameters of specific model, without considering instance specific data
142  virtual void copy_structure(SpReliabilitySingleModelInterface);
143 
144  //IMPLEMENT!! sets parameters from xml file
145  virtual void setParameters(QDomNode &i_parameters);
146 
147  //IMPLEMENT!! sets activation criteria for each model: reliability on input (distance, bad data),
148  // needs (occlusion, priority),
149  //sets priority of models (hierarchy).
150  //Checked before update phase
151  virtual void activate(BoundingBox &);
152 
153  //IMPLEMENT!! initializes every activated model, according to their own input (region-blob-segments schema)
154  //Executed only once at the begginning of single model creation (after 'copy_structure' and 'copy' methods
155  //are used to copy the information).
156  virtual void init();
157 
158  //IMPLEMENT!! updates activated model instance attributes for current frame, and returns the set of detected/enriched objects.
159  // It notifies if roi (bounding box, or segmentation) changes compared to the input, for reprocessing.
160  virtual bool updateInstance(BoundingBox &, std::set<SpRMMHypothesis, hypothesesOrderedByBestProbabilityOperator> &);
161 
162  //IMPLEMENT!! updates activated model instance attributes for current frame and object, and returns the detected/enriched object.
163  // It notifies if roi (bounding box, or segmentation) changes compared to the input, for reprocessing.
164  // This function does NOT generate mobiles or hypotheses. It processes attributes for the current object.
165  // This function could be used by updateInstance to generate mobile information.
166  virtual void updateSingleInstance(BoundingBox &);
167 
168 
169  //IMPLEMENT!! updates model dynamics, based on instances information
170  virtual void updateModelDynamics();
171 
172  //IMPLEMENT!! sets global probability for the model (how good is the model)
173  virtual void setProbabilityAndReliability();
174 
175  //IMPLEMENT!! sets blob enclosing the model in forward process (update)
176  virtual void setForwardInterface();
177 
178  //OPTIONAL
179  virtual bool draw(QPainter &painter);
180 
181  virtual bool draw(QPainter &painter, int r, int g, int b);
182 
183  virtual BoundingBox getEstimator(double &R);
184 
185  bool projectBoundingBox(BoundingBox &bbox);
186 
187 
188  //Get function pointer to segmentation function
189  getSegmentation getResegmentPointer(QString segAlternative, int &threshold);
190 
191  //Prints current patches on screen.
192  void printPatches();
193 
194  //Get strongest (FAST) and nearest-to-center point
195  bool getStrongPosition(int ix, int iy, int &ox, int &oy, cv::KeyPoint &op);
196  //... and simplified version for refill
197  bool refillStrongPosition(int ix, int iy, cv::KeyPoint &op);
198 
199 
200  //Initialize attribute with instance value
201  void initAttribute(ReliabilityDynamicsAttribute &d, AttributeDupletValue &i);
202 
203  //Standard attribute dynamics update, with no attribute value constraints
204  void updateAttribute(const QString &name, ReliabilityDynamicsAttribute &d);
205 
206  double getAttributeEstimate(ReliabilityDynamicsAttribute &d, double time_diff);
207 
208 
209  void updateAttributeDynamics(double data, double RData, double acuity,
210  double estimate, double currentCooling, AttributeTripletValue &att);
211 
212  void updateAttributeDynamicsNoData(double estimate, double acuity, double currentCooling,
213  AttributeTripletValue &att);
214 
215  double projectAttributeValue(ReliabilityDynamicsAttribute &, double &R);
216 
217  //Builds initial patches
218  void initLeftPatches(BoundingBox &boundingBox);
219  void initRightPatches(BoundingBox &boundingBox);
220  void initBottomPatches(BoundingBox &boundingBox);
221  void initTopPatches(BoundingBox &boundingBox);
222 
223  //Refill patches
224  void refillLeftPatches(BoundingBox &b);
225  void refillRightPatches(BoundingBox &b);
226  void refillBottomPatches(BoundingBox &b);
227  void refillTopPatches(BoundingBox &b);
228 
229  void insertPatch(BoundingBox &bbox, enum Bound bound, int row, int col);
230  void refillPatch(BoundingBox &bbox, enum Bound bound, int row, int col);
231  void setCentroid(BoundingBox &roi);
232  void setInitialCentroid(BoundingBox &roi);
233 
234  void preparePatches();
235  bool distantToExistingPatches(int x, int y, float response, bool &worst, int &wx, int &wy);
236  bool distantToExistingPatches(int x, int y, double distance);
237  bool distantToExistingPatches(int x, int y);
238  void getFREAKDescriptors();
239  void refillFREAKDescriptors(Bound bound);
240  void setTrackedPatches(BoundingBox &roi);
241  void computeFreak(std::vector<cv::KeyPoint>& points, cv::Mat &descriptors);
242  void setHistograms(ReliabilityPatch &p);
243  double illuminationReliability(uchar Y);
244  double BhattacharyyaDistance(cv::Mat &h1, cv::Mat &h2);
245 
246  void completePatch(ReliabilityPatch &p); //Completes patch data.
247  //Returns global reliability score (distance to all criteria).
248  double globalPatchDistance(ReliabilityPatch &p1, ReliabilityPatch &p2);
249  bool adjustBoundingBoxDimensions(BoundingBox &b);
250  double displacementCoherenceReliability(double distance);
251  void saveWeakReliabilityPatches(BoundingBox &b);
252  void deleteWeakPatches();
253  void addNewPatches(BoundingBox &roi);
254 
255 
256  //Sets the most reliable patches (analyzing short history) in terms of
257  // - Coherence with the bounding box movement
258  // - Contrast reliability
259  // - Low distance among feature
260  void setHighlyReliablePatches();
261  void setWeakPatches();
262  void removeEquallyConverged();
263  void setInstancesDisplacement();
264  void moveProjectedBoxToLowestDisplacementAndHighestCoveragePosition(BoundingBox &p, BoundingBox &roi);
265 
266  double contrastGaussianMean(int x, int y, QImage &cn);
267  double contrastGaussianMeanFG(int x, int y, QImage *cn, QImage *fg);
268 
269  void printTrackedPatches();
270  void printTrackedPatch(unsigned int id);
271 
272  std::map<int, std::map<int, ReliabilityPatch> > patches;
273  std::deque<ReliabilityPatch *> new_patches;
274  std::vector<cv::KeyPoint> new_keypoints;
275 
276  std::map<uchar, cv::Mat> new_segmentations;
277 
278  std::deque<ReliabilityPatchTracking> tracked_patches;
279  std::vector<ReliabilityPatchTracking *> reliable_patches;
280 
281  int m_patchSize;
282  int m_generationStep;
283  int m_topPatchesSize;
284  int m_maxDimensionChange;
285  int m_histoBins;
286  int m_ill_alpha;
287  int m_ill_beta;
288  int m_ill_gamma;
289  double m_minimalPatchReliability;
290  int m_refillDistance;
291  QString m_auxSegmentation;
292  bool showText;
293  bool por_aca;
294  int patches_initial_count;
295  double refPatchesRate; // patches/(w*h) measured to keep the proportion of reference patches. If the value is lower than
296  // this rate, patch refill is activated
297 
298  unsigned int tracked_patch_id;
299  std::vector<int> gdx;
300  std::vector<int> gdy;
301  CentroidBuffer centroids;
302 
303  BoundingBox current_bbox;
304  cv::Mat current_map;
305  cv::Mat current_fg;
306  cv::Mat current_fg_eroded;
307  cv::Mat eelement;
308 
309  static const double R_MIN;
310  static const bool USE_GAUSSIAN;
311  static const int GAUSSIAN_SIZE;
312  static const bool USE_FG;
313  static const bool USE_SQUARE_DISTANCE;
314  static const bool USE_MAX;
315  static const bool USE_MEAN;
316  static const bool USE_MAX_WEIGHTED;
317  static const bool USE_MEAN_WEIGHTED;
318 
319  static const int GF7[];
320  static const int GF5[];
321  static const int GF3[];
322 
323  static getSegmentation m_seg;
324  static bool availableSeg;
325  static int m_referenceThreshold;
326 
327  double m_minimalAttributeVelocityReliability;
328  double m_minimalAttributeAccelerationReliability;
329  double m_minimalWidth;
330  double m_minimalHeight;
331  int m_pixelAcuity;
332  bool m_useReliability;
333  bool m_useFeatureMap;
334 
335  int *GF;
336  double GFactor;
337 
338  bool m_showId;
339  bool m_blackId;
340  bool m_build2DBoundReliabilityImage;
341  bool m_activateProjectedBlobCorrection;
342  double localMaxBound;
343 
344  cv::Ptr<cv::xfeatures2d::FREAK> freak;
345 
346  //Pointers to available segmentations
347 
348 };
349 
350 typedef QSharedPointer<ReliabilityPatchTrackingModel> SpReliabilityPatchTrackingModel;
351 
352 
353 #endif // RELIABILITYPATCHTRACKINGMODEL_H
Definition: geometric.h:20
Definition: reliabilityattribute.h:101
Definition: ReliabilityPatchTrackingModel.h:103
Definition: reliabilitysinglemodelinterface.h:96
Definition: ReliabilityPatchTrackingModel.h:20
Definition: ReliabilityPatchTrackingModel.h:77
Definition: reliabilityattribute.h:10
An object of this class is instantiated at main code, and this object is used by every class to push ...
Definition: Datapool.h:39
Definition: reliabilityattribute.h:76
Definition: gtstructures.h:32
Definition: ReliabilityPatchTrackingModel.h:29
Definition: ReliabilityPatchTrackingModel.h:52
Definition: BackgroundRecLigth.h:20