convert_cam_param_file.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * $Id: file.cpp 3496 2011-11-22 15:14:32Z fnovotny $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Contact visp@irisa.fr if any conditions of this licensing are
34  * not clear to you.
35  *
36  * Description:
37  * command line tool to convert a ViSP camera parameter file to a INI/YAML file compatible with ROS drivers
38  *
39  * Authors:
40  * Riccardo Spica
41  *
42  *
43  *****************************************************************************/
44 
51 #include "visp_bridge/camera.h"
52 
54 #include <visp/vpXmlParserCamera.h>
55 #include <visp/vpCameraParameters.h>
56 
57 #include <boost/program_options/options_description.hpp>
58 #include <boost/program_options/variables_map.hpp>
59 #include <boost/program_options/parsers.hpp>
60 #include <boost/filesystem.hpp>
61 
62 #include <iostream>
63 
64 namespace po = boost::program_options;
65 namespace fs = boost::filesystem;
66 
67 // Usage : [-int <integer value>] [-float <float value>] [-double <double value>] [-h]
68 int main(int argc, const char ** argv)
69 {
70  // Declare the supported options.
71  po::options_description desc("Usage examples:\n"
72  " convert_cam_param_file -i input_cam_parameters.ini -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
73  " convert_cam_param_file -i input_cam_parameters.yml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
74  " convert_cam_param_file -i input_cam_parameters.xml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
75  " convert_cam_param_file -i input_cam_parameters.xml -o input_cam_parameters.yml -c Dragonfly2-8mm-ccmop -w 640 -h 480\n"
76  "Allowed options");
77  desc.add_options()
78  ("help", "produce help message")
79  ("input,i", po::value<std::string>(), "input file path")
80  ("output,o", po::value<std::string>(), "output file path")
81  ("camera,c", po::value<std::string>(), "camera name")
82  ("width,w", po::value<unsigned int>(), "camera width")
83  ("height,h", po::value<unsigned int>(), "camera height")
84  ("distortion,d", "Use distortion model")
85  ("force-deleting,f", "Force deleting output file if this exists")
86  ;
87 
88  po::variables_map vm;
89  po::store(po::parse_command_line(argc, argv, desc), vm);
90  po::notify(vm);
91 
92  if (vm.count("help")) {
93  std::cout << desc << std::endl;
94  return 0;
95  }
96 
97  if (!(vm.count("input") && vm.count("camera") && vm.count("width") && vm.count("height"))) {
98  std::cout << "Missing options" << std::endl;
99  std::cout << desc << std::endl;
100  return 1;
101  }
102 
103  const fs::path inPath = vm["input"].as<std::string>();
104  fs::path outPath;
105 
106  vpXmlParserCamera parser;
107  vpCameraParameters vispParam;
108  sensor_msgs::CameraInfo rosParam;
109  const unsigned int width = vm["width"].as<uint>();
110  const unsigned int height = vm["height"].as<uint>();
111 
112  if (inPath.extension() == std::string(".xml")){
113 
114  if (vm.count("output")){
115  outPath = vm["output"].as<std::string>();
116  } else {
117  outPath = inPath;
118  outPath.replace_extension(fs::path(".ini"));
119  }
120 
121  if (boost::filesystem::exists(outPath)){
122  if (vm.count("force-deleting")){
123  boost::filesystem::remove(outPath);
124  } else {
125  std::cout << "Output file " << outPath.string() << " already exists. Use -f to force deleting"<< std::endl;
126  return 1;
127  }
128  }
129 
130  vpCameraParameters::vpCameraParametersProjType projModel;
131 
132  if (vm.count("distortion")){
133  projModel = vpCameraParameters::perspectiveProjWithDistortion;
134  } else {
135  projModel = vpCameraParameters::perspectiveProjWithoutDistortion;
136  }
137 
138  if (parser.parse(vispParam, inPath.string().c_str(), vm["camera"].as<std::string>().c_str(),
139  projModel, width, height)!=vpXmlParserCamera::SEQUENCE_OK){
140  std::cout << "Error parsing visp input file " << inPath.string() << std::endl;
141  return 1;
142  }
143 
144  rosParam = visp_bridge::toSensorMsgsCameraInfo(vispParam, width, height);
145 
146  if(!camera_calibration_parsers::writeCalibration(outPath.string(), vm["camera"].as<std::string>(), rosParam)){
147  std::cout << "Error writing ros output file " << outPath.string() << std::endl;
148  return 1;
149  }
150 
151  } else if (inPath.extension() == std::string(".ini") || inPath.extension() == std::string(".yml")){
152 
153  if (vm.count("output")){
154  outPath = vm["output"].as<std::string>();
155  } else {
156  outPath = inPath;
157  outPath.replace_extension(fs::path(".xml"));
158  }
159 
160  if (boost::filesystem::exists(outPath)){
161  if (vm.count("force-deleting")){
162  boost::filesystem::remove(outPath);
163  } else {
164  std::cout << "Output file " << outPath.string() << " already exists. Use -f to force deleting"<< std::endl;
165  return 1;
166  }
167  }
168 
169  std::string cameraName = vm["camera"].as<std::string>();
170 
171  if (!camera_calibration_parsers::readCalibration(inPath.string(), cameraName, rosParam)){
172  std::cout << "Error parsing ros input file " << inPath.string() << std::endl;
173  return 1;
174  }
175 
176  vispParam = visp_bridge::toVispCameraParameters(rosParam);
177 
178  if(parser.save(vispParam, outPath.string().c_str(), cameraName, width, height)!=vpXmlParserCamera::SEQUENCE_OK){
179  std::cout << "Error writing visp output file " << outPath.string() << std::endl;
180  return 1;
181  }
182 
183  } else {
184  std::cout << "Unknown input file format" << std::endl;
185  return 1;
186  }
187 
188  std::cout << "Successfully created output file: " << outPath << std::endl;
189 
190  return 0;
191 }
192 
def readCalibration(file_name)
sensor_msgs::CameraInfo toSensorMsgsCameraInfo(vpCameraParameters &cam_info, unsigned int cam_image_width, unsigned int cam_image_height)
Converts ViSP camera parameters (vpCameraParameters) to sensor_msgs::CameraInfo.
Definition: camera.cpp:94
vpCameraParameters toVispCameraParameters(const sensor_msgs::CameraInfo &cam_info)
Converts a sensor_msgs::CameraInfo to ViSP camera parameters (vpCameraParameters).
Definition: camera.cpp:56
conversions between ROS and ViSP structures representing camera parameters
bool writeCalibration(const std::string &file_name, const std::string &camera_name, const sensor_msgs::CameraInfo &cam_info)
int main(int argc, const char **argv)


visp_bridge
Author(s): Filip Novotny
autogenerated on Sat Mar 13 2021 03:20:01