FastTrack 6.1.0
Tracks multiples objects dealing with occlusion and identities.
Loading...
Searching...
No Matches
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 <QHash>
32#include <QList>
33#include <QObject>
34#include <QProgressBar>
35#include <QRandomGenerator>
36#include <QSharedPointer>
37#include <QSqlDatabase>
38#include <QSqlQuery>
39#include <QString>
40#include <QTextStream>
41#include <QThread>
42#include <QTimer>
43#include <QVector>
44#include <algorithm>
45#include <fstream>
46#include <iostream>
47#include <numeric>
48#include <opencv2/calib3d.hpp>
49#include <opencv2/core/types.hpp>
50#include <opencv2/highgui/highgui.hpp>
51#include <opencv2/imgproc/imgproc.hpp>
52#include <opencv2/video/tracking.hpp>
53#include <stdexcept>
54#include <string>
55#include <tuple>
56#include <utility>
57#include "opencv2/features2d/features2d.hpp"
58#include "videoreader.h"
59
60using namespace cv;
61using namespace std;
62
63class Tracking : public QObject {
64 Q_OBJECT
65
66 QSharedPointer<QElapsedTimer> m_timer;
67
69
70 string m_path;
74 QString m_savingPath;
75
76 VideoReader *video;
77 int m_im;
78 QString m_error;
81 Rect m_ROI;
82 QFile m_logFile;
83 vector<cv::String> m_files;
84 vector<int> m_id;
85 vector<int> m_lost;
86 int m_idMax;
87 bool m_isMorph;
88 QHash<QString, QString> m_parameters;
89
90 public:
91 explicit Tracking(QWidget *parent = nullptr) : QObject(parent){};
92 Tracking(const string &path, const string &background, int startImage = 0, int stopImage = -1);
93 Tracking(const string &path, const UMat &background, int startImage = 0, int stopImage = -1);
94 Tracking(const Tracking &) = delete;
95 Tracking &operator=(const Tracking &T) = delete;
96 Tracking &operator=(Tracking &&T) = delete;
97 Tracking(Tracking &&T) = delete;
98 ~Tracking();
99
100 const QString connectionName;
101 Point2d curvatureCenter(const Point3d &tail, const Point3d &head) const;
102 double curvature(Point2d center, const Mat &image) const;
103 double divide(double a, double b) const;
104 bool objectDirection(const UMat &image, vector<double> &information) const;
105 vector<double> objectInformation(const UMat &image) const;
106 vector<Point3d> reassignment(const vector<Point3d> &past, const vector<Point3d> &input, const vector<int> &assignment) const;
107 vector<vector<Point3d>> objectPosition(const UMat &frame, int minSize, int maxSize) const;
108 vector<int> costFunc(const vector<vector<Point3d>> &prevPos, const vector<vector<Point3d>> &pos, double LENGHT, double ANGLE, double LO, double AREA, double PERIMETER) const;
109 void cleaning(const vector<int> &occluded, vector<int> &lostCounter, vector<int> &id, vector<vector<Point3d>> &input, double param_maximalTime) const;
110 vector<Point3d> prevision(vector<Point3d> past, vector<Point3d> present) const;
111 vector<int> findOcclusion(vector<int> assignment) const;
112 static double modul(double angle);
113 static double angleDifference(double alpha, double beta);
114 static UMat backgroundExtraction(VideoReader &video, int n, const int method, const int registrationMethod);
115 static void registration(UMat imageReference, UMat &frame, int method);
116 static void binarisation(UMat &frame, char backgroundColor, int value);
117 static bool exportTrackingResult(const QString &path, const QSqlDatabase &db);
118 static bool importTrackingResult(const QString &path, QSqlDatabase db);
119
121 vector<vector<Point3d>> m_out;
122 vector<vector<Point3d>> m_outPrev;
123
124 public slots:
125 virtual void startProcess();
126 void updatingParameters(const QHash<QString, QString> &);
127 virtual void imageProcessing();
128
129 signals:
134 void progress(int);
135
141
146
150 void finished();
151
155 void forceFinished(QString message);
156
160 void statistic(long long int time);
161};
162
163#endif
This class is intended to execute a tracking analysis on an image sequence. It is initialized with th...
Definition tracking.h:63
virtual void startProcess()
Initializes a tracking analysis and triggers its execution. Constructs from the path to a folder wher...
Definition tracking.cpp:813
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:1021
void statistic(long long int time)
Emitted at the end of the analysis.
int m_startImage
Definition tracking.h:79
~Tracking()
Destructs the tracking object.
Definition tracking.cpp:978
void finished()
Emitted when all images have been processed.
string m_path
Definition tracking.h:70
UMat m_background
Definition tracking.h:72
vector< int > m_lost
Definition tracking.h:85
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:377
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
bool m_isMorph
Definition tracking.h:87
int m_displayTime
Definition tracking.h:73
static UMat backgroundExtraction(VideoReader &video, int n, const int method, const int registrationMethod)
Computes the background of an image sequence by averaging n images.
Definition tracking.cpp:214
int m_im
Definition tracking.h:77
vector< vector< Point3d > > m_outPrev
Definition tracking.h:122
QFile m_logFile
Definition tracking.h:82
void forceFinished(QString message)
Emitted when a crash occurs during the analysis.
vector< cv::String > m_files
Definition tracking.h:83
UMat m_binaryFrame
Definition tracking.h:120
static void registration(UMat imageReference, UMat &frame, int method)
Register two images. To speed-up, the registration is made in a pyramidal way: the images are downsam...
Definition tracking.cpp:287
int m_stopImage
Definition tracking.h:80
vector< Point3d > prevision(vector< Point3d > past, vector< Point3d > present) const
Predicts the next position of an object from the previous position.
Definition tracking.cpp:655
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:515
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
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:590
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:84
static void binarisation(UMat &frame, char backgroundColor, int value)
Binarizes the image by thresholding.
Definition tracking.cpp:358
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
virtual void imageProcessing()
Processes an image from an images sequence and tracks and matchs objects according to the previous im...
Definition tracking.cpp:676
void progress(int)
Emitted when an image is processed.
static bool exportTrackingResult(const QString &path, const QSqlDatabase &db)
Exports the tracking data from the database to a text file.
Definition tracking.cpp:988
bool m_statusBinarisation
Definition tracking.h:68
QString m_savingPath
Definition tracking.h:74
string m_backgroundPath
Definition tracking.h:71
Rect m_ROI
Definition tracking.h:81
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:571
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:78
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:627
QHash< QString, QString > m_parameters
Definition tracking.h:88
void updatingParameters(const QHash< QString, QString > &)
Updates the private members from the external parameters. This function links the tracking logic with...
Definition tracking.cpp:966
void backgroundProgress(int)
Emitted when an image to compute the background is processed.
vector< vector< Point3d > > m_out
Definition tracking.h:121
void finishedProcessFrame()
Emitted when the first image has been processed to trigger the starting of the analysis.
QSharedPointer< QElapsedTimer > m_timer
Definition tracking.h:66
This class is intended to abstract the opening of a video, it can load image sequence and video with ...
Definition videoreader.h:36