ViSP  2.10.0
testUndistortImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testUndistortImage.cpp 4814 2014-07-31 11:38:39Z 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-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 void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:695
static double measureTimeMs()
Definition: vpTime.cpp:86
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
Generic class defining intrinsic camera parameters.
static std::string getUserName()
Definition: vpIoTools.cpp:141
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)