Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-undistort.cpp
#include <visp3/core/vpConfig.h>
#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)
{
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
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;
}
}
std::cout << "Read input image: " << opt_input_image << std::endl;
vpImageIo::read(I, opt_input_image);
#if defined(VISP_HAVE_PUGIXML)
if (p.parse(cam, opt_camera_file, opt_camera_name, projModel, I.getWidth(), I.getHeight()) !=
std::cout << "Cannot found parameters for camera named \"Camera\"" << std::endl;
}
#else
cam.initPersProjWithDistortion(582.7, 580.6, 326.6, 215.0, -0.3372, 0.4021);
#endif
std::cout << cam << std::endl;
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);
}
catch (const vpException &e) {
std::cout << "Catch an exception: " << e << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
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:60
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 undistort(const vpImage< Type > &I, const vpCameraParameters &cam, vpImage< Type > &newI, unsigned int nThreads=2)
Definition: vpImageTools.h:664
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getFileExtension(const std::string &pathname, bool checkFile=false)
Definition: vpIoTools.cpp:1124
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
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)