Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
tutorial-undistort.cpp
1 #include <visp3/core/vpImageTools.h>
3 #include <visp3/core/vpIoTools.h>
4 #include <visp3/core/vpXmlParserCamera.h>
5 #include <visp3/io/vpImageIo.h>
6 
7 int main(int argc, char **argv)
8 {
9  std::string opt_input_image = "chessboard.jpg";
10  std::string opt_camera_file = "camera.xml";
11  std::string opt_camera_name = "Camera";
12 
13  try {
14  for (int i = 1; i < argc; i++) {
15  if (std::string(argv[i]) == "--image" && i + 1 < argc) {
16  opt_input_image = std::string(argv[i + 1]);
17  }
18  else if (std::string(argv[i]) == "--camera-file" && i + 1 < argc) {
19  opt_camera_file = std::string(argv[i + 1]);
20  }
21  else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
22  opt_camera_name = std::string(argv[i + 1]);
23  }
24  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
25  std::cout << argv[0] << " [--image <input image (pgm,ppm,jpeg,png,tiff,bmp,ras,jp2)>]"
26  << " [--camera-file <xml file>] [--camera-name <name>] [--help] [-h]\n"
27  << std::endl;
28  std::cout << "Examples: " << std::endl
29  << argv[0] << std::endl
30  << argv[0] << " --image chessboard.jpg --camera-file camera.xml --camera-name Camera" << std::endl;
31  return EXIT_SUCCESS;
32  }
33  }
34 
37  std::cout << "Read input image: " << opt_input_image << std::endl;
38  vpImageIo::read(I, opt_input_image);
40 
41 
43 #if defined(VISP_HAVE_PUGIXML)
48  if (p.parse(cam, opt_camera_file, opt_camera_name, projModel, I.getWidth(), I.getHeight()) !=
50  std::cout << "Cannot found parameters for camera named \"Camera\"" << std::endl;
51  }
53 #else
55  cam.initPersProjWithDistortion(582.7, 580.6, 326.6, 215.0, -0.3372, 0.4021);
57 #endif
58 
59  std::cout << cam << std::endl;
60 
63  vpImageTools::undistort(I, cam, Iud);
64  std::string name_we = vpIoTools::getNameWE(opt_input_image);
65  std::string ext = vpIoTools::getFileExtension(opt_input_image);
66  std::string output_image = name_we + "-undistort" + ext;
67  std::cout << "Save undistorted image in: " << output_image << std::endl;
68  vpImageIo::write(Iud, output_image);
70  }
71  catch (const vpException &e) {
72  std::cout << "Catch an exception: " << e << std::endl;
73  return EXIT_FAILURE;
74  }
75 
76  return EXIT_SUCCESS;
77 }
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
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 read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
Definition: vpImageTools.h:654
unsigned int getWidth() const
Definition: vpImage.h:245
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
Definition: vpIoTools.cpp:1900
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:2003
XML parser to load and save intrinsic camera parameters.
int parse(vpCameraParameters &cam, const std::string &filename, const std::string &camera_name, const vpCameraParameters::vpCameraParametersProjType &projModel, unsigned int image_width=0, unsigned int image_height=0, bool verbose=true)