main.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010-2016, 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/CameraRGB.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 <opencv2/highgui/highgui_c.h>
00036 #include <stdio.h>
00037 
00038 void showUsage()
00039 {
00040         printf("\nUsage:\n"
00041                         "rtabmap-camera [option] \n"
00042                         " Options:\n"
00043                         "    --device #            USB camera device id (default 0).\n"
00044                         "    --rate #              Frame rate (default 0 Hz). 0 means as fast as possible.\n"
00045                         "    --path ""             Path to a directory of images or a video file.\n"
00046                         "    --calibration ""      Calibration file (*.yaml).\n\n");
00047         exit(1);
00048 }
00049 
00050 int main(int argc, char * argv[])
00051 {
00052         ULogger::setType(ULogger::kTypeConsole);
00053         ULogger::setLevel(ULogger::kInfo);
00054 
00055         int device = 0;
00056         std::string path;
00057         float rate = 0.0f;
00058         std::string calibrationFile;
00059         for(int i=1; i<argc; ++i)
00060         {
00061                 if(strcmp(argv[i], "--rate") == 0)
00062                 {
00063                         ++i;
00064                         if(i < argc)
00065                         {
00066                                 rate = uStr2Float(argv[i]);
00067                                 if(rate < 0)
00068                                 {
00069                                         showUsage();
00070                                 }
00071                         }
00072                         else
00073                         {
00074                                 showUsage();
00075                         }
00076                         continue;
00077                 }
00078                 if(strcmp(argv[i], "--device") == 0)
00079                 {
00080                         ++i;
00081                         if(i < argc)
00082                         {
00083                                 device = std::atoi(argv[i]);
00084                                 if(device < 0)
00085                                 {
00086                                         showUsage();
00087                                 }
00088                         }
00089                         else
00090                         {
00091                                 showUsage();
00092                         }
00093                         continue;
00094                 }
00095                 if(strcmp(argv[i], "--path") == 0)
00096                 {
00097                         ++i;
00098                         if(i < argc)
00099                         {
00100                                 path = argv[i];
00101                         }
00102                         else
00103                         {
00104                                 showUsage();
00105                         }
00106                         continue;
00107                 }
00108                 if(strcmp(argv[i], "--calibration") == 0)
00109                 {
00110                         ++i;
00111                         if(i < argc)
00112                         {
00113                                 calibrationFile = argv[i];
00114                         }
00115                         else
00116                         {
00117                                 showUsage();
00118                         }
00119                         continue;
00120                 }
00121 
00122                 printf("Unrecognized option : %s\n", argv[i]);
00123                 showUsage();
00124         }
00125 
00126         if(path.empty())
00127         {
00128                 UINFO("Using device %d", device);
00129         }
00130         else
00131         {
00132                 UINFO("Using path %s", path.c_str());
00133         }
00134 
00135         rtabmap::Camera * camera = 0;
00136 
00137         if(!path.empty())
00138         {
00139                 if(UFile::exists(path))
00140                 {
00141                         if(UFile::getExtension(path).compare("db") == 0)
00142                         {
00143                                 camera = new rtabmap::DBReader(path, rate);
00144                         }
00145                         else
00146                         {
00147                                 camera = new rtabmap::CameraVideo(path, false, 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, false, rate);
00163         }
00164 
00165         if(camera)
00166         {
00167                 if(!calibrationFile.empty())
00168                 {
00169                         UINFO("Set calibration: %s", calibrationFile.c_str());
00170                 }
00171                 if(!camera->init(UDirectory::getDir(calibrationFile), UFile::getName(calibrationFile)))
00172                 {
00173                         delete camera;
00174                         UERROR("Cannot initialize the camera.");
00175                         return -1;
00176                 }
00177         }
00178 
00179         cv::Mat rgb;
00180         rgb = camera->takeImage().imageRaw();
00181         cv::namedWindow("Video", CV_WINDOW_AUTOSIZE); // create window
00182         while(!rgb.empty())
00183         {
00184                 cv::imshow("Video", rgb); // show frame
00185 
00186                 int c = cv::waitKey(10); // wait 10 ms or for key stroke
00187                 if(c == 27)
00188                         break; // if ESC, break and quit
00189 
00190                 rgb = camera->takeImage().imageRaw();
00191         }
00192         cv::destroyWindow("Video");
00193         if(camera)
00194         {
00195                 delete camera;
00196         }
00197         return 0;
00198 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:20