SampleMarkerlessDetector.cpp
Go to the documentation of this file.
1 #include "CvTestbed.h"
2 #include "GlutViewer.h"
3 #include "Shared.h"
4 #include "FernImageDetector.h"
5 #include "FernPoseEstimator.h"
6 
7 using namespace alvar;
8 using namespace std;
9 
10 bool init = true;
11 std::stringstream calibrationFilename;
15 cv::Mat gray;
16 bool reset = false;
17 
18 void videocallback(IplImage *image)
19 {
20  bool flip_image = (image->origin?true:false);
21  if (flip_image) {
22  cvFlip(image);
23  image->origin = !image->origin;
24  }
25 
26  if (init) {
27  init = false;
28  cout << "Loading calibration: " << calibrationFilename.str();
29  if (fernEstimator.setCalibration(calibrationFilename.str(), image->width, image->height)) {
30  cout << " [Ok]" << endl;
31  } else {
32  fernEstimator.setResolution(image->width, image->height);
33  cout << " [Fail]" << endl;
34  }
35  double p[16];
36  fernEstimator.camera().GetOpenglProjectionMatrix(p, image->width, image->height);
38  d.SetScale(10);
39  gray = cv::Mat(image);
40  }
41 
42  if (image->nChannels == 3) {
43  cv::Mat img = cvarrToMat(image);
44  cv::cvtColor(img, gray, CV_RGB2GRAY);
45  }
46  else {
47  gray = image;
48  }
49 
50  vector<CvPoint2D64f> ipts;
51  vector<CvPoint3D64f> mpts;
52 
55  fernDetector.modelPoints(mpts, true);
56  double test = fernDetector.inlierRatio();
57  if (test > 0.15 && mpts.size() > 4) {
58  fernEstimator.calculateFromPointCorrespondences(mpts, ipts);
59  }
60 
62  Pose pose = fernEstimator.pose();
63  pose.GetMatrixGL(d.gl_mat);
65 
66  if (flip_image) {
67  cvFlip(image);
68  image->origin = !image->origin;
69  }
70 }
71 
72 int main(int argc, char *argv[])
73 {
74  try {
75  // Output usage message
76  std::string filename(argv[0]);
77  filename = filename.substr(filename.find_last_of('\\') + 1);
78  std::cout << "SampleMarkerlessDetector" << std::endl;
79  std::cout << "========================" << std::endl;
80  std::cout << std::endl;
81  std::cout << "Description:" << std::endl;
82  std::cout << " This is an example of how to use the 'FernImageDetector' and" << std::endl;
83  std::cout << " 'FernPoseEstimator' classes to detect and track an image and" << std::endl;
84  std::cout << " visualize it using 'GlutViewer'. The classification must first" << std::endl;
85  std::cout << " be trained with the SampleMarkerlessCreator sample and the" << std::endl;
86  std::cout << " resulting file passed as an argument to this sample." << std::endl;
87  std::cout << std::endl;
88  std::cout << " For optimal results, a high quality USB camera or a Firewire" << std::endl;
89  std::cout << " camera is necessary. It is also advised to calibrate the camera" << std::endl;
90  std::cout << " using the SampleCamCalib sample. It should be noted that the size" << std::endl;
91  std::cout << " of the trained image will affect the optimal distance for detection." << std::endl;
92  std::cout << std::endl;
93  std::cout << "Usage:" << std::endl;
94  std::cout << " " << filename << " filename [device]" << std::endl;
95  std::cout << std::endl;
96  std::cout << " filename the filename of classifier (.dat)" << std::endl;
97  std::cout << " device integer selecting device from enumeration list (default 0)" << std::endl;
98  std::cout << " highgui capture devices are prefered" << std::endl;
99  std::cout << std::endl;
100  std::cout << "Keyboard Shortcuts:" << std::endl;
101  std::cout << " q: quit" << std::endl;
102  std::cout << std::endl;
103 
104  if (argc < 2) {
105  std::cout << "Filename not specified." << std::endl;
106  return 0;
107  }
108  std::string classifierFilename(argv[1]);
109 
110  // Initialise GlutViewer and CvTestbed
111  GlutViewer::Start(argc, argv, 640, 480);
113 
114  // Enumerate possible capture plugins
116  if (plugins.size() < 1) {
117  std::cout << "Could not find any capture plugins." << std::endl;
118  return 0;
119  }
120 
121  // Display capture plugins
122  std::cout << "Available Plugins: ";
123  outputEnumeratedPlugins(plugins);
124  std::cout << std::endl;
125 
126  // Enumerate possible capture devices
128  if (devices.size() < 1) {
129  std::cout << "Could not find any capture devices." << std::endl;
130  return 0;
131  }
132 
133  // Check command line argument for which device to use
134  int selectedDevice = defaultDevice(devices);
135  if (argc > 2) {
136  selectedDevice = atoi(argv[2]);
137  }
138  if (selectedDevice >= (int)devices.size()) {
139  selectedDevice = defaultDevice(devices);
140  }
141 
142  // Display capture devices
143  std::cout << "Enumerated Capture Devices:" << std::endl;
144  outputEnumeratedDevices(devices, selectedDevice);
145  std::cout << std::endl;
146 
147  // Create capture object from camera
148  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
149  std::string uniqueName = devices[selectedDevice].uniqueName();
150 
151  // Handle capture lifecycle and start video capture
152  // Note that loadSettings/saveSettings are not supported by all plugins
153  if (cap) {
154  std::cout << "Loading classifier." << std::endl;
155  if (!fernDetector.read(classifierFilename)) {
156  std::cout << "Loading classifier failed." << std::endl;
157  cap->stop();
158  delete cap;
159  return 1;
160  }
161 
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
This file implements a Fern-based image detector.
filename
Capture * createCapture(const CaptureDevice captureDevice)
Create Capture class. Transfers onwership to the caller.
virtual void stop()=0
Stops the camera capture.
FernImageDetector fernDetector(true)
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.
CaptureDevice captureDevice()
The camera information associated to this capture object.
Definition: Capture.h:70
Image detector based on a Fern classifier.
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.
void modelPoints(vector< CvPoint3D64f > &points, bool normalize=true)
unsigned char * image
Definition: GlutViewer.cpp:155
double gl_mat[16]
Definition: GlutViewer.h:33
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
FernPoseEstimator fernEstimator
This file implements a pose estimator for the Fern-based image detector.
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
void setResolution(int width, int height)
std::vector< std::string > CapturePluginVector
Vector of strings.
void videocallback(IplImage *image)
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
void SetGlProjectionMatrix(double p[16])
Definition: GlutViewer.cpp:388
void imagePoints(vector< CvPoint2D64f > &points)
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
void Start(int argc, char **argv, int w, int h, float r=300.0)
Definition: GlutViewer.cpp:227
void DrawableClear()
Definition: GlutViewer.cpp:416
bool setCalibration(const std::string &filename, int width, int height)
Pose representation derived from the Rotation class
Definition: Pose.h:50
void DrawableAdd(Drawable *item)
Definition: GlutViewer.cpp:421
std::stringstream calibrationFilename
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
cv::Mat gray
bool read(const std::string &filename, const bool binary=true)
int main(int argc, char *argv[])
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
void findFeatures(Mat &image, bool planeAssumption=true)
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
Pose estimation class for FernImageDetector.
virtual bool saveSettings(std::string filename)
Save camera settings to a file.
Definition: Capture.h:124
void calculateFromPointCorrespondences(ModelPointVector &mpts, ImagePointVector &ipts)
Drawable d


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