Visual Servoing Platform  version 3.6.1 under development (2024-12-03)
tutorial-bridge-opencv-camera-param.cpp
1 #include <iostream>
3 #include <visp3/core/vpCameraParameters.h>
4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/io/vpImageIo.h>
7 
8 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_IMGPROC)
9 #include <opencv2/calib3d/calib3d.hpp>
10 #include <opencv2/core/core.hpp>
11 #include <opencv2/imgproc/imgproc.hpp>
12 
13 int main()
14 {
15 #ifdef ENABLE_VISP_NAMESPACE
16  using namespace VISP_NAMESPACE_NAME;
17 #endif
19  double u0 = 326.6;
20  double v0 = 215.0;
21  double px = 582.7;
22  double py = 580.6;
23  double kud = -0.3372;
24  double kdu = 0.4021;
25  vpCameraParameters cam(px, py, u0, v0, kud, kdu);
27 
29  cv::Mat K = (cv::Mat_<double>(3, 3) << cam.get_px(), 0, cam.get_u0(), 0, cam.get_py(), cam.get_v0(), 0, 0, 1);
30  cv::Mat D = (cv::Mat_<double>(4, 1) << cam.get_kud(), 0, 0, 0);
32 
35  vpImageIo::read(I, "chessboard.pgm");
37 
39  cv::Mat image;
40  vpImageConvert::convert(I, image);
42 
44  cv::Mat imageUndistorted;
45  cv::undistort(image, imageUndistorted, K, D);
47 
49  vpImage<unsigned char> IUndistorted;
50  vpImageConvert::convert(imageUndistorted, IUndistorted);
52 
54  vpImageIo::write(IUndistorted, "chessboard-undistorted.pgm");
56 }
57 
58 #else
59 int main()
60 {
61  std::cout << "This tutorial required OpenCV imgproc module" << std::endl;
62  return EXIT_SUCCESS;
63 }
64 #endif
Generic class defining intrinsic camera parameters.
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)
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