VAT  3.0
Video Analysis Tool
PersonalityModule.h
1 #ifndef PERSONALITYMODULE_H
2 #define PERSONALITYMODULE_H
3 
4 #include <map>
5 #include <string>
6 #include <QImage>
7 #include "Datapool.h"
8 #include "ModuleInterface.h"
9 #include "src/hullmodel.h"
10 #include "src/supervisedstatisticalinfo.h"
11 
12 #include <QDir>
13 
14 #define M_1_DIV_2PI 0.159154943091895
15 
16 
18 public:
19  PersonalityModule(Datapool *i_data);
21 
22  //Set module configuration parameters
23  bool setParameters(QDomNode& config);
24 
25  //Initialization after reading parameters
26  bool init();
27 
28  //Function executed at each frame
29  bool run();
30 
31  //update parameters at runtime.
32  bool updateParameters();
33 
34  //Reads file formated
35  int readSupervisedData();
36 
37  void getPersonality(QImage *current, SupervisedStatisticalInfo &sup);
38 
39  //For parameter tuning, separet procedure
40  void getPersonalityPreAngular(QImage *current,
41  std::vector< std::vector<cv::Point> > &ccontours);
42  void getPersonalityFromAngular(std::vector< std::vector<cv::Point> > &contours,
44 
45 
46 
47  static void getFarPoints(cv::Mat im, float dx, float dy, cv::Point2f &r1, cv::Point2f &r2, float mean_x, float mean_y);
48  static bool pointInNeighborhood(cv::Mat im, float pos_x, float pos_y,
49  int &x, int &y);
50  static bool movementFound(cv::Mat f, int wsize, int i0, int j0);
51  static bool movementFound(cv::Mat f, int wsize, int i0, int j0, cv::Rect roi);
52  static double histogramDistance(cv::MatND h1, cv::MatND h2);
53  static double distanceToSegment(int i1, int i2, std::vector<cv::Point> &contour);
54  static bool lineSegmentIntersection(cv::Point3f &line,
55  cv::Point2f &p1, cv::Point2f &p2, cv::Point2f &r);
56  static bool getGeneralLineForm(cv::Point2f p1, cv::Point2f p2,
57  cv::Point3f &rline);
58  static bool getGeneralLineForm(float x, float y, float dx, float dy,
59  cv::Point3f &rline);
60 
61 
62  //Get intersection points r1 and r2 between line 'rline' and contour 'hull' (oriented to
63  //convex hull). If non NULL n1 and n2 pointers, are used to store indexes of contour segments
64  //where intersections were found (to be used later on pixel distance function construction).
65  static int getContourToLineIntersection(std::vector<cv::Point> &hull,
66  cv::Point3f &rline,
67  cv::Point2f &r1, cv::Point2f &r2,
68  int *n1 = NULL, int *n2 = NULL);
69 
70  //Fills a segments vector with moving pixel bounds for each normal on the axis
71  void getNormalIntersections(cv::Mat &f, cv::Rect &roi, std::vector<cv::Point> &hull,
72  cv::Point2f &r1, cv::Point2f &r2, int n1, int n2,
73  float dx, float dy, std::vector< segment2D<float> > &segs);
74 
75  //Sets the moving segment for one normal
76  void setForegroundSegment(cv::Mat &f, cv::Rect &roi, std::vector<cv::Point> &hull,
77  segment2D<float> &seg, float x, float y,
78  float dx, float dy, int &I1, int &I2);
79 
80  //Gets the intersection betweel a line and a contour, in current or next contour
81  //segment.
82  static int getContourToLineIntersectionIndexed(std::vector<cv::Point> &polygon,
83  cv::Point3f &rline,
84  cv::Point2f &r1, cv::Point2f &r2,
85  int &n1, int &n2);
86 
87  //Counts foreground pixels on a window
88  static int movementCount(cv::Mat f, int wsize, int i0, int j0, cv::Rect roi);
89 
90  //Copies a QImage to a cv::Mat, considering the possible difference between
91  //bytes per line between them.
92  static void safeCopyQImageToMat(QImage &q, cv::Mat &m);
93 
94  //Draw contour to mat colouring it from blue to red
95  static void drawContourToMat(std::vector<cv::Point> &v, cv::Mat &m);
96 
97  //Draw contour to mat with a given color
98  static void drawContourToMat(std::vector<cv::Point> &v, cv::Mat &m, cv::Scalar color);
99 
100  //Gaussian blur for a contour
101  static void blurContour(std::vector<cv::Point> &contour,
102  int scale, int step);
103 
104  //Connect contours by nearest extremes
105  void connectContours(std::vector< std::vector<cv::Point> > &contours,
106  std::vector< std::vector<cv::Point> > &ccontours);
107 
108  //Filter short contours
109  void filterContours(std::vector< std::vector<cv::Point> > &contours,
110  std::vector< std::vector<cv::Point> > &fcontours,
111  int min_length);
112 
113  //Draw points
114  void drawPointsToMat(std::vector<cv::Point2f> &pca_points, cv::Mat &m);
115 
116  void drawKMeansPointsToMat(std::vector<cv::Point2f> &pca_points, cv::Mat &m,
117  cv::Mat &klabels, cv::Mat &kcenters, int K);
118 
119 
120  //The process
121  //1. Prepare current image
122  void prepareImage(cv::Mat &current);
123 
124  //2. Get Shape Border
125  void getShapeBorder(cv::Mat &current, cv::Mat &border);
126 
127  //3. Get Centroid. Returns the number of considered points
128  int getCentroid(cv::Mat &image, float x, float y);
129 
130  //4. Get Main Axis
131  void getMainAxis(cv::Mat &image, float x, float y, int n,
132  cv::Point2f &r1, cv::Point2f &r2);
133 
134  //8.1 Get Angular Function
135  void getAngularFunction(std::vector<float> &angular,
136  std::vector< std::vector<cv::Point> > &contours);
137 
138  //8.2 Get Histogram
139  void getHistogram(std::vector<float> &input, cv::MatND &hist);
140 
141  cv::PCA *getPCAfromHistograms(std::vector<cv::MatND> h);
142  cv::PCA *getPCAfromHistograms(std::vector<cv::MatND> h, cv::Mat &pcadata, cv::Mat &pcamean);
143 
144  cv::PCA *getPCA(cv::Mat &pcadata, cv::Mat &pcamean);
145 
146  //Returns kcenters from KMeans por pca eigenvectors
147  cv::Mat getKMeansFromPCA(cv::PCA *pca, cv::Mat &pcadata, cv::Mat &pcamean);
148 
149 #ifdef __OPENCV3__
150  float crossValidation(std::vector<SupervisedStatisticalInfo> &supervised, std::vector< cv::Ptr<cv::ml::ANN_MLP> > &anns, float &rel_error);
151 
152  void getModel(std::vector<SupervisedStatisticalInfo> &sup,
153  cv::Ptr<cv::ml::ANN_MLP> &ann, bool mean, bool reliability);
154 
155  float getPrediction(cv::Ptr<cv::ml::ANN_MLP> ann,
157 
158  std::vector<cv::Ptr<cv::ml::ANN_MLP> > anns;
159 #else
160  float crossValidation(std::vector<SupervisedStatisticalInfo> &supervised, std::vector<CvANN_MLP> &anns, float &rel_error);
161 
162  void getModel(std::vector<SupervisedStatisticalInfo> &sup,
163  CvANN_MLP &ann, bool mean, bool reliability);
164 
165  float getPrediction(CvANN_MLP &ann,
167 
168  std::vector<CvANN_MLP> anns;
169 #endif
170 
171  int line;
172  int cont;
173 
174  int m_min_images_to_process;
175  int processed_images;
176  int m_bins;
177  int w_size;
178  int m_hullOffset;
179  bool m_abs_angular_diff;
180  float m_K;
181 
182  int m_num_ev; //num of eigenvectors to consider in K-means
183  int m_clusters; //kmeans clusters
184  cv::Mat kcenters; //last result, k-means centers
185  bool m_use_mean;
186  bool m_use_reliability;
187 
188  std::vector<cv::MatND> histograms;
189  std::vector<SupervisedStatisticalInfo> supervised;
190  //For tuning:
191  std::vector< std::vector< std::vector<cv::Point> > > image_contours;
192 
193  std::map<int, double> gaussianConstant;
194  std::map<int, double> gaussianFactor;
195 
196  std::vector<std::string> names;
197  std::vector<bool> elegant;
198 
199  std::string m_supervisedFile;
200  int m_min_contour_length;
201  bool first;
202  int m_scale;
203  int m_ostep;
204  bool m_tuning_mode;
205 
206 };
207 #ifdef __OPENCV3__
208 std::ostream& operator<<(std::ostream& out, cv::ml::ANN_MLP &ann);
209 #else
210 std::ostream& operator<<(std::ostream& out, CvANN_MLP &ann);
211 #endif
212 
213 #endif // PERSONALITYMODULE_H
Definition: geometric.h:103
Definition: supervisedstatisticalinfo.h:9
Definition: ModuleInterface.h:43
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: PersonalityModule.h:17