SampleTrack.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "Camera.h"
3 #include "TrackerPsa.h"
4 #include "TrackerStat.h"
5 #include "TrackerFeatures.h"
6 #include "Shared.h"
7 using namespace alvar;
8 using namespace std;
9 
10 int tracker = 0;
11 bool reset = true;
12 CvFont font;
13 std::stringstream calibrationFilename;
14 
15 void track_none(IplImage *image, IplImage *img_gray) {
16 }
17 
18 void track_psa(IplImage *image, IplImage *img_gray) {
19  static TrackerPsa tracker_psa;
20  static double x, y;
21  if (reset) {
22  reset = false;
23  x = img_gray->width / 2;
24  y = img_gray->height / 2;
25  tracker_psa.Track(img_gray); // To reset tracker call it twice
26  }
27  tracker_psa.Track(img_gray);
28  tracker_psa.Compensate(&x, &y);
29  cvCircle(image, cvPoint(int(x), int(y)), 10, CV_RGB(255,0,0));
30 }
31 
32 void track_psa_rot(IplImage *image, IplImage *img_gray) {
33  static TrackerPsaRot tracker_psa_rot;
34  static double x, y, r;
35  if (reset) {
36  reset = false;
37  x = img_gray->width / 2;
38  y = img_gray->height / 2;
39  r = 0;
40  tracker_psa_rot.Track(img_gray); // To reset tracker call it twice
41  }
42  tracker_psa_rot.Track(img_gray);
43  tracker_psa_rot.Compensate(&x, &y);
44  r += tracker_psa_rot.rotd;
45  cvCircle(image, cvPoint(int(x), int(y)), 15, CV_RGB(255,0,0));
46  double r_rad = r*3.1415926535/180;
47  cvLine(image, cvPoint(int(x), int(y)), cvPoint(int(x-sin(r_rad)*15), int(y+cos(r_rad)*15)), CV_RGB(255,0,0));
48 }
49 
50 void track_stat(IplImage *image, IplImage *img_gray) {
51  static TrackerStat tracker_stat;
52  static double x, y;
53  if (reset) {
54  reset = false;
55  x = img_gray->width / 2;
56  y = img_gray->height / 2;
57  tracker_stat.Track(img_gray); // To reset tracker call it twice
58  }
59  tracker_stat.Track(img_gray);
60  tracker_stat.Compensate(&x, &y);
61  cvCircle(image, cvPoint(int(x), int(y)), 10, CV_RGB(0,255,0));
62 }
63 
64 void track_stat_rot(IplImage *image, IplImage *img_gray) {
65  static TrackerStatRot tracker_stat_rot;
66  static double x, y, r;
67  if (reset) {
68  reset = false;
69  x = img_gray->width / 2;
70  y = img_gray->height / 2;
71  r = 0;
72  tracker_stat_rot.Track(img_gray); // To reset tracker call it twice
73  }
74  tracker_stat_rot.Track(img_gray);
75  tracker_stat_rot.Compensate(&x, &y);
76  r += tracker_stat_rot.rotd;
77  cvCircle(image, cvPoint(int(x), int(y)), 15, CV_RGB(0,255,0));
78  double r_rad = r*3.1415926535/180;
79  cvLine(image, cvPoint(int(x), int(y)), cvPoint(int(x-sin(r_rad)*15), int(y+cos(r_rad)*15)), CV_RGB(0,255,0));
80 }
81 
82 void track_features(IplImage *image, IplImage *img_gray) {
83  static TrackerFeatures tracker_features(200, 190, 0.01, 0, 4, 6);
84  if (reset) {
85  reset = false;
86  tracker_features.Reset();
87  }
88  tracker_features.Purge();
89  tracker_features.Track(img_gray);
90  for (int i=0; i<tracker_features.feature_count; i++) {
91  cvCircle(image,
92  cvPoint(int(tracker_features.features[i].x), int(tracker_features.features[i].y)), 2,
93  CV_RGB(tracker_features.ids[i]%255,(tracker_features.ids[i]*7)%255,(tracker_features.ids[i]*11)%255));
94  }
95 }
96 
97 const int nof_trackers = 6;
98 void (*(trackers[nof_trackers]))(IplImage *image, IplImage *img_gray) = {
99  track_none,
100  track_psa,
102  track_stat,
105 };
107  "No tracker - Press any key to change",
108  "TrackerPsa",
109  "TrackerPsaRot",
110  "TrackerStat",
111  "TrackerStatRot",
112  "TrackerFeatures",
113 };
114 
115 void videocallback(IplImage *image)
116 {
117  assert(image);
118  static Camera cam;
119  static IplImage *img_gray=NULL;
120  static bool init=true;
121 
122  if (init) {
123  init = false;
124  img_gray = CvTestbed::Instance().CreateImageWithProto("img_gray", image, 0, 1);
125  cout<<"Loading calibration: "<<calibrationFilename.str();
126  if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height)) {
127  cout<<" [Ok]"<<endl;
128  }
129  else {
130  cam.SetRes(image->width, image->height);
131  cout<<" [Fail]"<<endl;
132  }
133  }
134  if (image->nChannels == 1) cvCopy(image, img_gray);
135  else cvCvtColor(image, img_gray, CV_RGB2GRAY);
136 
137  trackers[tracker](image, img_gray);
138  cvPutText(image, tracker_names[tracker], cvPoint(3, image->height - 20), &font, CV_RGB(255, 255, 255));
139 }
140 
141 int keycallback(int key) {
142  if ((key == 'r') || (key == 't')) {
143  reset = true;
144  return 0;
145  }
146  else if ((key == 'n') || (key == ' ')){
147  tracker = ((tracker+1)%nof_trackers);
148  reset = true;
149  return 0;
150  }
151  return key;
152 }
153 
154 int main(int argc, char *argv[])
155 {
156  try {
157  // Output usage message
158  std::string filename(argv[0]);
159  filename = filename.substr(filename.find_last_of('\\') + 1);
160  std::cout << "SampleTrack" << std::endl;
161  std::cout << "===========" << std::endl;
162  std::cout << std::endl;
163  std::cout << "Description:" << std::endl;
164  std::cout << " This is an example of how to use the 'TrackerPsa', 'TrackerPsaRot'," << std::endl;
165  std::cout << " 'TrackerFeatures', 'TrackerStat' and 'TrackerStatRot' classes to" << std::endl;
166  std::cout << " track the optical flow of the video." << std::endl;
167  std::cout << std::endl;
168  std::cout << "Usage:" << std::endl;
169  std::cout << " " << filename << " [device]" << std::endl;
170  std::cout << std::endl;
171  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
172  std::cout << " highgui capture devices are prefered" << std::endl;
173  std::cout << std::endl;
174  std::cout << "Keyboard Shortcuts:" << std::endl;
175  std::cout << " r,t: reset tracker" << std::endl;
176  std::cout << " n,space: cycle through tracking algorithms" << std::endl;
177  std::cout << " q: quit" << std::endl;
178  std::cout << std::endl;
179 
180  // Initialize font
181  cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1.0, 1.0);
182 
183  // Initialise CvTestbed
186 
187  // Enumerate possible capture plugins
189  if (plugins.size() < 1) {
190  std::cout << "Could not find any capture plugins." << std::endl;
191  return 0;
192  }
193 
194  // Display capture plugins
195  std::cout << "Available Plugins: ";
196  outputEnumeratedPlugins(plugins);
197  std::cout << std::endl;
198 
199  // Enumerate possible capture devices
201  if (devices.size() < 1) {
202  std::cout << "Could not find any capture devices." << std::endl;
203  return 0;
204  }
205 
206  // Check command line argument for which device to use
207  int selectedDevice = defaultDevice(devices);
208  if (argc > 1) {
209  selectedDevice = atoi(argv[1]);
210  }
211  if (selectedDevice >= (int)devices.size()) {
212  selectedDevice = defaultDevice(devices);
213  }
214 
215  // Display capture devices
216  std::cout << "Enumerated Capture Devices:" << std::endl;
217  outputEnumeratedDevices(devices, selectedDevice);
218  std::cout << std::endl;
219 
220  // Create capture object from camera
221  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
222  std::string uniqueName = devices[selectedDevice].uniqueName();
223 
224  // Handle capture lifecycle and start video capture
225  // Note that loadSettings/saveSettings are not supported by all plugins
226  if (cap) {
227  std::stringstream settingsFilename;
228  settingsFilename << "camera_settings_" << uniqueName << ".xml";
229  calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
230 
231  cap->start();
232  cap->setResolution(640, 480);
233 
234  if (cap->loadSettings(settingsFilename.str())) {
235  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
236  }
237 
238  std::stringstream title;
239  title << "SampleTrack (" << cap->captureDevice().captureType() << ")";
240 
241  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
242 
243  if (cap->saveSettings(settingsFilename.str())) {
244  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
245  }
246 
247  cap->stop();
248  delete cap;
249  }
250  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
251  }
252  else {
253  std::cout << "Could not initialize the selected capture backend." << std::endl;
254  }
255 
256  return 0;
257  }
258  catch (const std::exception &e) {
259  std::cout << "Exception: " << e.what() << endl;
260  }
261  catch (...) {
262  std::cout << "Exception: unknown" << std::endl;
263  }
264 }
Main ALVAR namespace.
Definition: Alvar.h:174
void videocallback(IplImage *image)
static CvTestbed & Instance()
The one and only instance of CvTestbed is accessed using CvTestbed::Instance()
Definition: CvTestbed.cpp:88
filename
Capture * createCapture(const CaptureDevice captureDevice)
Create Capture class. Transfers onwership to the caller.
TrackerPsa implements a very simple PSA tracker
Definition: TrackerPsa.h:42
virtual void stop()=0
Stops the camera capture.
CvPoint2D32f * features
Track result: current features
void trackers(IplImage *image, IplImage *img_gray)
double Track(IplImage *img)
Track features.
This file implements a camera used for projecting points and computing homographies.
TrackerPsaRot implements a slightly extended version of a TrackerPsa which can also detect sideways r...
Definition: TrackerPsa.h:68
void track_none(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:15
virtual bool start()=0
Starts the camera capture.
void track_psa(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:18
CaptureDevice captureDevice()
The camera information associated to this capture object.
Definition: Capture.h:70
TrackerStatRot implements a slightly extended version of TrackerStat which can also detect sideways r...
Definition: TrackerStat.h:65
void SetVideoCallback(void(*_videocallback)(IplImage *image))
Set the videocallback function that will be called for every frame.
Definition: CvTestbed.cpp:93
static CaptureFactory * instance()
The singleton instance of CaptureFactory.
IplImage * CreateImageWithProto(const char *title, IplImage *proto, int depth=0, int channels=0)
Creates an image based on the given prototype and stores it with a given &#39;title&#39; (see CvTestbed::SetI...
Definition: CvTestbed.cpp:150
void SetRes(int _x_res, int _y_res)
If we have no calibration file we can still adjust the default calibration to current resolution...
Definition: Camera.cpp:374
bool reset
Definition: SampleTrack.cpp:11
void track_psa_rot(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:32
unsigned char * image
Definition: GlutViewer.cpp:155
void track_features(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:82
double Track(IplImage *img)
Track using PSA with rotation.
Definition: TrackerPsa.cpp:161
CvFont font
Definition: SampleTrack.cpp:12
TFSIMD_FORCE_INLINE const tfScalar & y() const
std::stringstream calibrationFilename
Definition: SampleTrack.cpp:13
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
int Purge()
Purge features that are considerably closer than the defined min_distance.
std::string captureType() const
The type of capture backend.
void track_stat(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:50
bool StartVideo(Capture *_cap, const char *_wintitle=0)
Start video input from given capture device.
Definition: CvTestbed.cpp:101
bool SetCalib(const char *calibfile, int _x_res, int _y_res, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Set the calibration file and the current resolution for which the calibration is adjusted to...
Definition: Camera.cpp:270
virtual void Compensate(double *x, double *y)
Definition: TrackerPsa.cpp:144
std::vector< std::string > CapturePluginVector
Vector of strings.
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
void SetKeyCallback(int(*_keycallback)(int key))
Sets the keyboard callback function that will be called when keyboard is pressed. ...
Definition: CvTestbed.cpp:97
This file implements a statistical tracker.
This file implements a PSA tracker.
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
This file implements a feature tracker.
double Track(IplImage *img)
Translation tracker (the simplest possible)
Definition: TrackerStat.cpp:40
double rotd
Track result rotation in degrees
Definition: TrackerPsa.h:75
virtual void Compensate(double *x, double *y)
TFSIMD_FORCE_INLINE const tfScalar & x() const
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
int feature_count
Track result: count of current features
TrackerFeatures tracks features using OpenCV&#39;s cvGoodFeaturesToTrack and cvCalcOpticalFlowPyrLK ...
bool init
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
int * ids
Track result: ID:s for current features
virtual void Compensate(double *x, double *y)
Definition: TrackerPsa.cpp:227
int main(int argc, char *argv[])
Capture interface that plugins must implement.
Definition: Capture.h:46
double Track(IplImage *img)
Translation + rotation tracker.
Definition: TrackerStat.cpp:65
virtual bool loadSettings(std::string filename)
Load camera settings from a file.
Definition: Capture.h:145
int defaultDevice(CaptureFactory::CaptureDeviceVector &devices)
Definition: Shared.h:40
Camera * cam
TrackerStat deduces the optical flow based on tracked features using Seppo Valli&#39;s statistical tracki...
Definition: TrackerStat.h:42
virtual void setResolution(const unsigned long xResolution, const unsigned long yResolution)
Set the resolution.
Definition: Capture.h:93
const int nof_trackers
Definition: SampleTrack.cpp:97
double rotd
Track result rotation in degrees
Definition: TrackerStat.h:70
int tracker
Definition: SampleTrack.cpp:10
r
int keycallback(int key)
void track_stat_rot(IplImage *image, IplImage *img_gray)
Definition: SampleTrack.cpp:64
virtual bool saveSettings(std::string filename)
Save camera settings to a file.
Definition: Capture.h:124
char tracker_names[nof_trackers][64]
double Track(IplImage *img)
Track using PSA.
Definition: TrackerPsa.cpp:46
virtual void Compensate(double *x, double *y)
Definition: TrackerStat.cpp:57


ar_track_alvar
Author(s): Scott Niekum
autogenerated on Mon Jun 10 2019 12:47:04