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