-
Notifications
You must be signed in to change notification settings - Fork 0
/
KFBallTracker.hpp
161 lines (134 loc) · 5.38 KB
/
KFBallTracker.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* LiveFit
* Copyright (C) 2016 The University of Georgia
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef KFBALLTRACKER_HPP
#define KFBALLTRACKER_HPP
#include "BallTrackingFilter.hpp"
#include "ColorSpace.hpp"
#include "KFPrediction.hpp"
#include "KalmanFilterPlus.hpp"
#include "PersonTrackingFilter.hpp"
#include "TrackingBall.hpp"
#include <QObject>
#include <QPolygonF>
#include <QVector>
#include <opencv2/core.hpp>
#include <opencv2/video/tracking.hpp>
#define SETT_DEF_BLOB_BLUR 3.0
#define SETT_DEF_BLOB_THRESH 4.0
#define SETT_DEF_BLOB_MINRAD 2.0
#define SETT_DEF_BLOB_MAXRAD 20.0
#define SETT_DEF_BLOB_GRAV 470.0
/**
* @brief Contains most of the logic for live-tracking objects.
*
* Recieves a frame (as cv::Mat) and uses it (and its own internal state) to
* identify a moving projectile ball and emit them (as TrackingBall and as
* KFPrediction)
*/
class KFBallTracker : public QObject
{
Q_OBJECT
/** Unused */
int mBlobRad;
/** Unused */
int mLatency;
/** Tracking filter for ball objects in the frame */
BallTrackingFilter mBallFilter;
/** Tracking filter for people in the frame (to ignore!); unused */
PersonTrackingFilter mPersonFilter;
/** History of frames possibly used for tracking (eg 3-way-diff) */
QVector<cv::Mat> mFrameHistory;
/** Previous frame diff of history (stored to avoid a duplicate diff) */
cv::Mat mOldDiff;
/** Strength of the Gaussian blur in the blur step of preprocessing */
double mBlurSize;
/** Minimum white value for the thresholding step */
double mThreshVal;
/** Minimum radius of blobs to consider */
double mMinRadius;
/** Maximum radius of blobs to consider */
double mMaxRadius;
/** If mClipTrack, only consider blobs within this polygon */
QPolygonF mClipShape;
/** Whether to only consider blobs in mClipShape */
bool mClipTrack = false;
public:
/** Default constructor */
explicit KFBallTracker(QObject *parent = 0);
/** Process the next frame, possibly finding balls and updating state */
QMap<double, TrackingBall> processNextFrame(cv::Mat &frame, int t);
/** Update the internal time state of the tracker */
void updateTimeState(double t);
/** Kalman distance; see BallFilter.kalmanDistance(); unused. */
double kalmanDistance(cv::Mat measurement);
/** Set mBlurSize */
void setBlurSize(double blurSize) { mBlurSize = blurSize; }
/** Set mThreshVal */
void setThreshVal(double thresh) { mThreshVal = thresh; }
/** Set mMinRadius */
void setMinRadius(double radius) { mMinRadius = radius; }
/** Set mMaxRadius */
void setMaxRadius(double radius) { mMaxRadius = radius; }
/** Set the covariance of x and y values of ball for KF */
void setXYCovariance(double sigma);
/** Set the acceleration due to gravity of the mBallFilter */
void setGravConstant(double g) { mBallFilter.setGravConstant(g); }
signals:
/** Emit a TrackingBall when a ball is spotted in the frame */
void ballSpotted(TrackingBall);
/** Emit a KFPrediction when the KF makes a prediction about the ball
* location */
void ballPredicted(KFPrediction);
/** Emitted when the ball is lost */
void ballLost();
/** Emitted when a person is spotted; unused */
void personSpotted(QRect);
/** Emitted when a thresh image is ready to be converted */
void threshReady(const cv::Mat &, enum ColorSpace);
/** Emitted when a contour image is ready to be converted */
void contourReady(const cv::Mat &, enum ColorSpace);
/** Emitted when a blur image is ready to be converted */
void blurReady(const cv::Mat &, enum ColorSpace);
public slots:
/** Set the clipping shape for tracking */
void setClipShape(QPolygonF shape);
/** Set whether we should clip tracking */
void toggleClip(bool clip);
protected:
/** Find people in a frame; unused. */
QList<cv::Rect> findPeople(cv::Mat &frame);
/** Find balls in a frame, ignoring the region ignores */
QMap<double, TrackingBall> findBalls(cv::Mat &frame, QList<cv::Rect> ignores);
/** Find any movement in a thresholded image, ignoring the region ignores */
QMap<double, TrackingBall> findMovementThresh(cv::Mat threshDiff, QList<cv::Rect> ignores);
/** Called to alert that we did not find a ball in this frame */
void updateTrackFailure();
/** Called to alert that we found ball ball in this frame */
void updateTrackSuccess(TrackingBall ball);
/** Whether the ball is currently lost */
bool ballIsLost();
/** Whether we currently are tracking a found ball */
bool ballFound();
/** Update the internal state of the frame */
void updateFrameState(cv::Mat *frame);
/** Given a TrackingBall ball in threshDiff, score its likelihood to be our
* tracking ball */
double scoreContour(TrackingBall ball, cv::Mat &threshDiff);
};
#endif // KFBALLTRACKER_HPP