tools/Camera/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "rtabmap/core/CameraRGB.h"
29 #include "rtabmap/core/DBReader.h"
31 #include "rtabmap/utilite/UFile.h"
34 #include <opencv2/highgui/highgui.hpp>
35 #include <opencv2/highgui/highgui_c.h>
36 #include <stdio.h>
37 
38 void showUsage()
39 {
40  printf("\nUsage:\n"
41  "rtabmap-camera [option] \n"
42  " Options:\n"
43  " --device # USB camera device id (default 0).\n"
44  " --rate # Frame rate (default 0 Hz). 0 means as fast as possible.\n"
45  " --path "" Path to a directory of images or a video file.\n"
46  " --calibration "" Calibration file (*.yaml).\n\n");
47  exit(1);
48 }
49 
50 int main(int argc, char * argv[])
51 {
54 
55  int device = 0;
56  std::string path;
57  float rate = 0.0f;
58  std::string calibrationFile;
59  for(int i=1; i<argc; ++i)
60  {
61  if(strcmp(argv[i], "--rate") == 0)
62  {
63  ++i;
64  if(i < argc)
65  {
66  rate = uStr2Float(argv[i]);
67  if(rate < 0)
68  {
69  showUsage();
70  }
71  }
72  else
73  {
74  showUsage();
75  }
76  continue;
77  }
78  if(strcmp(argv[i], "--device") == 0)
79  {
80  ++i;
81  if(i < argc)
82  {
83  device = std::atoi(argv[i]);
84  if(device < 0)
85  {
86  showUsage();
87  }
88  }
89  else
90  {
91  showUsage();
92  }
93  continue;
94  }
95  if(strcmp(argv[i], "--path") == 0)
96  {
97  ++i;
98  if(i < argc)
99  {
100  path = argv[i];
101  }
102  else
103  {
104  showUsage();
105  }
106  continue;
107  }
108  if(strcmp(argv[i], "--calibration") == 0)
109  {
110  ++i;
111  if(i < argc)
112  {
113  calibrationFile = argv[i];
114  }
115  else
116  {
117  showUsage();
118  }
119  continue;
120  }
121 
122  printf("Unrecognized option : %s\n", argv[i]);
123  showUsage();
124  }
125 
126  if(path.empty())
127  {
128  UINFO("Using device %d", device);
129  }
130  else
131  {
132  UINFO("Using path %s", path.c_str());
133  }
134 
135  rtabmap::Camera * camera = 0;
136 
137  if(!path.empty())
138  {
139  if(UFile::exists(path))
140  {
141  if(UFile::getExtension(path).compare("db") == 0)
142  {
143  camera = new rtabmap::DBReader(path, rate);
144  }
145  else
146  {
147  camera = new rtabmap::CameraVideo(path, false, rate);
148  }
149  }
150  else if(UDirectory::exists(path))
151  {
152  camera = new rtabmap::CameraImages(path, rate);
153  }
154  else
155  {
156  UERROR("Path not valid! \"%s\"", path.c_str());
157  return -1;
158  }
159  }
160  else
161  {
162  camera = new rtabmap::CameraVideo(device, false, rate);
163  }
164 
165  if(camera)
166  {
167  if(!calibrationFile.empty())
168  {
169  UINFO("Set calibration: %s", calibrationFile.c_str());
170  }
171  if(!camera->init(UDirectory::getDir(calibrationFile), UFile::getName(calibrationFile)))
172  {
173  delete camera;
174  UERROR("Cannot initialize the camera.");
175  return -1;
176  }
177  }
178 
179  cv::Mat rgb;
180  rgb = camera->takeImage().imageRaw();
181  cv::namedWindow("Video", CV_WINDOW_AUTOSIZE); // create window
182  while(!rgb.empty())
183  {
184  cv::imshow("Video", rgb); // show frame
185 
186  int c = cv::waitKey(10); // wait 10 ms or for key stroke
187  if(c == 27)
188  break; // if ESC, break and quit
189 
190  rgb = camera->takeImage().imageRaw();
191  }
192  cv::destroyWindow("Video");
193  if(camera)
194  {
195  delete camera;
196  }
197  return 0;
198 }
std::string getName()
Definition: UFile.h:135
void showUsage()
int main(int argc, char *argv[])
static std::string getDir(const std::string &filePath)
Definition: UDirectory.cpp:273
float UTILITE_EXP uStr2Float(const std::string &str)
Some conversion functions.
std::string getExtension()
Definition: UFile.h:140
static void setLevel(ULogger::Level level)
Definition: ULogger.h:339
static void setType(Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
Definition: ULogger.cpp:176
bool exists()
Definition: UFile.h:104
#define UERROR(...)
ULogger class and convenient macros.
static bool exists(const std::string &dirPath)
Definition: UDirectory.cpp:249
#define UINFO(...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:31