Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
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
8 main( int argc, char **argv )
9 {
10  std::string opt_input_image = "chessboard.jpg";
11  std::string opt_camera_file = "camera.xml";
12  std::string opt_camera_name = "Camera";
13 
14  try
15  {
16  for ( int i = 1; i < argc; i++ )
17  {
18  if ( std::string( argv[i] ) == "--image" && i + 1 < argc )
19  {
20  opt_input_image = std::string( argv[i + 1] );
21  }
22  else if ( std::string( argv[i] ) == "--camera-file" && i + 1 < argc )
23  {
24  opt_camera_file = std::string( argv[i + 1] );
25  }
26  else if ( std::string( argv[i] ) == "--camera-name" && i + 1 < argc )
27  {
28  opt_camera_name = std::string( argv[i + 1] );
29  }
30  else if ( std::string( argv[i] ) == "--help" || std::string( argv[i] ) == "-h" )
31  {
32  std::cout << argv[0] << " [--image <input image (pgm,ppm,jpeg,png,tiff,bmp,ras,jp2)>]"
33  << " [--camera-file <xml file>] [--camera-name <name>] [--help] [-h]\n"
34  << std::endl;
35  std::cout << "Examples: " << std::endl
36  << argv[0] << std::endl
37  << argv[0] << " --image chessboard.jpg --camera-file camera.xml --camera-name Camera" << std::endl;
38  return EXIT_SUCCESS;
39  }
40  }
41 
44  std::cout << "Read input image: " << opt_input_image << std::endl;
45  vpImageIo::read( I, opt_input_image );
47 
53  if ( p.parse( cam, opt_camera_file, opt_camera_name, projModel, I.getWidth(), I.getHeight() ) !=
55  {
56  std::cout << "Cannot found parameters for camera named \"Camera\"" << std::endl;
57  }
60 #if 0
61  cam.initPersProjWithDistortion(582.7, 580.6, 326.6, 215.0, -0.3372, 0.4021);
62 #endif
63 
65  std::cout << cam << std::endl;
66 
69  vpImageTools::undistort( I, cam, Iud );
70  std::string name_we = vpIoTools::getNameWE( opt_input_image );
71  std::string ext = vpIoTools::getFileExtension( opt_input_image );
72  std::string output_image = name_we + "-undistort" + ext;
73  std::cout << "Save undistorted image in: " << output_image << std::endl;
74  vpImageIo::write( Iud, output_image );
76  }
77  catch ( const vpException &e )
78  {
79  std::cout << "Catch an exception: " << e << std::endl;
80  }
81 
82  return 0;
83 }
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:149
error that can be emited by ViSP classes.
Definition: vpException.h:71
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
Definition: vpIoTools.cpp:1430
XML parser to load and save intrinsic camera parameters.
void initPersProjWithDistortion(double px, double py, double u0, double v0, double kud, double kdu)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:293
Generic class defining 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)
static void undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
Definition: vpImageTools.h:645
unsigned int getHeight() const
Definition: vpImage.h:188
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1532
unsigned int getWidth() const
Definition: vpImage.h:246