ViSP  2.9.0
testUndistortImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testUndistortImage.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  *
34  * Description:
35  * Test for image undistortion.
36  *
37  * Authors:
38  * Anthony Saunier
39  *
40  *****************************************************************************/
41 
42 
43 
44 #include <visp/vpImage.h>
45 #include <visp/vpRGBa.h>
46 #include <visp/vpImageIo.h>
47 #include <visp/vpImageTools.h>
48 #include <visp/vpIoTools.h>
49 #include <visp/vpParseArgv.h>
50 #include <visp/vpDebug.h>
51 #include <visp/vpTime.h>
52 #include <stdlib.h>
63 // List of allowed command line options
64 #define GETOPTARGS "i:o:h"
65 
66 //#define COLOR
67 #define BW
68 
69 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
70 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
71 
72 /*
73 
74  Print the program options.
75 
76  \param name : Program name.
77  \param badparam : Bad parameter name.
78  \param ipath: Input image path.
79  \param opath : Output image path.
80  \param user : Username.
81 
82  */
83 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
84 {
85  fprintf(stdout, "\n\
86 Read an image from the disk, undistort it \n\
87 and save the undistorted image on the disk.\n\
88 (Klimt_undistorted.pgm).\n\
89 \n\
90 SYNOPSIS\n\
91  %s [-i <input image path>] [-o <output image path>]\n\
92  [-h]\n\
93  ", name);
94 
95  fprintf(stdout, "\n\
96 OPTIONS: Default\n\
97  -i <input image path> %s\n\
98  Set image input path.\n\
99  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
100  image.\n\
101  Setting the VISP_INPUT_IMAGE_PATH environment\n\
102  variable produces the same behaviour than using\n\
103  this option.\n\
104  \n\
105  -o <output image path> %s\n\
106  Set image output path.\n\
107  From this directory, creates the \"%s\"\n\
108  subdirectory depending on the username, where \n\
109  Klimt_undistorted.pgm output image is written.\n\
110 \n\
111  -h\n\
112  Print the help.\n\n",
113  ipath.c_str(), opath.c_str(), user.c_str());
114 
115  if (badparam)
116  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
117 }
118 
131 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
132 {
133  const char *optarg_;
134  int c;
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136 
137  switch (c) {
138  case 'i': ipath = optarg_; break;
139  case 'o': opath = optarg_; break;
140  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; 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_IMAGE_PATH environment variable value
170  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
171  if (ptenv != NULL)
172  env_ipath = ptenv;
173 
174  // Set the default input path
175  if (! env_ipath.empty())
176  ipath = env_ipath;
177 
178  // Set the default output path
179 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
180  opt_opath = "/tmp";
181 #elif defined(_WIN32)
182  opt_opath = "C:\\temp";
183 #endif
184 
185  // Get the user login name
186  vpIoTools::getUserName(username);
187 
188  // Read the command line options
189  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
190  exit (-1);
191  }
192 
193  // Get the option values
194  if (!opt_ipath.empty())
195  ipath = opt_ipath;
196  if (!opt_opath.empty())
197  opath = opt_opath;
198 
199  // Append to the output path string, the login name of the user
200  opath += vpIoTools::path("/") + username;
201 
202  // Test if the output path exist. If no try to create it
203  if (vpIoTools::checkDirectory(opath) == false) {
204  try {
205  // Create the dirname
207  }
208  catch (...) {
209  usage(argv[0], NULL, ipath, opt_opath, username);
210  std::cerr << std::endl
211  << "ERROR:" << std::endl;
212  std::cerr << " Cannot create " << opath << std::endl;
213  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
214  exit(-1);
215  }
216  }
217 
218  // Compare ipath and env_ipath. If they differ, we take into account
219  // the input path comming from the command line option
220  if (opt_ipath.empty()) {
221  if (ipath != env_ipath) {
222  std::cout << std::endl
223  << "WARNING: " << std::endl;
224  std::cout << " Since -i <visp image path=" << ipath << "> "
225  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
226  << " we skip the environment variable." << std::endl;
227  }
228  }
229 
230  // Test if an input path is set
231  if (opt_ipath.empty() && env_ipath.empty()){
232  usage(argv[0], NULL, ipath, opt_opath, username);
233  std::cerr << std::endl
234  << "ERROR:" << std::endl;
235  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
236  << std::endl
237  << " environment variable to specify the location of the " << std::endl
238  << " image path where test images are located." << std::endl << 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 = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
257 #elif defined COLOR
258  filename = ipath + vpIoTools::path("/ViSP-images/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
277  << std::endl;
278 
279  // Write the undistorted image on the disk
280 #if defined BW
281  filename = opath + vpIoTools::path("/Klimt_undistorted.pgm");
282 #elif defined COLOR
283  filename = opath + vpIoTools::path("/Klimt_undistorted.ppm");
284 #endif
285  std::cout << "Write undistorted image: " << filename << std::endl;
286  vpImageIo::write(U, filename) ;
287  return 0;
288  }
289  catch(vpException e) {
290  std::cout << "Catch an exception: " << e << std::endl;
291  return 1;
292  }
293 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static double measureTimeMs()
Definition: vpTime.cpp:86
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:140
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI)
Definition: vpImageTools.h:362
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
void initPersProjWithDistortion(const double px, const double py, const double u0, const double v0, const double kud, const double kdu)