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