main.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #include "rtabmap/core/Camera.h"
00029 #include "rtabmap/core/DBReader.h"
00030 #include "rtabmap/utilite/ULogger.h"
00031 #include "rtabmap/utilite/UFile.h"
00032 #include "rtabmap/utilite/UDirectory.h"
00033 #include "rtabmap/utilite/UConversion.h"
00034 #include <opencv2/highgui/highgui.hpp>
00035 #include <stdio.h>
00036 
00037 void showUsage()
00038 {
00039         printf("\nUsage:\n"
00040                         "rtabmap-camera [option] \n"
00041                         " Options:\n"
00042                         "    --device #            USB camera device id (default 0).\n"
00043                         "    --rate #              Frame rate (default 30 Hz). 0 means as fast as possible.\n"
00044                         "    --path ""             Path to a directory of images or a video file.\n"
00045                         "    --calibration ""      Calibration file (*.yaml).\n\n");
00046         exit(1);
00047 }
00048 
00049 int main(int argc, char * argv[])
00050 {
00051         ULogger::setType(ULogger::kTypeConsole);
00052         ULogger::setLevel(ULogger::kInfo);
00053 
00054         int device = 0;
00055         std::string path;
00056         float rate = 30.0f;
00057         std::string calibrationFile;
00058         for(int i=1; i<argc; ++i)
00059         {
00060                 if(strcmp(argv[i], "--rate") == 0)
00061                 {
00062                         ++i;
00063                         if(i < argc)
00064                         {
00065                                 rate = uStr2Float(argv[i]);
00066                                 if(rate < 0)
00067                                 {
00068                                         showUsage();
00069                                 }
00070                         }
00071                         else
00072                         {
00073                                 showUsage();
00074                         }
00075                         continue;
00076                 }
00077                 if(strcmp(argv[i], "--device") == 0)
00078                 {
00079                         ++i;
00080                         if(i < argc)
00081                         {
00082                                 device = std::atoi(argv[i]);
00083                                 if(device < 0)
00084                                 {
00085                                         showUsage();
00086                                 }
00087                         }
00088                         else
00089                         {
00090                                 showUsage();
00091                         }
00092                         continue;
00093                 }
00094                 if(strcmp(argv[i], "--path") == 0)
00095                 {
00096                         ++i;
00097                         if(i < argc)
00098                         {
00099                                 path = argv[i];
00100                         }
00101                         else
00102                         {
00103                                 showUsage();
00104                         }
00105                         continue;
00106                 }
00107                 if(strcmp(argv[i], "--calibration") == 0)
00108                 {
00109                         ++i;
00110                         if(i < argc)
00111                         {
00112                                 calibrationFile = argv[i];
00113                         }
00114                         else
00115                         {
00116                                 showUsage();
00117                         }
00118                         continue;
00119                 }
00120 
00121                 printf("Unrecognized option : %s\n", argv[i]);
00122                 showUsage();
00123         }
00124 
00125         if(path.empty())
00126         {
00127                 UINFO("Using device %d", device);
00128         }
00129         else
00130         {
00131                 UINFO("Using path %s", path.c_str());
00132         }
00133 
00134         rtabmap::Camera * camera = 0;
00135         rtabmap::DBReader * dbReader = 0;
00136 
00137         if(!path.empty())
00138         {
00139                 if(UFile::exists(path))
00140                 {
00141                         if(UFile::getExtension(path).compare("db") == 0)
00142                         {
00143                                 dbReader = new rtabmap::DBReader(path, rate);
00144                         }
00145                         else
00146                         {
00147                                 camera = new rtabmap::CameraVideo(path, rate);
00148                         }
00149                 }
00150                 else if(UDirectory::exists(path))
00151                 {
00152                         camera = new rtabmap::CameraImages(path, rate);
00153                 }
00154                 else
00155                 {
00156                         UERROR("Path not valid! \"%s\"", path.c_str());
00157                         return -1;
00158                 }
00159         }
00160         else
00161         {
00162                 camera = new rtabmap::CameraVideo(device, rate);
00163         }
00164 
00165         if(camera)
00166         {
00167                 if(!camera->init())
00168                 {
00169                         delete camera;
00170                         UERROR("Cannot initialize the camera.");
00171                         return -1;
00172                 }
00173 
00174                 if(!calibrationFile.empty())
00175                 {
00176                         UINFO("Set calibration: %s", calibrationFile.c_str());
00177                         camera->setCalibration(calibrationFile);
00178                 }
00179         }
00180 
00181         if(dbReader)
00182         {
00183                 if(!dbReader->init())
00184                 {
00185                         delete dbReader;
00186                         UERROR("Cannot initialize the camera.");
00187                         return -1;
00188                 }
00189         }
00190 
00191         cv::Mat rgb;
00192         rgb = camera?camera->takeImage():dbReader->getNextData().image();
00193         cv::namedWindow("Video", CV_WINDOW_AUTOSIZE); // create window
00194         while(!rgb.empty())
00195         {
00196                 cv::imshow("Video", rgb); // show frame
00197 
00198                 int c = cv::waitKey(10); // wait 10 ms or for key stroke
00199                 if(c == 27)
00200                         break; // if ESC, break and quit
00201 
00202                 rgb = camera?camera->takeImage():dbReader->getNextData().image();
00203         }
00204         cv::destroyWindow("Video");
00205         if(camera)
00206         {
00207                 delete camera;
00208         }
00209         if(dbReader)
00210         {
00211                 delete dbReader;
00212         }
00213         return 0;
00214 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:31