LiveFit
Track and plot projectiles for in-class demonstration
TrackingStream.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 TRACKINGSTREAM_HPP
21 #define TRACKINGSTREAM_HPP
22 
23 #include "ColorSpace.hpp"
24 #include "DisplayFrameType.hpp"
25 #include "KFBallTracker.hpp"
26 #include "KFPrediction.hpp"
27 
28 #include <QBasicTimer>
29 #include <QImage>
30 
31 #include <opencv2/videoio.hpp>
32 
33 // Default settings pertaining to the TrackingStream. Ensure that
34 // settings in the .ui file default to these!
35 #define SETT_DEF_DISPLAYTYPE 0
36 #define SETT_DEF_XYCOVAR 1.0
37 #define SETT_DEF_FOV 56
38 #define SETT_DEF_PROJW 50
39 #define SETT_DEF_PROJH 50
40 #define SETT_DEF_BALLZ 0
41 
47 class TrackingStream : public QObject
48 {
49  Q_OBJECT
50 
51  QBasicTimer mTimer;
56  QScopedPointer<cv::VideoCapture> mVideoHandle;
57 
68  int mFov;
72  QSize mVideoSize;
73 
77  QSize mProjSize;
82  double mBallZ;
83 
90 
96  std::vector<cv::Point2f> mCorners;
97 
102  std::vector<cv::Point2f> mBallPlane;
103 
107  cv::Mat mCameraMatrix;
108 
112  cv::Mat mRMatrix;
113 
117  cv::Mat mTVector;
118 
122  cv::Mat mFrame;
123 
127  DisplayFrameType mEmitFrameType;
128 
129  bool mStarted = false;
130  bool mProjReady = false;
131  bool mStreamPaused = false;
133  int mFps;
139  bool mDisplayVideo = true;
141  QPolygonF mProjScreen;
142  bool mClipTrack = false;
144 signals:
145  // Signals for emitting frames for display
149  void frameReady(const cv::Mat &, enum ColorSpace=TS_BGR);
150 
151  // Signals which emit information about tracking
153  void ballSpotted(TrackingBall ball);
155  void ballPredicted(KFPrediction pred);
157  void ballProjSpotted(TrackingBall ball);
159  void ballProjPredicted(KFPrediction pred);
161  void ballLost();
162 
164  void started();
165 
166 public slots:
167  // Slots to start or stop this tracking stream
169  void start(int cam = 0);
171  void start(QString fname);
173  void stop();
175  void pauseStream();
176 
177  // Slots for responding to projector settings
179  void changeProjectorCorners(std::vector<cv::Point2f> corners);
180 
182  void changeClipTrack(bool checked);
183 
185  void predictionBall(KFPrediction pred);
186 
187 
189  void setFov(int fov);
194  void changeProjWidth(int width);
199  void changeProjHeight(int height);
204  void changeBallZ(double z);
205 
206  // Slots for responding to tracker UI param settings
211  void changeBlurSize(double blurSize);
216  void changeThreshVal(double thresh);
221  void changeMinRadius(double radius);
226  void changeMaxRadius(double radius);
231  void changeGravConstant(double g);
236  void changeXYCovariance(double sigma);
237 
238  // Slots for receiving frames to (possibly) send along to converter
240  void processThresh(const cv::Mat &, enum ColorSpace=TS_GRAY);
242  void processBlur(const cv::Mat &, enum ColorSpace=TS_BGR);
244  void processContour(const cv::Mat &, enum ColorSpace=TS_GRAY);
246  void processVideoFrame(const cv::Mat& mat, ColorSpace=TS_BGR);
247 
249  void changeDisplayFrameType(QString s);
250 public:
251  TrackingStream(QObject *parent = 0);
252  ~TrackingStream();
253 
255  void startProcess();
256 
286  cv::Point2f imageToProjector(cv::Point2f imP, double z=0);
287 
297  QPointF imageToProjector(QPointF imP, double z=0);
298 
304  cv::Point2f projectorToImage(cv::Point3d prP);
305 
307  void updateProjectorCoordinates(std::vector<cv::Point2f> corners);
308 
310  void setProjSize(const QSize& projSize);
311 
313  void refreshCameraMatrix();
316 
324  bool cameraValid(int idx);
325 private:
327  void timerEvent(QTimerEvent *);
328 };
329 
330 #endif // TRACKINGSTREAM_HPP
void setFov(int fov)
Set the camera FOV.
Definition: TrackingStream.cpp:69
QSize mProjSize
The dimensions (in real world units) of the projector screen.
Definition: TrackingStream.hpp:77
int mFps
The FPS of the video stream (=0 for webcam stream)
Definition: TrackingStream.hpp:133
void changeBlurSize(double blurSize)
Set the strength of blur in preprocessing step.
Definition: TrackingStream.cpp:328
cv::Point2f projectorToImage(cv::Point3d prP)
projectorToImage converts projector (world) coords to image coords
Definition: TrackingStream.cpp:218
void changeMinRadius(double radius)
Set the min viable radius of a ball blob.
Definition: TrackingStream.cpp:338
void processThresh(const cv::Mat &, enum ColorSpace=TS_GRAY)
Process a "thresholded" frame, with white blobs.
Definition: TrackingStream.cpp:375
void timerEvent(QTimerEvent *)
Process a timer event: Read the next frame, process it, emit it.
Definition: TrackingStream.cpp:398
void processContour(const cv::Mat &, enum ColorSpace=TS_GRAY)
Process a "contour" frame, which marks contours.
Definition: TrackingStream.cpp:391
void changeProjWidth(int width)
changeProjWidth sets the width of the projector screen (in inches)
Definition: TrackingStream.cpp:310
void changeDisplayFrameType(QString s)
Set the type of video frame to actually be emitted.
Definition: TrackingStream.cpp:358
void started()
Signal emitted when stream starts processing.
void ballProjSpotted(TrackingBall ball)
A ball has been spotted (proj coordinates)
void processBlur(const cv::Mat &, enum ColorSpace=TS_BGR)
Process a "blurred" frame.
Definition: TrackingStream.cpp:384
bool mProjReady
Whether there is enough projector data for transform.
Definition: TrackingStream.hpp:130
bool cameraValid(int idx)
Check if a camera index is valid.
Definition: TrackingStream.cpp:175
void pauseStream()
Pause the current video stream.
Definition: TrackingStream.cpp:280
void setProjSize(const QSize &projSize)
Set the size of the projector (in real units)
Definition: TrackingStream.cpp:227
KFBallTracker mTracker
The ball tracker; we give it frames, it gives us ball predictions.
Definition: TrackingStream.hpp:138
void changeBallZ(double z)
changeBallZ sets the z-plane that the ball is thrown in (in inches)
Definition: TrackingStream.cpp:322
cv::Mat mFrame
The present frame received from the video stream.
Definition: TrackingStream.hpp:122
void start(int cam=0)
Start a stream with a camera input.
Definition: TrackingStream.cpp:232
void predictionBall(KFPrediction pred)
Alert the stream to a predicted ball.
Definition: TrackingStream.cpp:296
void refreshCameraMatrix()
Update the camera matrices.
Definition: TrackingStream.cpp:75
void changeClipTrack(bool checked)
Set whether or not tracking is clipped to projector.
Definition: TrackingStream.cpp:290
void processVideoFrame(const cv::Mat &mat, ColorSpace=TS_BGR)
Process a "video" frame, which is raw video input.
Definition: TrackingStream.cpp:368
std::vector< cv::Point2f > mBallPlane
The bounding polygon of image coordinates which project to ball coordinates that live in the projecto...
Definition: TrackingStream.hpp:102
cv::Mat mCameraMatrix
The intrinsic camera matrix.
Definition: TrackingStream.hpp:107
void updateProjectorCoordinates(std::vector< cv::Point2f > corners)
Set the in-image coordinates of the projector corners.
Definition: TrackingStream.cpp:91
QBasicTimer mTimer
Timer for processing frames.
Definition: TrackingStream.hpp:51
void ballSpotted(TrackingBall ball)
A ball has been spotted (image coordinates)
A ball which has been tracked by the software.
Definition: TrackingBall.hpp:30
QScopedPointer< cv::VideoCapture > mVideoHandle
The handler for the input video stream.
Definition: TrackingStream.hpp:56
double mBallZ
The distance of the ball plane from the projector plane (in real world units)
Definition: TrackingStream.hpp:82
void changeXYCovariance(double sigma)
Set the covariance in predicted X, Y (a KF parameter)
Definition: TrackingStream.cpp:353
Contains most of the logic for live-tracking objects.
Definition: KFBallTracker.hpp:50
void stop()
Terminate the video stream.
Definition: TrackingStream.cpp:275
void frameReady(const cv::Mat &, enum ColorSpace=TS_BGR)
frameReady emits a cv::Mat of specified color space for processing
void changeMaxRadius(double radius)
Set the max viable radius of a ball blob.
Definition: TrackingStream.cpp:343
An object which owns some sort of input video stream, manages running it (and any tracking...
Definition: TrackingStream.hpp:47
void refreshProjectorMatrices()
Update the projector transformation matrices.
Definition: TrackingStream.cpp:133
bool mStreamPaused
Whether the stream is currently paused.
Definition: TrackingStream.hpp:131
cv::Point2f imageToProjector(cv::Point2f imP, double z=0)
Convert image coordinates to projector coordinates.
Definition: TrackingStream.cpp:180
Object which represents a prediction from the Kalman filter.
Definition: KFPrediction.hpp:34
void changeProjectorCorners(std::vector< cv::Point2f > corners)
Set the location of projector corners in the image.
Definition: TrackingStream.cpp:285
std::vector< cv::Point2f > mCorners
The location of projector coordinates in the image.
Definition: TrackingStream.hpp:96
void changeGravConstant(double g)
Set the accel due to gravity g.
Definition: TrackingStream.cpp:348
bool mClipTrack
Whether we should clip tracking to the proj screen.
Definition: TrackingStream.hpp:142
cv::Mat mTVector
The camera&#39;s extrinsic(?) translation vector.
Definition: TrackingStream.hpp:117
cv::Mat mRMatrix
The camera&#39;s extrinsic(?) rotation matrix.
Definition: TrackingStream.hpp:112
int mFov
The FOV of the camera.
Definition: TrackingStream.hpp:68
void ballProjPredicted(KFPrediction pred)
A ball has been predicted (proj coordinates)
QSize mVideoSize
The dimensions of the video stream.
Definition: TrackingStream.hpp:72
void changeThreshVal(double thresh)
Set the threshold in preprocessing step.
Definition: TrackingStream.cpp:333
void changeProjHeight(int height)
changeProjHeight sets the height of the projector screen (in inches)
Definition: TrackingStream.cpp:316
void ballLost()
We have lost track of the ball entirely.
void startProcess()
Start the tracking process.
bool mDisplayVideo
Whether we should display video or not.
Definition: TrackingStream.hpp:139
void ballPredicted(KFPrediction pred)
A ball has been predicted (image coordinates)
QPolygonF mProjScreen
Region to crop to if mClipTrack is on.
Definition: TrackingStream.hpp:141
DisplayFrameType mEmitFrameType
The type (e.g.
Definition: TrackingStream.hpp:127
bool mStarted
Whether the video stream is started.
Definition: TrackingStream.hpp:129
cv::Mat mProjCornersCamera
The location of projector coordinates in world (real units)
Definition: TrackingStream.hpp:89