SampleCamCalib.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "Camera.h"
3 #include "CvTestbed.h"
4 #include "Shared.h"
5 using namespace alvar;
6 using namespace std;
7 
8 const int calib_count_max=50;
9 const int etalon_rows=6;
10 const int etalon_columns=8;
11 std::stringstream calibrationFilename;
12 
13 void videocallback(IplImage *image)
14 {
15  static bool calibrated = false;
16  static int calib_count=0;
17  static Camera cam;
18  static ProjPoints pp;
19  static int64 prev_tick=0;
20  static bool initialized = false;
21 
22  if (!initialized) {
23  cam.SetRes(image->width, image->height);
24  prev_tick = cvGetTickCount();
25  initialized = true;
26  }
27 
28  bool flip_image = (image->origin?true:false);
29  if (flip_image) {
30  cvFlip(image);
31  image->origin = !image->origin;
32  }
33 
34  assert(image);
35  if (!calibrated) {
36  // If we have already collected enough data to make the calibration
37  // - We are ready to end the capture loop
38  // - Calibrate
39  // - Save the calibration file
40  if (calib_count >= calib_count_max) {
41  std::cout<<"Calibrating..."<<endl;
42  calib_count = 0;
43  cam.Calibrate(pp);
44  pp.Reset();
45  cam.SaveCalib(calibrationFilename.str().c_str());
46  std::cout<<"Saving calibration: "<<calibrationFilename.str()<<endl;
47  calibrated = true;
48  }
49  // If we are still collecting calibration data
50  // - For every 1.5s add calibration data from detected 7*9 chessboard (and visualize it if true)
51  else {
52  int64 tick = cvGetTickCount();
53  if ((tick - prev_tick) > (cvGetTickFrequency() * 1000 * 1000 * 1.5)) {
54  if (pp.AddPointsUsingChessboard(image, 2.8, etalon_rows, etalon_columns, true)) {
55  prev_tick = tick;
56  calib_count++;
57  cout<<calib_count<<"/"<<calib_count_max<<endl;
58  }
59  }
60  }
61  } else {
62  if (pp.AddPointsUsingChessboard(image, 2.5, etalon_rows, etalon_columns, true)) {
63  Pose pose;
65  cam.ProjectPoints(pp.object_points, &pose, pp.image_points);
66  for (size_t i=0; i<pp.image_points.size(); i++) {
67  cvCircle(image, cvPoint((int)pp.image_points[i].x, (int)pp.image_points[i].y), 6, CV_RGB(0, 0, 255));
68  }
69  pp.Reset();
70  }
71  }
72 
73  if (flip_image) {
74  cvFlip(image);
75  image->origin = !image->origin;
76  }
77 }
78 
79 int main(int argc, char *argv[])
80 {
81  try {
82  // Output usage message
83  std::string filename(argv[0]);
84  filename = filename.substr(filename.find_last_of('\\') + 1);
85  std::cout << "SampleCamCalib" << std::endl;
86  std::cout << "==============" << std::endl;
87  std::cout << std::endl;
88  std::cout << "Description:" << std::endl;
89  std::cout << " This is an example of how to use the 'Camera' and 'ProjPoints' classes" << std::endl;
90  std::cout << " to perform camera calibration. Point the camera to the chessboard" << std::endl;
91  std::cout << " calibration pattern (see ALVAR.pdf) from several directions until 50" << std::endl;
92  std::cout << " calibration images are collected. A 'calib.xml' file that contains the" << std::endl;
93  std::cout << " internal parameters of the camera is generated and can be used by other" << std::endl;
94  std::cout << " applications that require a calibrated camera." << std::endl;
95  std::cout << std::endl;
96  std::cout << "Usage:" << std::endl;
97  std::cout << " " << filename << " [device]" << 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 << std::endl;
102  std::cout << "Keyboard Shortcuts:" << std::endl;
103  std::cout << " q: quit" << std::endl;
104  std::cout << std::endl;
105 
106  // Initialise CvTestbed
108 
109  // Enumerate possible capture plugins
111  if (plugins.size() < 1) {
112  std::cout << "Could not find any capture plugins." << std::endl;
113  return 0;
114  }
115 
116  // Display capture plugins
117  std::cout << "Available Plugins: ";
118  outputEnumeratedPlugins(plugins);
119  std::cout << std::endl;
120 
121  // Enumerate possible capture devices
123  if (devices.size() < 1) {
124  std::cout << "Could not find any capture devices." << std::endl;
125  return 0;
126  }
127 
128  // Check command line argument for which device to use
129  int selectedDevice = defaultDevice(devices);
130  if (argc > 1) {
131  selectedDevice = atoi(argv[1]);
132  }
133  if (selectedDevice >= (int)devices.size()) {
134  selectedDevice = defaultDevice(devices);
135  }
136 
137  // Display capture devices
138  std::cout << "Enumerated Capture Devices:" << std::endl;
139  outputEnumeratedDevices(devices, selectedDevice);
140  std::cout << std::endl;
141 
142  // Create capture object from camera
143  Capture *cap = CaptureFactory::instance()->createCapture(devices[selectedDevice]);
144  std::string uniqueName = devices[selectedDevice].uniqueName();
145 
146  // Handle capture lifecycle and start video capture
147  // Note that loadSettings/saveSettings are not supported by all plugins
148  if (cap) {
149  std::stringstream settingsFilename;
150  settingsFilename << "camera_settings_" << uniqueName << ".xml";
151  calibrationFilename << "camera_calibration_" << uniqueName << ".xml";
152 
153  cap->start();
154  cap->setResolution(640, 480);
155 
156  if (cap->loadSettings(settingsFilename.str())) {
157  std::cout << "Loading settings: " << settingsFilename.str() << std::endl;
158  }
159 
160  std::stringstream title;
161  title << "SampleCamCalib (" << cap->captureDevice().captureType() << ")";
162 
163  CvTestbed::Instance().StartVideo(cap, title.str().c_str());
164 
165  if (cap->saveSettings(settingsFilename.str())) {
166  std::cout << "Saving settings: " << settingsFilename.str() << std::endl;
167  }
168 
169  cap->stop();
170  delete cap;
171  }
172  else if (CvTestbed::Instance().StartVideo(0, argv[0])) {
173  }
174  else {
175  std::cout << "Could not initialize the selected capture backend." << std::endl;
176  }
177 
178  return 0;
179  }
180  catch (const std::exception &e) {
181  std::cout << "Exception: " << e.what() << endl;
182  }
183  catch (...) {
184  std::cout << "Exception: unknown" << std::endl;
185  }
186 }
Main ALVAR namespace.
Definition: Alvar.h:174
const int etalon_columns
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.
bool SaveCalib(const char *calibfile, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Save the current calibration information to a file.
Definition: Camera.cpp:336
virtual void stop()=0
Stops the camera capture.
int main(int argc, char *argv[])
This file implements a camera used for projecting points and computing homographies.
virtual bool start()=0
Starts the camera capture.
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.
void Reset()
Reset object_points , image_points and point_counts.
Definition: Camera.cpp:35
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::stringstream calibrationFilename
std::vector< CaptureDevice > CaptureDeviceVector
Vector of CaptureDevices.
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
Definition: Camera.h:82
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
std::vector< std::string > CapturePluginVector
Vector of strings.
void outputEnumeratedPlugins(CaptureFactory::CapturePluginVector &plugins)
Definition: Shared.h:8
std::vector< CvPoint3D64f > object_points
3D object points corresponding with the detected 2D image points.
Definition: Camera.h:57
void ProjectPoints(std::vector< CvPoint3D64f > &pw, Pose *pose, std::vector< CvPoint2D64f > &pi) const
Project points.
CaptureDeviceVector enumerateDevices(const std::string &captureType="")
Enumerate capture devices currently available.
Simple structure for collecting 2D and 3D points e.g. for camera calibration.
Definition: Camera.h:52
Pose representation derived from the Rotation class
Definition: Pose.h:50
CapturePluginVector enumeratePlugins()
Enumerate capture plugins currently available.
void Calibrate(ProjPoints &pp)
Calibrate using the collected ProjPoints.
Definition: Camera.cpp:351
const int etalon_rows
std::vector< CvPoint2D64f > image_points
Detected 2D object points If point_counts[0] == 10, then the first 10 points are detected in the firs...
Definition: Camera.h:64
void outputEnumeratedDevices(CaptureFactory::CaptureDeviceVector &devices, int selectedDevice)
Definition: Shared.h:20
bool AddPointsUsingChessboard(IplImage *image, double etalon_square_size, int etalon_rows, int etalon_columns, bool visualize)
Add elements to object_points , image_points and point_counts using Chessboard pattern.
Definition: Camera.cpp:42
Capture interface that plugins must implement.
Definition: Capture.h:46
void CalcExteriorOrientation(std::vector< CvPoint3D64f > &pw, std::vector< CvPoint2D64f > &pi, Pose *pose)
Calculate exterior orientation.
virtual bool loadSettings(std::string filename)
Load camera settings from a file.
Definition: Capture.h:145
void videocallback(IplImage *image)
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
const int calib_count_max
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