ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testUndistortImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testUndistortImage.cpp 4323 2013-07-18 09:24:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 /*
70 
71  Print the program options.
72 
73  \param name : Program name.
74  \param badparam : Bad parameter name.
75  \param ipath: Input image path.
76  \param opath : Output image path.
77  \param user : Username.
78 
79  */
80 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
81 {
82  fprintf(stdout, "\n\
83 Read an image from the disk, undistort it \n\
84 and save the undistorted image on the disk.\n\
85 (Klimt_undistorted.pgm).\n\
86 \n\
87 SYNOPSIS\n\
88  %s [-i <input image path>] [-o <output image path>]\n\
89  [-h]\n\
90  ", name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -i <input image path> %s\n\
95  Set image input path.\n\
96  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
97  image.\n\
98  Setting the VISP_INPUT_IMAGE_PATH environment\n\
99  variable produces the same behaviour than using\n\
100  this option.\n\
101  \n\
102  -o <output image path> %s\n\
103  Set image output path.\n\
104  From this directory, creates the \"%s\"\n\
105  subdirectory depending on the username, where \n\
106  Klimt_undistorted.pgm output image is written.\n\
107 \n\
108  -h\n\
109  Print the help.\n\n",
110  ipath.c_str(), opath.c_str(), user.c_str());
111 
112  if (badparam)
113  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
114 }
115 
128 bool getOptions(int argc, const char **argv,
129  std::string &ipath, std::string &opath, std::string user)
130 {
131  const char *optarg;
132  int c;
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
134 
135  switch (c) {
136  case 'i': ipath = optarg; break;
137  case 'o': opath = optarg; break;
138  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
139 
140  default:
141  usage(argv[0], optarg, ipath, opath, user); return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL, ipath, opath, user);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 int main(int argc, const char ** argv)
157 {
158  std::string env_ipath;
159  std::string opt_ipath;
160  std::string opt_opath;
161  std::string ipath;
162  std::string opath;
163  std::string filename;
164  std::string username;
165 
166  // Get the VISP_IMAGE_PATH environment variable value
167  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
168  if (ptenv != NULL)
169  env_ipath = ptenv;
170 
171  // Set the default input path
172  if (! env_ipath.empty())
173  ipath = env_ipath;
174 
175  // Set the default output path
176 #ifdef UNIX
177  opt_opath = "/tmp";
178 #elif WIN32
179  opt_opath = "C:\\temp";
180 #endif
181 
182  // Get the user login name
183  vpIoTools::getUserName(username);
184 
185  // Read the command line options
186  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
187  exit (-1);
188  }
189 
190  // Get the option values
191  if (!opt_ipath.empty())
192  ipath = opt_ipath;
193  if (!opt_opath.empty())
194  opath = opt_opath;
195 
196  // Append to the output path string, the login name of the user
197  opath += vpIoTools::path("/") + username;
198 
199  // Test if the output path exist. If no try to create it
200  if (vpIoTools::checkDirectory(opath) == false) {
201  try {
202  // Create the dirname
204  }
205  catch (...) {
206  usage(argv[0], NULL, ipath, opt_opath, username);
207  std::cerr << std::endl
208  << "ERROR:" << std::endl;
209  std::cerr << " Cannot create " << opath << std::endl;
210  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
211  exit(-1);
212  }
213  }
214 
215  // Compare ipath and env_ipath. If they differ, we take into account
216  // the input path comming from the command line option
217  if (opt_ipath.empty()) {
218  if (ipath != env_ipath) {
219  std::cout << std::endl
220  << "WARNING: " << std::endl;
221  std::cout << " Since -i <visp image path=" << ipath << "> "
222  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
223  << " we skip the environment variable." << std::endl;
224  }
225  }
226 
227  // Test if an input path is set
228  if (opt_ipath.empty() && env_ipath.empty()){
229  usage(argv[0], NULL, ipath, opt_opath, username);
230  std::cerr << std::endl
231  << "ERROR:" << std::endl;
232  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
233  << std::endl
234  << " environment variable to specify the location of the " << std::endl
235  << " image path where test images are located." << std::endl << std::endl;
236  exit(-1);
237  }
238 
239  //
240  // Here starts really the test
241  //
242 #if defined BW
243  vpImage<unsigned char> I; // Input image
244  vpImage<unsigned char> U; // undistorted output image
245 #elif defined COLOR
246  vpImage<vpRGBa> I; // Input image
247  vpImage<vpRGBa> U; // undistorted output image
248 #endif
249  vpCameraParameters cam;
250  cam.initPersProjWithDistortion(600,600,192,144,-0.17,0.17);
251  // Read the input grey image from the disk
252 #if defined BW
253  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
254 #elif defined COLOR
255  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
256 #endif
257  std::cout << "Read image: " << filename << std::endl;
258  vpImageIo::read(I, filename) ;
259 
260  std::cout << "Undistortion in process... " << std::endl;
261  vpImageTools::undistort(I, cam, U);
262 
263  double begintime = vpTime::measureTimeMs();
264 
265  // For the test, to have a significant time measure we repeat the
266  // undistortion 100 times
267  for(unsigned int i=0;i<100;i++)
268  // Create the undistorted image
269  vpImageTools::undistort(I, cam, U);
270 
271  double endtime = vpTime::measureTimeMs();
272 
273  std::cout<<"Time for 100 undistortion (ms): "<< endtime - begintime
274  << std::endl;
275 
276  // Write the undistorted image on the disk
277 #if defined BW
278  filename = opath + vpIoTools::path("/Klimt_undistorted.pgm");
279 #elif defined COLOR
280  filename = opath + vpIoTools::path("/Klimt_undistorted.ppm");
281 #endif
282  std::cout << "Write undistorted image: " << filename << std::endl;
283  vpImageIo::write(U, filename) ;
284 }