Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testUndistortImage.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Test for image undistortion.
32  *
33  * Authors:
34  * Anthony Saunier
35  *
36  *****************************************************************************/
37 
38 
39 
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpRGBa.h>
42 #include <visp3/io/vpImageIo.h>
43 #include <visp3/core/vpImageTools.h>
44 #include <visp3/core/vpIoTools.h>
45 #include <visp3/io/vpParseArgv.h>
46 #include <visp3/core/vpDebug.h>
47 #include <visp3/core/vpTime.h>
48 #include <stdlib.h>
59 // List of allowed command line options
60 #define GETOPTARGS "cdi:o:h"
61 
62 //#define COLOR
63 #define BW
64 
65 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
66 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
67 
68 /*
69 
70  Print the program options.
71 
72  \param name : Program name.
73  \param badparam : Bad parameter name.
74  \param ipath: Input image path.
75  \param opath : Output image path.
76  \param user : Username.
77 
78  */
79 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
80 {
81  fprintf(stdout, "\n\
82 Read an image from the disk, undistort it \n\
83 and save the undistorted image on the disk.\n\
84 (Klimt_undistorted.pgm).\n\
85 \n\
86 SYNOPSIS\n\
87  %s [-i <input image path>] [-o <output image path>]\n\
88  [-h]\n\
89  ", name);
90 
91  fprintf(stdout, "\n\
92 OPTIONS: Default\n\
93  -i <input image path> %s\n\
94  Set image input path.\n\
95  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
96  image.\n\
97  Setting the VISP_INPUT_IMAGE_PATH environment\n\
98  variable produces the same behaviour than using\n\
99  this option.\n\
100  \n\
101  -o <output image path> %s\n\
102  Set image output path.\n\
103  From this directory, creates the \"%s\"\n\
104  subdirectory depending on the username, where \n\
105  Klimt_undistorted.pgm output image is written.\n\
106 \n\
107  -h\n\
108  Print the help.\n\n",
109  ipath.c_str(), opath.c_str(), user.c_str());
110 
111  if (badparam)
112  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
113 }
114 
127 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
128 {
129  const char *optarg_;
130  int c;
131  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
132 
133  switch (c) {
134  case 'i': ipath = optarg_; break;
135  case 'o': opath = optarg_; break;
136  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
137 
138  case 'c':
139  case 'd':
140  break;
141 
142  default:
143  usage(argv[0], optarg_, ipath, opath, user); return false; break;
144  }
145  }
146 
147  if ((c == 1) || (c == -1)) {
148  // standalone param or error
149  usage(argv[0], NULL, ipath, opath, user);
150  std::cerr << "ERROR: " << std::endl;
151  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
152  return false;
153  }
154 
155  return true;
156 }
157 
158 int main(int argc, const char ** argv)
159 {
160  try {
161  std::string env_ipath;
162  std::string opt_ipath;
163  std::string opt_opath;
164  std::string ipath;
165  std::string opath;
166  std::string filename;
167  std::string username;
168 
169  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
170  env_ipath = vpIoTools::getViSPImagesDataPath();
171 
172  // Set the default input path
173  if (! env_ipath.empty())
174  ipath = env_ipath;
175 
176  // Set the default output path
177 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
178  opt_opath = "/tmp";
179 #elif defined(_WIN32)
180  opt_opath = "C:\\temp";
181 #endif
182 
183  // Get the user login name
184  vpIoTools::getUserName(username);
185 
186  // Read the command line options
187  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
188  exit (-1);
189  }
190 
191  // Get the option values
192  if (!opt_ipath.empty())
193  ipath = opt_ipath;
194  if (!opt_opath.empty())
195  opath = opt_opath;
196 
197  // Append to the output path string, the login name of the user
198  opath = vpIoTools::createFilePath(opath, username);
199 
200  // Test if the output path exist. If no try to create it
201  if (vpIoTools::checkDirectory(opath) == false) {
202  try {
203  // Create the dirname
205  }
206  catch (...) {
207  usage(argv[0], NULL, ipath, opt_opath, username);
208  std::cerr << std::endl
209  << "ERROR:" << std::endl;
210  std::cerr << " Cannot create " << opath << std::endl;
211  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
212  exit(-1);
213  }
214  }
215 
216  // Compare ipath and env_ipath. If they differ, we take into account
217  // the input path comming from the command line option
218  if (opt_ipath.empty()) {
219  if (ipath != env_ipath) {
220  std::cout << std::endl
221  << "WARNING: " << std::endl;
222  std::cout << " Since -i <visp image path=" << ipath << "> "
223  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
224  << " we skip the environment variable." << std::endl;
225  }
226  }
227 
228  // Test if an input path is set
229  if (opt_ipath.empty() && env_ipath.empty()){
230  usage(argv[0], NULL, ipath, opt_opath, username);
231  std::cerr << std::endl
232  << "ERROR:" << std::endl;
233  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
234  << std::endl
235  << " environment variable to specify the location of the " << std::endl
236  << " image path where test images are located." << std::endl << std::endl;
237  exit(-1);
238  }
239 
240  //
241  // Here starts really the test
242  //
243 #if defined BW
244  vpImage<unsigned char> I; // Input image
245  vpImage<unsigned char> U; // undistorted output image
246 #elif defined COLOR
247  vpImage<vpRGBa> I; // Input image
248  vpImage<vpRGBa> U; // undistorted output image
249 #endif
250  vpCameraParameters cam;
251  cam.initPersProjWithDistortion(600,600,192,144,-0.17,0.17);
252  // Read the input grey image from the disk
253 #if defined BW
254  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm") ;
255 #elif defined COLOR
256  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
257 #endif
258  std::cout << "Read image: " << filename << std::endl;
259  vpImageIo::read(I, filename) ;
260 
261  std::cout << "Undistortion in process... " << std::endl;
262  vpImageTools::undistort(I, cam, U);
263 
264  double begintime = vpTime::measureTimeMs();
265 
266  // For the test, to have a significant time measure we repeat the
267  // undistortion 100 times
268  for(unsigned int i=0;i<100;i++)
269  // Create the undistorted image
270  vpImageTools::undistort(I, cam, U);
271 
272  double endtime = vpTime::measureTimeMs();
273 
274  std::cout<<"Time for 100 undistortion (ms): "<< endtime - begintime
275  << std::endl;
276 
277  // Write the undistorted image on the disk
278 #if defined BW
279  filename = vpIoTools::path( vpIoTools::createFilePath(opath, "Klimt_undistorted.pgm") );
280 #elif defined COLOR
281  filename = vpIoTools::path( vpIoTools::createFilePath(opath, "Klimt_undistorted.ppm") );
282 #endif
283  std::cout << "Write undistorted image: " << filename << std::endl;
284  vpImageIo::write(U, filename) ;
285  return 0;
286  }
287  catch(vpException &e) {
288  std::cout << "Catch an exception: " << e << std::endl;
289  return 1;
290  }
291 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
error that can be emited by ViSP classes.
Definition: vpException.h:73
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:770
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:177
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI)
Definition: vpImageTools.h:609
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)