SampleMultiMarker.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "MarkerDetector.h"
3 #include "MultiMarker.h"
4 #include "Pose.h"
5 #include "Shared.h"
6 using namespace alvar;
7 using namespace std;
8 
9 int visualize=0;
10 bool detect_additional = false;
11 const int nof_markers = 5;
12 const double marker_size = 4;
14 //MultiMarker<MarkerData> *multi_marker;
16 std::stringstream calibrationFilename;
17 
18 void videocallback(IplImage *image)
19 {
20  static Camera cam;
21  Pose pose;
22  bool flip_image = (image->origin?true:false);
23  if (flip_image) {
24  cvFlip(image);
25  image->origin = !image->origin;
26  }
27 
28  static bool init = true;
29  if (init)
30  {
31 
32  init = false;
33 
34  // Initialize camera
35  cout<<"Loading calibration: "<<calibrationFilename.str();
36 
37  if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height))
38  {
39  cout<<" [Ok]"<<endl;
40  }
41  else
42  {
43  cam.SetRes(image->width, image->height);
44  cout<<" [Fail]"<<endl;
45  }
46 
47  vector<int> id_vector;
48  for(int i = 0; i < nof_markers; ++i)
49  id_vector.push_back(i);
50 
51  // We make the initialization for MultiMarkerBundle using a fixed marker field (can be printed from ALVAR.pdf)
52  marker_detector.SetMarkerSize(marker_size);
53  marker_detector.SetMarkerSizeForId(0, marker_size*2);
54  multi_marker = new MultiMarker(id_vector);
55  pose.Reset();
56  multi_marker->PointCloudAdd(0, marker_size*2, pose);
57  pose.SetTranslation(-marker_size*2.5, +marker_size*1.5, 0);
58  multi_marker->PointCloudAdd(1, marker_size, pose);
59  pose.SetTranslation(+marker_size*2.5, +marker_size*1.5, 0);
60  multi_marker->PointCloudAdd(2, marker_size, pose);
61  pose.SetTranslation(-marker_size*2.5, -marker_size*1.5, 0);
62  multi_marker->PointCloudAdd(3, marker_size, pose);
63  pose.SetTranslation(+marker_size*2.5, -marker_size*1.5, 0);
64  multi_marker->PointCloudAdd(4, marker_size, pose);
65  }
66 
67  double error=-1;
68  if (marker_detector.Detect(image, &cam, true, (visualize == 1), 0.0)) {
69  if (detect_additional) {
70  error = multi_marker->Update(marker_detector.markers, &cam, pose);
71  multi_marker->SetTrackMarkers(marker_detector, &cam, pose, visualize ? image : NULL);
72  marker_detector.DetectAdditional(image, &cam, (visualize == 1));
73  }
74  if (visualize == 2)
75  error = multi_marker->Update(marker_detector.markers, &cam, pose, image);
76  else
77  error = multi_marker->Update(marker_detector.markers, &cam, pose);
78  }
79 
80  static Marker foo;
82  if ((error >= 0) && (error < 5))
83  {
84  foo.pose = pose;
85  }
86  foo.Visualize(image, &cam);
87 
88  if (flip_image) {
89  cvFlip(image);
90  image->origin = !image->origin;
91  }
92 }
93 
94 int keycallback(int key)
95 {
96  if(key == 'v')
97  {
98  visualize = (visualize+1)%3;
99  }
100  else if(key == 'l')
101  {
102  if(multi_marker->Load("mmarker.xml", alvar::FILE_FORMAT_XML))
103  {
104  cout<<"Multi marker loaded"<<endl;
105  }
106  else
107  cout<<"Cannot load multi marker"<<endl;
108  }
109  else if(key == 'd')
110  {
113  cout<<"Unreadable marker detection enabled."<<endl;
114  else
115  cout<<"Unreadable marker detection disabled."<<endl;
116  }
117  else return key;
118 
119  return 0;
120 }
121 
122 int main(int argc, char *argv[])
123 {
124  try {
125  // Output usage message
126  std::string filename(argv[0]);
127  filename = filename.substr(filename.find_last_of('\\') + 1);
128  std::cout << "SampleMultiMarker" << std::endl;
129  std::cout << "=================" << std::endl;
130  std::cout << std::endl;
131  std::cout << "Description:" << std::endl;
132  std::cout << " This is an example of how to use the 'MultiMarker' class to detect" << std::endl;
133  std::cout << " preconfigured multi-marker setup (see ALVAR.pdf). A large cube is drawn" << std::endl;
134  std::cout << " over the detected multi-marker even when only some of the markers are" << std::endl;
135  std::cout << " visible." << std::endl;
136  std::cout << std::endl;
137  std::cout << "Usage:" << std::endl;
138  std::cout << " " << filename << " [device]" << std::endl;
139  std::cout << std::endl;
140  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
141  std::cout << " highgui capture devices are prefered" << std::endl;
142  std::cout << std::endl;
143  std::cout << "Keyboard Shortcuts:" << std::endl;
144  std::cout << " v: switch between three debug visualizations" << std::endl;
145  std::cout << " l: load marker configuration from mmarker.txt" << std::endl;
146  std::cout << " d: toggle the detection of non-readable markers" << std::endl;
147  std::cout << " q: quit" << std::endl;
148  std::cout << std::endl;
149 
150  // Initialise CvTestbed
153 
154  // Enumerate possible capture plugins
156  if (plugins.size() < 1) {
157  std::cout << "Could not find any capture plugins." << std::endl;
158  return 0;
159  }
160 
161  // Display capture plugins
162  std::cout << "Available Plugins: ";
163  outputEnumeratedPlugins(plugins);
164  std::cout << std::endl;
165 
166  // Enumerate possible capture devices
168  if (devices.size() < 1) {
169  std::cout << "Could not find any capture devices." << std::endl;
170  return 0;
171  }
172 
173  // Check command line argument for which device to use
174  int selectedDevice = defaultDevice(devices);
175  if (argc > 1) {
176  selectedDevice = atoi(argv[1]);
177  }
178  if (selectedDevice >= (int)devices.size()) {
179  selectedDevice = defaultDevice(devices);
180  }
181 
182  // Display capture devices
183  std::cout << "Enumerated Capture Devices:" << std::endl;
184  outputEnumeratedDevices(devices, selectedDevice);
185  std::cout << std::endl;
186 
187  // Create capture object from camera
188  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
189  std::string uniqueName = devices[selectedDevice].uniqueName();
190 
191  // Handle capture lifecycle and start video capture
192  // Note that loadSettings/saveSettings are not supported by all plugins
193  if (cap) {
194  std::stringstream settingsFilename;
195  settingsFilename << "camera_settings_" << uniqueName << ".xml";
196  calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
197 
198  cap->start();
199  cap->setResolution(640, 480);
200 
201  if (cap->loadSettings(settingsFilename.str())) {
202  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
203  }
204 
205  std::stringstream title;
206  title << "SampleMultiMarker (" << cap->captureDevice().captureType() << ")";
207 
208  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
209 
210  if (cap->saveSettings(settingsFilename.str())) {
211  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
212  }
213 
214  cap->stop();
215  delete cap;
216  }
217  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
218  }
219  else {
220  std::cout << "Could not initialize the selected capture backend." << std::endl;
221  }
222 
223  return 0;
224  }
225  catch (const std::exception &e) {
226  std::cout << "Exception: " << e.what() << endl;
227  }
228  catch (...) {
229  std::cout << "Exception: unknown" << std::endl;
230  }
231 }
Main ALVAR namespace.
Definition: Alvar.h:174
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.
virtual void stop()=0
Stops the camera capture.
virtual bool start()=0
Starts the camera capture.
void SetMarkerSize(double _edge_length=1, int _res=5, double _margin=2)
int main(int argc, char *argv[])
CaptureDevice captureDevice()
The camera information associated to this capture object.
Definition: Capture.h:70
int visualize
const int nof_markers
void SetVideoCallback(void(*_videocallback)(IplImage *image))
Set the videocallback function that will be called for every frame.
Definition: CvTestbed.cpp:93
void Visualize(IplImage *image, Camera *cam, CvScalar color=CV_RGB(255, 0, 0)) const
Visualize the marker.
Definition: Marker.cpp:134
static CaptureFactory * instance()
The singleton instance of CaptureFactory.
This file implements a generic marker detector.
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
unsigned char * image
Definition: GlutViewer.cpp:155
Base class for using MultiMarker.
Definition: MultiMarker.h:47
std::stringstream calibrationFilename
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
int DetectAdditional(IplImage *image, Camera *cam, bool visualize=false, double max_track_error=0.2)
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
void videocallback(IplImage *image)
std::string captureType() const
The type of capture backend.
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
std::vector< M, Eigen::aligned_allocator< M > > * markers
std::vector< std::string > CapturePluginVector
Vector of strings.
MarkerDetector< MarkerData > marker_detector
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
MarkerDetector for detecting markers of type M
void SetKeyCallback(int(*_keycallback)(int key))
Sets the keyboard callback function that will be called when keyboard is pressed. ...
Definition: CvTestbed.cpp:97
Pose pose
The current marker Pose.
Definition: Marker.h:136
void Reset()
Definition: Pose.cpp:69
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
Pose representation derived from the Rotation class
Definition: Pose.h:50
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
const double marker_size
Basic 2D Marker functionality.
Definition: Marker.h:52
XML file format.
Definition: FileFormat.h:66
void PointCloudAdd(int marker_id, double edge_length, Pose &pose)
Adds marker corners to 3D point cloud of multi marker.
bool init
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
This file implements a multi-marker.
MultiMarker * multi_marker
bool Load(const char *fname, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Loads multi marker configuration from a text file.
Capture interface that plugins must implement.
Definition: Capture.h:46
void SetMarkerSize(double _edge_length=0, int _res=0, double _margin=0)
Method for resizing the marker dimensions.
Definition: Marker.cpp:356
int Detect(IplImage *image, Camera *cam, bool track=false, bool visualize=false, double max_new_marker_error=0.08, double max_track_error=0.2, LabelingMethod labeling_method=CVSEQ, bool update_pose=true)
Detect Marker &#39;s from image
This file implements a pose.
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
virtual void setResolution(const unsigned long xResolution, const unsigned long yResolution)
Set the resolution.
Definition: Capture.h:93
void SetTranslation(const CvMat *tra)
Definition: Pose.cpp:150
bool detect_additional
void SetMarkerSizeForId(unsigned long id, double _edge_length=1)
int SetTrackMarkers(MarkerDetector< M > &marker_detector, Camera *cam, Pose &pose, IplImage *image=0)
Set new markers to be tracked for MarkerDetector Sometimes the MultiMarker implementation knows more ...
Definition: MultiMarker.h:191
double Update(const std::vector< M, Eigen::aligned_allocator< M > > *markers, Camera *cam, Pose &pose, IplImage *image=0)
Calls GetPose to obtain camera pose.
Definition: MultiMarker.h:120
int keycallback(int key)
virtual bool saveSettings(std::string filename)
Save camera settings to a file.
Definition: Capture.h:124


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