File: tutorial-undistort.cpp

package info (click to toggle)
visp 3.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 119,296 kB
  • sloc: cpp: 500,914; ansic: 52,904; xml: 22,642; python: 7,365; java: 4,247; sh: 482; makefile: 237; objc: 145
file content (71 lines) | stat: -rw-r--r-- 2,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! \example tutorial-undistort.cpp
#include <visp3/core/vpImageTools.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpXmlParserCamera.h>
#include <visp3/io/vpImageIo.h>

int main(int argc, char **argv)
{
  std::string opt_input_image = "chessboard.jpg";
  std::string opt_camera_file = "camera.xml";
  std::string opt_camera_name = "Camera";

  try {
    for (int i = 1; i < argc; i++) {
      if (std::string(argv[i]) == "--image" && i + 1 < argc) {
        opt_input_image = std::string(argv[i + 1]);
      } else if (std::string(argv[i]) == "--camera-file" && i + 1 < argc) {
        opt_camera_file = std::string(argv[i + 1]);
      } else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
        opt_camera_name = std::string(argv[i + 1]);
      } else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
        std::cout << argv[0] << " [--image <input image (pgm,ppm,jpeg,png,tiff,bmp,ras,jp2)>]"
                  << " [--camera-file <xml file>] [--camera-name <name>] [--help] [-h]\n"
                  << std::endl;
        std::cout << "Examples: " << std::endl
                  << argv[0] << std::endl
                  << argv[0] << " --image chessboard.jpg --camera-file camera.xml --camera-name Camera" << std::endl;
        return EXIT_SUCCESS;
      }
    }

    //! [Load image]
    vpImage<unsigned char> I;
    std::cout << "Read input image: " << opt_input_image << std::endl;
    vpImageIo::read(I, opt_input_image);
    //! [Load image]

    //! [Load camera parameters from xml]
    vpCameraParameters cam;
    vpXmlParserCamera p;
    vpCameraParameters::vpCameraParametersProjType projModel;
    projModel = vpCameraParameters::perspectiveProjWithDistortion;
    if (p.parse(cam, opt_camera_file, opt_camera_name, projModel, I.getWidth(), I.getHeight()) !=
        vpXmlParserCamera::SEQUENCE_OK) {
      std::cout << "Cannot found parameters for camera named \"Camera\"" << std::endl;
    }
//! [Load camera parameters from xml]
//! [Set camera parameters]
#if 0
    cam.initPersProjWithDistortion(582.7, 580.6, 326.6, 215.0, -0.3372, 0.4021);
#endif
    //! [Set camera parameters]

    std::cout << cam << std::endl;

    //! [Create image without distortion]
    vpImage<unsigned char> Iud;
    vpImageTools::undistort(I, cam, Iud);
    std::string name_we = vpIoTools::getNameWE(opt_input_image);
    std::string ext = vpIoTools::getFileExtension(opt_input_image);
    std::string output_image = name_we + "-undistort" + ext;
    std::cout << "Save undistorted image in: " << output_image << std::endl;
    vpImageIo::write(Iud, output_image);
    //! [Create image without distortion]
  } catch (const vpException &e) {
    std::cout << "Catch an exception: " << e << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}