SampleMarkerDetector.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "MarkerDetector.h"
3 #include "GlutViewer.h"
4 #include "Shared.h"
5 using namespace alvar;
6 using namespace std;
7 
8 bool init=true;
9 const int marker_size=15;
11 Drawable d[32];
12 std::stringstream calibrationFilename;
13 
14 void videocallback(IplImage *image)
15 {
16  static IplImage *rgba;
17  bool flip_image = (image->origin?true:false);
18  if (flip_image) {
19  cvFlip(image);
20  image->origin = !image->origin;
21  }
22 
23  if (init) {
24  init = false;
25  cout<<"Loading calibration: "<<calibrationFilename.str();
26  if (cam.SetCalib(calibrationFilename.str().c_str(), image->width, image->height)) {
27  cout<<" [Ok]"<<endl;
28  } else {
29  cam.SetRes(image->width, image->height);
30  cout<<" [Fail]"<<endl;
31  }
32  double p[16];
33  cam.GetOpenglProjectionMatrix(p,image->width,image->height);
35  for (int i=0; i<32; i++) {
36  d[i].SetScale(marker_size);
37  }
38  rgba = CvTestbed::Instance().CreateImageWithProto("RGBA", image, 0, 4);
39  }
41  marker_detector.SetMarkerSize(marker_size); // for marker ids larger than 255, set the content resolution accordingly
42  //static MarkerDetector<MarkerArtoolkit> marker_detector;
43  //marker_detector.SetMarkerSize(2.8, 3, 1.5);
44 
45  // Here we try to use RGBA just to make sure that also it works...
46  //cvCvtColor(image, rgba, CV_RGB2RGBA);
47  marker_detector.Detect(image, &cam, true, true);
49  for (size_t i=0; i<marker_detector.markers->size(); i++) {
50  if (i >= 32) break;
51 
52  Pose p = (*(marker_detector.markers))[i].pose;
53  p.GetMatrixGL(d[i].gl_mat);
54 
55  int id = (*(marker_detector.markers))[i].GetId();
56  double r = 1.0 - double(id+1)/32.0;
57  double g = 1.0 - double(id*3%32+1)/32.0;
58  double b = 1.0 - double(id*7%32+1)/32.0;
59  d[i].SetColor(r, g, b);
60 
61  GlutViewer::DrawableAdd(&(d[i]));
62  }
63 
64  if (flip_image) {
65  cvFlip(image);
66  image->origin = !image->origin;
67  }
68 }
69 
70 int main(int argc, char *argv[])
71 {
72  try {
73  // Output usage message
74  std::string filename(argv[0]);
75  filename = filename.substr(filename.find_last_of('\\') + 1);
76  std::cout << "SampleMarkerDetector" << std::endl;
77  std::cout << "====================" << std::endl;
78  std::cout << std::endl;
79  std::cout << "Description:" << std::endl;
80  std::cout << " This is an example of how to detect 'MarkerData' markers using" << std::endl;
81  std::cout << " 'MarkerDetector' and visualize them using 'GlutViewer'. In the" << std::endl;
82  std::cout << " SampleMarkerDetector window, various debug information is shown" << std::endl;
83  std::cout << " about the detected markers. The coordinate axes and a virtual cube" << std::endl;
84  std::cout << " are superimposed onto the markers to visualize the detected pose." << std::endl;
85  std::cout << " For each marker, a small image of the marker content is displayed" << std::endl;
86  std::cout << " at the origin and the marker number is displayed at one of the" << std::endl;
87  std::cout << " corners. At the opposing corner, the error estimation percentages" << std::endl;
88  std::cout << " 'MARGIN_ERROR' and 'DECODE_ERROR' (red) or 'TRACK_ERROR' (dark red)" << std::endl;
89  std::cout << " are displayed." << std::endl;
90  std::cout << std::endl;
91  std::cout << " In the AR window, squares are drawn over the marker positions using" << std::endl;
92  std::cout << " OpenGL. In the VR window, the squares are drawn with respect to the" << std::endl;
93  std::cout << " camera coordinate frame. The viewpoint can be modified by dragging" << std::endl;
94  std::cout << " with the left and right mouse buttons." << std::endl;
95  std::cout << std::endl;
96  std::cout << "Usage:" << std::endl;
97  std::cout << " " << filename << " [device|filename]" << std::endl;
98  std::cout << std::endl;
99  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
100  std::cout << " highgui capture devices are prefered" << std::endl;
101  std::cout << " filename string specifying a media file as input" << std::endl;
102  std::cout << std::endl;
103  std::cout << "Keyboard Shortcuts:" << std::endl;
104  std::cout << " q: quit" << std::endl;
105  std::cout << std::endl;
106 
107  // Initialise GlutViewer and CvTestbed
108  GlutViewer::Start(argc, argv, 640, 480);
110 
111  // Create capture object from camera (argv[1] is a number) or from file (argv[1] is a string)
112  Capture *cap;
113  std::string uniqueName;
114  if ((argc > 1) && (!isdigit(argv[1][0]))) {
115  // Manually create capture device and initialize capture object
116  CaptureDevice device("file", argv[1]);
117  cap = CaptureFactory::instance()->createCapture(device);
118  uniqueName = "file";
119  }
120  else {
121  // Enumerate possible capture plugins
123  if (plugins.size() < 1) {
124  std::cout << "Could not find any capture plugins." << std::endl;
125  return 0;
126  }
127 
128  // Display capture plugins
129  std::cout << "Available Plugins: ";
130  outputEnumeratedPlugins(plugins);
131  std::cout << std::endl;
132 
133  // Enumerate possible capture devices
135  if (devices.size() < 1) {
136  std::cout << "Could not find any capture devices." << std::endl;
137  return 0;
138  }
139 
140  // Check command line argument for which device to use
141  int selectedDevice = defaultDevice(devices);
142  if (argc > 1) {
143  selectedDevice = atoi(argv[1]);
144  }
145  if (selectedDevice >= (int)devices.size()) {
146  selectedDevice = defaultDevice(devices);
147  }
148 
149  // Display capture devices
150  std::cout << "Enumerated Capture Devices:" << std::endl;
151  outputEnumeratedDevices(devices, selectedDevice);
152  std::cout << std::endl;
153 
154  // Create capture object from camera
155  cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
156  uniqueName = devices[selectedDevice].uniqueName();
157  }
158 
159  // Handle capture lifecycle and start video capture
160  // Note that loadSettings/saveSettings are not supported by all plugins
161  if (cap) {
162  std::stringstream settingsFilename;
163  settingsFilename << "camera_settings_" << uniqueName << ".xml";
164  calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
165 
166  cap->start();
167  cap->setResolution(640, 480);
168 
169  if (cap->loadSettings(settingsFilename.str())) {
170  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
171  }
172 
173  std::stringstream title;
174  title << "SampleMarkerDetector (" << cap->captureDevice().captureType() << ")";
175 
176  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
177 
178  if (cap->saveSettings(settingsFilename.str())) {
179  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
180  }
181 
182  cap->stop();
183  delete cap;
184  }
185  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
186  }
187  else {
188  std::cout << "Could not initialize the selected capture backend." << std::endl;
189  }
190 
191  return 0;
192  }
193  catch (const std::exception &e) {
194  std::cout << "Exception: " << e.what() << endl;
195  }
196  catch (...) {
197  std::cout << "Exception: unknown" << std::endl;
198  }
199 }
Main ALVAR namespace.
Definition: Alvar.h:174
void GetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height, const float far_clip=1000.0f, const float near_clip=0.1f)
Get OpenGL matrix Generates the OpenGL projection matrix based on OpenCV intrinsic camera matrix K...
Definition: Camera.cpp:394
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.
void GetMatrixGL(double gl[16], bool mirror=true)
Get the transformation matrix representation of the Pose using OpenGL&#39;s transposed format...
Definition: Pose.cpp:104
virtual bool start()=0
Starts the camera capture.
void SetMarkerSize(double _edge_length=1, int _res=5, double _margin=2)
CaptureDevice captureDevice()
The camera information associated to this capture object.
Definition: Capture.h:70
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.
This file implements a generic marker detector.
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
unsigned char * image
Definition: GlutViewer.cpp:155
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
Camera cam
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.
int main(int argc, char *argv[])
void SetColor(double _r=1, double _g=1, double _b=1)
Definition: GlutViewer.cpp:19
CaptureDevice holder for camera information.
Definition: CaptureDevice.h:44
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
MarkerDetector for detecting markers of type M
void SetGlProjectionMatrix(double p[16])
Definition: GlutViewer.cpp:388
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
bool init
void Start(int argc, char **argv, int w, int h, float r=300.0)
Definition: GlutViewer.cpp:227
void DrawableClear()
Definition: GlutViewer.cpp:416
Pose representation derived from the Rotation class
Definition: Pose.h:50
void DrawableAdd(Drawable *item)
Definition: GlutViewer.cpp:421
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
Drawable d[32]
void videocallback(IplImage *image)
MarkerDetector< MarkerData > marker_detector
std::stringstream calibrationFilename
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
void SetScale(double _scale)
Definition: GlutViewer.cpp:15
Capture interface that plugins must implement.
Definition: Capture.h:46
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
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
virtual void setResolution(const unsigned long xResolution, const unsigned long yResolution)
Set the resolution.
Definition: Capture.h:93
const int marker_size
r
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