convert_cam_param_file.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002  *
00003  * $Id: file.cpp 3496 2011-11-22 15:14:32Z fnovotny $
00004  *
00005  * This file is part of the ViSP software.
00006  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
00007  *
00008  * This software is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * ("GPL") version 2 as published by the Free Software Foundation.
00011  * See the file LICENSE.txt at the root directory of this source
00012  * distribution for additional information about the GNU GPL.
00013  *
00014  * For using ViSP with software that can not be combined with the GNU
00015  * GPL, please contact INRIA about acquiring a ViSP Professional
00016  * Edition License.
00017  *
00018  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
00019  *
00020  * This software was developed at:
00021  * INRIA Rennes - Bretagne Atlantique
00022  * Campus Universitaire de Beaulieu
00023  * 35042 Rennes Cedex
00024  * France
00025  * http://www.irisa.fr/lagadic
00026  *
00027  * If you have questions regarding the use of this file, please contact
00028  * INRIA at visp@inria.fr
00029  *
00030  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00031  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00032  *
00033  * Contact visp@irisa.fr if any conditions of this licensing are
00034  * not clear to you.
00035  *
00036  * Description:
00037  * command line tool to convert a ViSP camera parameter file to a INI/YAML file compatible with ROS drivers
00038  *
00039  * Authors:
00040  * Riccardo Spica
00041  *
00042  *
00043  *****************************************************************************/
00044 
00051 #include "visp_bridge/camera.h"
00052 
00053 #include <camera_calibration_parsers/parse.h>
00054 #include <visp/vpXmlParserCamera.h>
00055 #include <visp/vpCameraParameters.h>
00056 
00057 #include <boost/program_options/options_description.hpp>
00058 #include <boost/program_options/variables_map.hpp>
00059 #include <boost/program_options/parsers.hpp>
00060 #include <boost/filesystem.hpp>
00061 
00062 #include <iostream>
00063 
00064 namespace po = boost::program_options;
00065 namespace fs = boost::filesystem;
00066 
00067 // Usage : [-int <integer value>] [-float <float value>] [-double <double value>] [-h]
00068 int main(int argc, const char ** argv)
00069 {
00070     // Declare the supported options.
00071     po::options_description desc("Usage examples:\n"
00072             "  convert_cam_param_file -i input_cam_parameters.ini -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
00073             "  convert_cam_param_file -i input_cam_parameters.yml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
00074             "  convert_cam_param_file -i input_cam_parameters.xml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
00075             "  convert_cam_param_file -i input_cam_parameters.xml -o input_cam_parameters.yml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
00076             "Allowed options");
00077     desc.add_options()
00078         ("help", "produce help message")
00079         ("input,i", po::value<std::string>(), "input file path")
00080         ("output,o", po::value<std::string>(), "output file path")
00081         ("camera,c", po::value<std::string>(), "camera name")
00082         ("width,w", po::value<unsigned int>(), "camera width")
00083         ("height,h", po::value<unsigned int>(), "camera height")
00084         ("distortion,d", "Use distortion model")
00085         ("force-deleting,f", "Force deleting output file if this exists")
00086     ;
00087 
00088     po::variables_map vm;
00089     po::store(po::parse_command_line(argc, argv, desc), vm);
00090     po::notify(vm);
00091 
00092     if (vm.count("help")) {
00093         std::cout << desc << std::endl;
00094         return 0;
00095     }
00096 
00097     if (!(vm.count("input") && vm.count("camera") && vm.count("width") && vm.count("height"))) {
00098         std::cout << "Missing options" << std::endl;
00099         std::cout << desc << std::endl;
00100         return 1;
00101     }
00102 
00103     const fs::path inPath = vm["input"].as<std::string>();
00104     fs::path outPath;
00105 
00106     vpXmlParserCamera parser;
00107     vpCameraParameters vispParam;
00108     sensor_msgs::CameraInfo rosParam;
00109     const unsigned int width = vm["width"].as<uint>();
00110     const unsigned int height = vm["height"].as<uint>();
00111 
00112     if (inPath.extension() == std::string(".xml")){
00113 
00114         if (vm.count("output")){
00115             outPath = vm["output"].as<std::string>();
00116         } else {
00117             outPath = inPath;
00118             outPath.replace_extension(fs::path(".ini"));
00119         }
00120 
00121         if (boost::filesystem::exists(outPath)){
00122             if (vm.count("force-deleting")){
00123                 boost::filesystem::remove(outPath);
00124             } else {
00125                 std::cout << "Output file " << outPath.string() << " already exists. Use -f to force deleting"<< std::endl;
00126                 return 1;
00127             }
00128         }
00129 
00130         vpCameraParameters::vpCameraParametersProjType projModel;
00131 
00132         if (vm.count("distortion")){
00133             projModel = vpCameraParameters::perspectiveProjWithDistortion;
00134         } else {
00135             projModel = vpCameraParameters::perspectiveProjWithoutDistortion;
00136         }
00137 
00138         if (parser.parse(vispParam, inPath.string().c_str(), vm["camera"].as<std::string>().c_str(),
00139                 projModel, width, height)!=vpXmlParserCamera::SEQUENCE_OK){
00140             std::cout << "Error parsing visp input file " << inPath.string() << std::endl;
00141             return 1;
00142         }
00143 
00144         rosParam = visp_bridge::toSensorMsgsCameraInfo(vispParam, width, height);
00145 
00146         if(!camera_calibration_parsers::writeCalibration(outPath.string(), vm["camera"].as<std::string>(), rosParam)){
00147             std::cout << "Error writing ros output file " << outPath.string() << std::endl;
00148             return 1;
00149         }
00150 
00151     } else if (inPath.extension() == std::string(".ini") || inPath.extension() == std::string(".yml")){
00152 
00153         if (vm.count("output")){
00154             outPath = vm["output"].as<std::string>();
00155         } else {
00156             outPath = inPath;
00157             outPath.replace_extension(fs::path(".xml"));
00158         }
00159 
00160         if (boost::filesystem::exists(outPath)){
00161             if (vm.count("force-deleting")){
00162                 boost::filesystem::remove(outPath);
00163             } else {
00164                 std::cout << "Output file " << outPath.string() << " already exists. Use -f to force deleting"<< std::endl;
00165                 return 1;
00166             }
00167         }
00168 
00169         std::string cameraName = vm["camera"].as<std::string>();
00170 
00171         if (!camera_calibration_parsers::readCalibration(inPath.string(), cameraName, rosParam)){
00172             std::cout << "Error parsing ros input file " << inPath.string() << std::endl;
00173             return 1;
00174         }
00175 
00176         vispParam = visp_bridge::toVispCameraParameters(rosParam);
00177 
00178         if(parser.save(vispParam, outPath.string().c_str(), cameraName, width, height)!=vpXmlParserCamera::SEQUENCE_OK){
00179             std::cout << "Error writing visp output file " << outPath.string() << std::endl;
00180             return 1;
00181         }
00182 
00183     } else {
00184         std::cout << "Unknown input file format" << std::endl;
00185         return 1;
00186     }
00187 
00188     std::cout << "Successfully created output file: " << outPath << std::endl;
00189 
00190     return 0;
00191 }
00192 


visp_bridge
Author(s): Filip Novotny
autogenerated on Sun Feb 19 2017 03:28:30