Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testUndistortImage.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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 
34 #include <stdlib.h>
35 #include <visp3/core/vpDebug.h>
36 #include <visp3/core/vpImage.h>
37 #include <visp3/core/vpImageTools.h>
38 #include <visp3/core/vpIoTools.h>
39 #include <visp3/core/vpRGBa.h>
40 #include <visp3/core/vpTime.h>
41 #include <visp3/io/vpImageIo.h>
42 #include <visp3/io/vpParseArgv.h>
43 
52 // List of allowed command line options
53 #define GETOPTARGS "cdi:o:t:s:h"
54 
55 #ifdef ENABLE_VISP_NAMESPACE
56 using namespace VISP_NAMESPACE_NAME;
57 #endif
58 
59 /*
60  Print the program options.
61 
62  \param name : Program name.
63  \param badparam : Bad parameter name.
64  \param ipath: Input image path.
65  \param opath : Output image path.
66  \param user : Username.
67  */
68 void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
69  const std::string &user)
70 {
71 #if VISP_HAVE_DATASET_VERSION >= 0x030600
72  std::string ext("png");
73 #else
74  std::string ext("pgm");
75 #endif
76  fprintf(stdout, "\n\
77 Read an image from the disk, undistort it \n\
78 and save the undistorted image on the disk.\n\
79 (grid36-01_undistorted.pgm).\n\
80 \n\
81 SYNOPSIS\n\
82  %s [-i <input image path>] [-o <output image path>] [-t <nThreads>] [-s <scale>]\n\
83  [-h]\n\
84  ",
85  name);
86 
87  fprintf(stdout, "\n\
88 OPTIONS: Default\n\
89  -i <input image path> %s\n\
90  Set image input path.\n\
91  From this path read \"calibration/grid36-01.%s\"\n\
92  image.\n\
93  Setting the VISP_INPUT_IMAGE_PATH environment\n\
94  variable produces the same behaviour than using\n\
95  this option.\n\
96  \n\
97  -o <output image path> %s\n\
98  Set image output path.\n\
99  From this directory, creates the \"%s\"\n\
100  subdirectory depending on the username, where \n\
101  grid36-01_undistorted.pgm output image is written.\n\
102 \n\
103  -t <nThreads> \n\
104  Set the number of threads to use for vpImageTools::undistort().\n\
105 \n\
106  -s <scale> \n\
107  Resize the image by the specified scale factor.\n\
108 \n\
109  -h\n\
110  Print the help.\n\n",
111  ext.c_str(), ipath.c_str(), opath.c_str(), user.c_str());
112 
113  if (badparam)
114  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
115 }
116 
129 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
130  unsigned int &nThreads, unsigned int &scale)
131 {
132  const char *optarg_;
133  int c;
134  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
135 
136  switch (c) {
137  case 'i':
138  ipath = optarg_;
139  break;
140  case 'o':
141  opath = optarg_;
142  break;
143  case 't':
144  nThreads = atoi(optarg_);
145  break;
146  case 's':
147  scale = atoi(optarg_);
148  break;
149  case 'h':
150  usage(argv[0], nullptr, ipath, opath, user);
151  return false;
152 
153  case 'c':
154  case 'd':
155  break;
156 
157  default:
158  usage(argv[0], optarg_, ipath, opath, user);
159  return false;
160  }
161  }
162 
163  if ((c == 1) || (c == -1)) {
164  // standalone param or error
165  usage(argv[0], nullptr, ipath, opath, user);
166  std::cerr << "ERROR: " << std::endl;
167  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
168  return false;
169  }
170 
171  return true;
172 }
173 
174 int main(int argc, const char **argv)
175 {
176  try {
177  std::string env_ipath;
178  std::string opt_ipath;
179  std::string opt_opath;
180  std::string ipath;
181  std::string opath;
182  std::string filename;
183  std::string username;
184  unsigned int nThreads = 2;
185  unsigned int scale = 1;
186 
187 #if VISP_HAVE_DATASET_VERSION >= 0x030600
188  std::string ext("png");
189 #else
190  std::string ext("pgm");
191 #endif
192 
193  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
194  // environment variable value
195  env_ipath = vpIoTools::getViSPImagesDataPath();
196 
197  // Set the default input path
198  if (!env_ipath.empty())
199  ipath = env_ipath;
200 
201  // Set the default output path
202 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
203  opt_opath = "/tmp";
204 #elif defined(_WIN32)
205  opt_opath = "C:\\temp";
206 #endif
207 
208  // Get the user login name
209  vpIoTools::getUserName(username);
210 
211  // Read the command line options
212  if (getOptions(argc, argv, opt_ipath, opt_opath, username, nThreads, scale) == false) {
213  return EXIT_FAILURE;
214  }
215 
216  // Get the option values
217  if (!opt_ipath.empty())
218  ipath = opt_ipath;
219  if (!opt_opath.empty())
220  opath = opt_opath;
221 
222  // Append to the output path string, the login name of the user
223  opath = vpIoTools::createFilePath(opath, username);
224 
225  // Test if the output path exist. If no try to create it
226  if (vpIoTools::checkDirectory(opath) == false) {
227  try {
228  // Create the dirname
230  }
231  catch (...) {
232  usage(argv[0], nullptr, ipath, opt_opath, username);
233  std::cerr << std::endl << "ERROR:" << std::endl;
234  std::cerr << " Cannot create " << opath << std::endl;
235  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
236  return EXIT_FAILURE;
237  }
238  }
239 
240  // Compare ipath and env_ipath. If they differ, we take into account
241  // the input path coming from the command line option
242  if (opt_ipath.empty()) {
243  if (ipath != env_ipath) {
244  std::cout << std::endl << "WARNING: " << std::endl;
245  std::cout << " Since -i <visp image path=" << ipath << "> "
246  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
247  << " we skip the environment variable." << std::endl;
248  }
249  }
250 
251  // Test if an input path is set
252  if (opt_ipath.empty() && env_ipath.empty()) {
253  usage(argv[0], nullptr, ipath, opt_opath, username);
254  std::cerr << std::endl << "ERROR:" << std::endl;
255  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
256  << " environment variable to specify the location of the " << std::endl
257  << " image path where test images are located." << std::endl
258  << std::endl;
259  return EXIT_FAILURE;
260  }
261 
262  //
263  // Here starts really the test
264  //
265  vpImage<vpRGBa> I, I_; // Input image
266  vpImage<unsigned char> I_gray;
267  vpImage<vpRGBa> U; // undistorted output image
268  vpImage<unsigned char> U_gray; // undistorted output image
269  vpImage<vpRGBa> U_remap; // undistorted output image
270  vpImage<unsigned char> U_remap_gray; // undistorted output image
271 
272  vpCameraParameters cam;
273  cam.initPersProjWithDistortion(600, 600, 320, 240, -0.17, 0.17);
274  // Read the input grey image from the disk
275  filename = vpIoTools::createFilePath(ipath, "calibration/grid36-01." + ext);
276  std::cout << "Read image: " << filename << std::endl;
277  vpImageIo::read(I_, filename);
278  if (scale > 1) {
279  std::cout << "Scale the image by a factor of " << scale << std::endl;
280  vpImageTools::resize(I_, I, I_.getWidth() * scale, I_.getHeight() * scale);
281  }
282  else {
283  I = I_;
284  }
285  std::cout << "Input image: " << I.getWidth() << "x" << I.getHeight() << std::endl;
286  vpImageConvert::convert(I, I_gray);
287 
288  std::cout << "Nb threads to use for vpImageTools::undistort(): " << nThreads << std::endl;
289 
290  double t_undistort = 0.0, t_remap = 0.0;
291  {
292  std::cout << "\nUndistortion in process (color image)... " << std::endl;
293  vpImageTools::undistort(I, cam, U, nThreads);
294 
295  double begintime = vpTime::measureTimeMs();
296 
297  // For the test, to have a significant time measure we repeat the
298  // undistortion 10 times
299  for (unsigned int i = 0; i < 10; i++)
300  // Create the undistorted image
301  vpImageTools::undistort(I, cam, U, nThreads);
302 
303  double endtime = vpTime::measureTimeMs();
304  t_undistort = endtime - begintime;
305 
306  std::cout << "Time for 10 color image undistortion (ms): " << t_undistort << std::endl;
307  }
308 
309  {
310  std::cout << "Undistortion in process with remap (color image)... " << std::endl;
311 
312  double begintime = vpTime::measureTimeMs();
313 
314  // For the test, to have a significant time measure we repeat the
315  // undistortion 10 times
316  vpArray2D<int> mapU, mapV;
317  vpArray2D<float> mapDu, mapDv;
318  for (unsigned int i = 0; i < 10; i++) {
319  if (i == 0) {
320  vpImageTools::initUndistortMap(cam, I.getWidth(), I.getHeight(), mapU, mapV, mapDu, mapDv);
321  }
322  vpImageTools::remap(I, mapU, mapV, mapDu, mapDv, U_remap);
323  }
324 
325  double endtime = vpTime::measureTimeMs();
326  t_remap = endtime - begintime;
327 
328  std::cout << "Time for 10 color image undistortion with remap (ms): " << t_remap << std::endl;
329  std::cout << "Speed-up: " << t_undistort / t_remap << "X" << std::endl;
330  }
331 
332  {
333  std::cout << "\nUndistortion in process (gray image)... " << std::endl;
334  vpImageTools::undistort(I_gray, cam, U_gray, nThreads);
335 
336  double begintime = vpTime::measureTimeMs();
337 
338  // For the test, to have a significant time measure we repeat the
339  // undistortion 100 times
340  for (unsigned int i = 0; i < 100; i++)
341  // Create the undistorted image
342  vpImageTools::undistort(I_gray, cam, U_gray, nThreads);
343 
344  double endtime = vpTime::measureTimeMs();
345  t_undistort = endtime - begintime;
346 
347  std::cout << "Time for 100 gray image undistortion (ms): " << t_undistort << std::endl;
348  }
349 
350  {
351  std::cout << "Undistortion in process with remap (gray image)... " << std::endl;
352 
353  double begintime = vpTime::measureTimeMs();
354 
355  // For the test, to have a significant time measure we repeat the
356  // undistortion 100 times
357  vpArray2D<int> mapU, mapV;
358  vpArray2D<float> mapDu, mapDv;
359  for (unsigned int i = 0; i < 10; i++) {
360  if (i == 0) {
361  vpImageTools::initUndistortMap(cam, I.getWidth(), I.getHeight(), mapU, mapV, mapDu, mapDv);
362  }
363  vpImageTools::remap(I_gray, mapU, mapV, mapDu, mapDv, U_remap_gray);
364  }
365 
366  double endtime = vpTime::measureTimeMs();
367  t_remap = endtime - begintime;
368 
369  std::cout << "Time for 100 gray image undistortion with remap (ms): " << t_remap << std::endl;
370  std::cout << "Speed-up: " << t_undistort / t_remap << "X" << std::endl;
371  }
372 
373  // Write the undistorted images on the disk
374  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_color.png"));
375  std::cout << "\nWrite undistorted image: " << filename << std::endl;
376  vpImageIo::write(U, filename);
377 
378  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_gray.png"));
379  std::cout << "Write undistorted image: " << filename << std::endl;
380  vpImageIo::write(U_gray, filename);
381 
382  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_remap_color.png"));
383  std::cout << "\nWrite undistorted image with remap: " << filename << std::endl;
384  vpImageIo::write(U_remap, filename);
385 
386  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_remap_gray.png"));
387  std::cout << "Write undistorted image with remap: " << filename << std::endl;
388  vpImageIo::write(U_remap_gray, filename);
389 
390  // Compute images difference
391  vpImage<vpRGBa> U_diff_abs;
392  vpImageTools::imageDifferenceAbsolute(U, U_remap, U_diff_abs);
393  double mean_diff = 0.0;
394  for (unsigned int i = 0; i < U_diff_abs.getHeight(); i++) {
395  for (unsigned int j = 0; j < U_diff_abs.getWidth(); j++) {
396  mean_diff += U_diff_abs[i][j].R;
397  mean_diff += U_diff_abs[i][j].G;
398  mean_diff += U_diff_abs[i][j].B;
399  mean_diff += U_diff_abs[i][j].A;
400  }
401  }
402  double remap_mean_error = mean_diff / (4 * U_diff_abs.getSize());
403  std::cout << "U_diff_abs mean value: " << remap_mean_error << std::endl;
404  const double remap_error_threshold = 0.5;
405  if (remap_mean_error > remap_error_threshold) {
406  std::cerr << "Issue with vpImageTools::remap() with vpRGBa image" << std::endl;
407  return EXIT_FAILURE;
408  }
409 
410  vpImage<unsigned char> U_diff_gray_abs;
411  vpImageTools::imageDifferenceAbsolute(U_gray, U_remap_gray, U_diff_gray_abs);
412  double remap_mean_error_gray = U_diff_gray_abs.getSum() / U_diff_gray_abs.getSize();
413  std::cout << "U_diff_gray_abs mean value: " << remap_mean_error_gray << std::endl;
414  if (remap_mean_error_gray > remap_error_threshold) {
415  std::cerr << "Issue with vpImageTools::remap() with unsigned char image" << std::endl;
416  return EXIT_FAILURE;
417  }
418 
419  // Write the undistorted difference images on the disk
420  vpImage<vpRGBa> U_diff;
421  vpImage<unsigned char> U_diff_gray;
422  vpImageTools::imageDifference(U, U_remap, U_diff);
423  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_diff_color.png"));
424  std::cout << "\nWrite undistorted image: " << filename << std::endl;
425  vpImageIo::write(U_diff, filename);
426 
427  vpImageTools::imageDifference(U_gray, U_remap_gray, U_diff_gray);
428  filename = vpIoTools::path(vpIoTools::createFilePath(opath, "grid36-01_undistorted_diff_gray.png"));
429  std::cout << "Write undistorted image: " << filename << std::endl;
430  vpImageIo::write(U_diff_gray, filename);
431 
432  return EXIT_SUCCESS;
433  }
434  catch (const vpException &e) {
435  std::cout << "Catch an exception: " << e << std::endl;
436  return EXIT_FAILURE;
437  }
438 }
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition: vpArray2D.h:145
Generic class defining intrinsic camera parameters.
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
static void initUndistortMap(const vpCameraParameters &cam, unsigned int width, unsigned int height, vpArray2D< int > &mapU, vpArray2D< int > &mapV, vpArray2D< float > &mapDu, vpArray2D< float > &mapDv)
static void resize(const vpImage< Type > &I, vpImage< Type > &Ires, unsigned int width, unsigned int height, const vpImageInterpolationType &method=INTERPOLATION_NEAREST, unsigned int nThreads=0)
static void remap(const vpImage< unsigned char > &I, const vpArray2D< int > &mapU, const vpArray2D< int > &mapV, const vpArray2D< float > &mapDu, const vpArray2D< float > &mapDv, vpImage< unsigned char > &Iundist)
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
Definition: vpImageTools.h:664
static void imageDifferenceAbsolute(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
double getSum(const vpImage< bool > *p_mask=nullptr, unsigned int *nbValidPoints=nullptr) const
Compute the sum of image intensities.
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string path(const std::string &pathname)
Definition: vpIoTools.cpp:1005
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT double measureTimeMs()