FastTrack 6.1.0
Tracks multiples objects dealing with occlusion and identities.
tracking.h
1/*
2This file is part of Fast Track.
3
4 FastTrack is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 FastTrack is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with FastTrack. If not, see <https://www.gnu.org/licenses/>.
16*/
17
18#ifndef TRACKING_H
19#define TRACKING_H
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <time.h>
24#include <QDate>
25#include <QDebug>
26#include <QDir>
27#include <QDirIterator>
28#include <QElapsedTimer>
29#include <QFile>
30#include <QFileInfo>
31#include <QList>
32#include <QMap>
33#include <QObject>
34#include <QSqlDatabase>
35#include <QSqlQuery>
36#include <QString>
37#include <QTextStream>
38#include <QThread>
39#include <QTimer>
40#include <QVector>
41#include <algorithm>
42#include <fstream>
43#include <iostream>
44#include <numeric>
45#include <opencv2/calib3d.hpp>
46#include <opencv2/core/types.hpp>
47#include <opencv2/highgui/highgui.hpp>
48#include <opencv2/imgproc/imgproc.hpp>
49#include <opencv2/video/tracking.hpp>
50#include <stdexcept>
51#include <string>
52#include <tuple>
53#include <utility>
54#include "opencv2/features2d/features2d.hpp"
55#include "videoreader.h"
56
57using namespace cv;
58using namespace std;
59
60class Tracking : public QObject {
61 Q_OBJECT
62
63 QElapsedTimer *timer;
67 string m_path;
71 QString m_savingPath;
73 VideoReader *video;
74 int m_im;
75 QString m_error;
78 Rect m_ROI;
79 QFile m_logFile;
80 vector<cv::String> m_files;
81 vector<int> m_id;
82 vector<int> m_lost;
83 int m_idMax;
84
85 int param_n;
89 double param_len;
90 double param_angle;
91 double param_area;
93 double param_lo;
94 double param_to;
107 QMap<QString, QString> parameters;
109 public:
110 Tracking() = default;
111 Tracking(string path, string background, int startImage = 0, int stopImage = -1);
112 Tracking(string path, UMat background, int startImage = 0, int stopImage = -1);
113 Tracking(const Tracking &) = delete;
114 Tracking &operator=(const Tracking &) = delete;
115 ~Tracking();
116
117 Point2d curvatureCenter(const Point3d &tail, const Point3d &head) const;
118 double curvature(Point2d center, const Mat &image) const;
119 static double modul(double angle);
120 double divide(double a, double b) const;
121 static double angleDifference(double alpha, double beta);
122 bool objectDirection(const UMat &image, vector<double> &information) const;
123 vector<double> objectInformation(const UMat &image) const;
124 vector<Point3d> reassignment(const vector<Point3d> &past, const vector<Point3d> &input, const vector<int> &assignment) const;
125 UMat backgroundExtraction(VideoReader &video, int n, const int method, const int registrationMethod) const;
126 void registration(UMat imageReference, UMat &frame, int method) const;
127 void binarisation(UMat &frame, char backgroundColor, int value) const;
128 vector<vector<Point3d>> objectPosition(const UMat &frame, int minSize, int maxSize) const;
129 vector<int> costFunc(const vector<vector<Point3d>> &prevPos, const vector<vector<Point3d>> &pos, double LENGHT, double ANGLE, double LO, double AREA, double PERIMETER) const;
130 void cleaning(const vector<int> &occluded, vector<int> &lostCounter, vector<int> &id, vector<vector<Point3d>> &input, double param_maximalTime) const;
131 vector<Point3d> prevision(vector<Point3d> past, vector<Point3d> present) const;
132 vector<int> findOcclusion(vector<int> assignment) const;
133 static bool exportTrackingResult(const QString path, QSqlDatabase db);
134 static bool importTrackingResult(const QString path, QSqlDatabase db);
135
138 vector<vector<Point3d>> m_out;
139 vector<vector<Point3d>> m_outPrev;
141 public slots:
142 virtual void startProcess();
143 void updatingParameters(const QMap<QString, QString> &);
144 virtual void imageProcessing();
145
146 signals:
151 void progress(int) const;
152
157 void backgroundProgress(int) const;
158
163
167 void finished() const;
168
172 void forceFinished(QString message) const;
173
177 void statistic(long long int time) const;
178};
179
180#endif
This class is intended to execute a tracking analysis on an image sequence. It is initialized with th...
Definition: tracking.h:60
UMat backgroundExtraction(VideoReader &video, int n, const int method, const int registrationMethod) const
Computes the background of an image sequence by averaging n images.
Definition: tracking.cpp:218
void binarisation(UMat &frame, char backgroundColor, int value) const
Binarizes the image by thresholding.
Definition: tracking.cpp:383
virtual void startProcess()
Initializes a tracking analysis and triggers its execution. Constructs from the path to a folder wher...
Definition: tracking.cpp:828
static double modul(double angle)
Computes the usual mathematical modulo 2*PI of an angle.
Definition: tracking.cpp:109
static bool importTrackingResult(const QString path, QSqlDatabase db)
Imports the tracking data from a text file to the database.
Definition: tracking.cpp:1058
int param_y2
Definition: tracking.h:103
int param_registration
Definition: tracking.h:99
double param_to
Definition: tracking.h:94
int m_startImage
Definition: tracking.h:76
~Tracking()
Destructs the tracking object.
Definition: tracking.cpp:1015
static bool exportTrackingResult(const QString path, QSqlDatabase db)
Exports the tracking data from the database to a text file.
Definition: tracking.cpp:1025
int param_spot
Definition: tracking.h:88
string m_path
Definition: tracking.h:67
void statistic(long long int time) const
Emitted at the end of the analysis.
UMat m_background
Definition: tracking.h:69
vector< int > m_lost
Definition: tracking.h:82
vector< vector< Point3d > > objectPosition(const UMat &frame, int minSize, int maxSize) const
Computes the positions of the objects and extracts the object's features.
Definition: tracking.cpp:402
double curvature(Point2d center, const Mat &image) const
Computes the radius of curvature of the object defined as the inverse of the mean distance between ea...
Definition: tracking.cpp:90
void progress(int) const
Emitted when an image is processed.
int m_displayTime
Definition: tracking.h:70
void forceFinished(QString message) const
Emitted when a crash occurs during the analysis.
double param_area
Definition: tracking.h:91
int m_im
Definition: tracking.h:74
vector< vector< Point3d > > m_outPrev
Definition: tracking.h:139
int param_x2
Definition: tracking.h:102
QFile m_logFile
Definition: tracking.h:79
QElapsedTimer * timer
Definition: tracking.h:63
double param_len
Definition: tracking.h:89
int param_kernelSize
Definition: tracking.h:104
int param_x1
Definition: tracking.h:100
vector< cv::String > m_files
Definition: tracking.h:80
UMat m_binaryFrame
Definition: tracking.h:136
int param_methodBackground
Definition: tracking.h:97
int param_minArea
Definition: tracking.h:87
int param_thresh
Definition: tracking.h:95
int m_stopImage
Definition: tracking.h:77
vector< Point3d > prevision(vector< Point3d > past, vector< Point3d > present) const
Predicts the next position of an object from the previous position.
Definition: tracking.cpp:680
vector< int > costFunc(const vector< vector< Point3d > > &prevPos, const vector< vector< Point3d > > &pos, double LENGHT, double ANGLE, double LO, double AREA, double PERIMETER) const
Computes a cost function and use a global optimization association to associate targets between image...
Definition: tracking.cpp:540
static double angleDifference(double alpha, double beta)
Computes the least difference between two angles, alpha - beta. The difference is oriented in the tri...
Definition: tracking.cpp:134
QMap< QString, QString > parameters
Definition: tracking.h:107
vector< Point3d > reassignment(const vector< Point3d > &past, const vector< Point3d > &input, const vector< int > &assignment) const
Sorts a vector accordingly to a new set of indexes. The sorted vector at index i is the input at inde...
Definition: tracking.cpp:615
Point2d curvatureCenter(const Point3d &tail, const Point3d &head) const
Computes the center of the curvature, defined as the intersection of the minor axis of the head ellip...
Definition: tracking.cpp:47
vector< int > m_id
Definition: tracking.h:81
int param_maxArea
Definition: tracking.h:86
vector< double > objectInformation(const UMat &image) const
Computes the equivalent ellipse of an object by computing the moments of the image....
Definition: tracking.cpp:146
int param_n
Definition: tracking.h:85
int param_kernelType
Definition: tracking.h:105
void finishedProcessFrame() const
Emitted when the first image has been processed to trigger the starting of the analysis.
virtual void imageProcessing()
Processes an image from an images sequence and tracks and matchs objects according to the previous im...
Definition: tracking.cpp:701
void updatingParameters(const QMap< QString, QString > &)
Updates the private members from the external parameters. This function links the tracking logic with...
Definition: tracking.cpp:984
void finished() const
Emitted when all images have been processed.
QString m_savingPath
Definition: tracking.h:71
string m_backgroundPath
Definition: tracking.h:68
Rect m_ROI
Definition: tracking.h:78
UMat m_visuFrame
Definition: tracking.h:137
bool objectDirection(const UMat &image, vector< double > &information) const
Computes the direction of the object from the object parameter (coordinate of the center of mass and ...
Definition: tracking.cpp:175
vector< int > findOcclusion(vector< int > assignment) const
Finds the objects that are occluded during the tracking.
Definition: tracking.cpp:596
bool statusBinarisation
Definition: tracking.h:65
double divide(double a, double b) const
Computes the float division and handle the division by 0 by returning 0.
Definition: tracking.cpp:119
QString m_error
Definition: tracking.h:75
void cleaning(const vector< int > &occluded, vector< int > &lostCounter, vector< int > &id, vector< vector< Point3d > > &input, double param_maximalTime) const
Cleans the data if an object is lost more than a certain time.
Definition: tracking.cpp:652
int param_y1
Definition: tracking.h:101
void registration(UMat imageReference, UMat &frame, int method) const
Register two images. To speed-up, the registration is made in a pyramidal way: the images are downsam...
Definition: tracking.cpp:296
int param_methodRegistrationBackground
Definition: tracking.h:98
double param_perimeter
Definition: tracking.h:92
double param_nBackground
Definition: tracking.h:96
void backgroundProgress(int) const
Emitted when an image to compute the background is processed.
vector< vector< Point3d > > m_out
Definition: tracking.h:138
double param_angle
Definition: tracking.h:90
int param_morphOperation
Definition: tracking.h:106
double param_lo
Definition: tracking.h:93
This class is intended to abstract the opening of a video, it can load image sequence and video with ...
Definition: videoreader.h:34