Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testUndistortImage.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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:t:s:h"
60
61/*
62 Print the program options.
63
64 \param name : Program name.
65 \param badparam : Bad parameter name.
66 \param ipath: Input image path.
67 \param opath : Output image path.
68 \param user : Username.
69 */
70void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
71 const std::string &user)
72{
73#if VISP_HAVE_DATASET_VERSION >= 0x030600
74 std::string ext("png");
75#else
76 std::string ext("pgm");
77#endif
78 fprintf(stdout, "\n\
79Read an image from the disk, undistort it \n\
80and save the undistorted image on the disk.\n\
81(grid36-01_undistorted.pgm).\n\
82\n\
83SYNOPSIS\n\
84 %s [-i <input image path>] [-o <output image path>] [-t <nThreads>] [-s <scale>]\n\
85 [-h]\n\
86 ",
87 name);
88
89 fprintf(stdout, "\n\
90OPTIONS: Default\n\
91 -i <input image path> %s\n\
92 Set image input path.\n\
93 From this path read \"calibration/grid36-01.%s\"\n\
94 image.\n\
95 Setting the VISP_INPUT_IMAGE_PATH environment\n\
96 variable produces the same behaviour than using\n\
97 this option.\n\
98 \n\
99 -o <output image path> %s\n\
100 Set image output path.\n\
101 From this directory, creates the \"%s\"\n\
102 subdirectory depending on the username, where \n\
103 grid36-01_undistorted.pgm output image is written.\n\
104\n\
105 -t <nThreads> \n\
106 Set the number of threads to use for vpImageTools::undistort().\n\
107\n\
108 -s <scale> \n\
109 Resize the image by the specified scale factor.\n\
110\n\
111 -h\n\
112 Print the help.\n\n",
113 ext.c_str(), ipath.c_str(), opath.c_str(), user.c_str());
114
115 if (badparam)
116 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
117}
118
131bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user,
132 unsigned int &nThreads, unsigned int &scale)
133{
134 const char *optarg_;
135 int c;
136 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
137
138 switch (c) {
139 case 'i':
140 ipath = optarg_;
141 break;
142 case 'o':
143 opath = optarg_;
144 break;
145 case 't':
146 nThreads = atoi(optarg_);
147 break;
148 case 's':
149 scale = atoi(optarg_);
150 break;
151 case 'h':
152 usage(argv[0], NULL, ipath, opath, user);
153 return false;
154
155 case 'c':
156 case 'd':
157 break;
158
159 default:
160 usage(argv[0], optarg_, ipath, opath, user);
161 return false;
162 }
163 }
164
165 if ((c == 1) || (c == -1)) {
166 // standalone param or error
167 usage(argv[0], NULL, ipath, opath, user);
168 std::cerr << "ERROR: " << std::endl;
169 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
170 return false;
171 }
172
173 return true;
174}
175
176int main(int argc, const char **argv)
177{
178 try {
179 std::string env_ipath;
180 std::string opt_ipath;
181 std::string opt_opath;
182 std::string ipath;
183 std::string opath;
184 std::string filename;
185 std::string username;
186 unsigned int nThreads = 2;
187 unsigned int scale = 1;
188
189#if VISP_HAVE_DATASET_VERSION >= 0x030600
190 std::string ext("png");
191#else
192 std::string ext("pgm");
193#endif
194
195 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
196 // environment variable value
198
199 // Set the default input path
200 if (!env_ipath.empty())
201 ipath = env_ipath;
202
203// Set the default output path
204#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
205 opt_opath = "/tmp";
206#elif defined(_WIN32)
207 opt_opath = "C:\\temp";
208#endif
209
210 // Get the user login name
211 vpIoTools::getUserName(username);
212
213 // Read the command line options
214 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nThreads, scale) == false) {
215 return EXIT_FAILURE;
216 }
217
218 // Get the option values
219 if (!opt_ipath.empty())
220 ipath = opt_ipath;
221 if (!opt_opath.empty())
222 opath = opt_opath;
223
224 // Append to the output path string, the login name of the user
225 opath = vpIoTools::createFilePath(opath, username);
226
227 // Test if the output path exist. If no try to create it
228 if (vpIoTools::checkDirectory(opath) == false) {
229 try {
230 // Create the dirname
232 } catch (...) {
233 usage(argv[0], NULL, ipath, opt_opath, username);
234 std::cerr << std::endl << "ERROR:" << std::endl;
235 std::cerr << " Cannot create " << opath << std::endl;
236 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
237 return EXIT_FAILURE;
238 }
239 }
240
241 // Compare ipath and env_ipath. If they differ, we take into account
242 // the input path comming from the command line option
243 if (opt_ipath.empty()) {
244 if (ipath != env_ipath) {
245 std::cout << std::endl << "WARNING: " << std::endl;
246 std::cout << " Since -i <visp image path=" << ipath << "> "
247 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
248 << " we skip the environment variable." << std::endl;
249 }
250 }
251
252 // Test if an input path is set
253 if (opt_ipath.empty() && env_ipath.empty()) {
254 usage(argv[0], NULL, ipath, opt_opath, username);
255 std::cerr << std::endl << "ERROR:" << std::endl;
256 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
257 << " environment variable to specify the location of the " << std::endl
258 << " image path where test images are located." << std::endl
259 << std::endl;
260 return EXIT_FAILURE;
261 }
262
263 //
264 // Here starts really the test
265 //
266 vpImage<vpRGBa> I, I_; // Input image
268 vpImage<vpRGBa> U; // undistorted output image
269 vpImage<unsigned char> U_gray; // undistorted output image
270 vpImage<vpRGBa> U_remap; // undistorted output image
271 vpImage<unsigned char> U_remap_gray; // undistorted output image
272
274 cam.initPersProjWithDistortion(600, 600, 320, 240, -0.17, 0.17);
275 // Read the input grey image from the disk
276 filename = vpIoTools::createFilePath(ipath, "calibration/grid36-01." + ext);
277 std::cout << "Read image: " << filename << std::endl;
278 vpImageIo::read(I_, filename);
279 if (scale > 1) {
280 std::cout << "Scale the image by a factor of " << scale << std::endl;
281 vpImageTools::resize(I_, I, I_.getWidth() * scale, I_.getHeight() * scale);
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 } catch (const vpException &e) {
434 std::cout << "Catch an exception: " << e << std::endl;
435 return EXIT_FAILURE;
436 }
437}
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition vpArray2D.h:131
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:59
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)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
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)
static void imageDifferenceAbsolute(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
double getSum() const
Definition vpImage.h:1885
unsigned int getSize() const
Definition vpImage.h:223
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static std::string path(const std::string &pathname)
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
VISP_EXPORT double measureTimeMs()