Visual Servoing Platform  version 3.6.1 under development (2024-12-11)
tutorial-image-simulator.cpp
1 #include <visp3/core/vpConfig.h>
3 #include <visp3/gui/vpDisplayGDI.h>
4 #include <visp3/gui/vpDisplayOpenCV.h>
5 #include <visp3/gui/vpDisplayX.h>
6 #include <visp3/io/vpImageIo.h>
8 #include <visp3/robot/vpImageSimulator.h>
10 
11 int main()
12 {
13 #ifdef ENABLE_VISP_NAMESPACE
14  using namespace VISP_NAMESPACE_NAME;
15 #endif
16  try {
19  vpImageIo::read(target, "./target_square.pgm");
21 
23  vpColVector X[4];
24  for (int i = 0; i < 4; i++)
25  X[i].resize(3);
26  // Top left Top right Bottom right Bottom left
27  X[0][0] = -0.1;
28  X[1][0] = 0.1;
29  X[2][0] = 0.1;
30  X[3][0] = -0.1;
31  X[0][1] = -0.1;
32  X[1][1] = -0.1;
33  X[2][1] = 0.1;
34  X[3][1] = 0.1;
35  X[0][2] = 0;
36  X[1][2] = 0;
37  X[2][2] = 0;
38  X[3][2] = 0;
40 
42  vpImage<unsigned char> I(480, 640);
45  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2);
48  vpHomogeneousMatrix cMo(0, 0, 0.35, 0, vpMath::rad(30), vpMath::rad(15));
50 
52  vpImageSimulator sim;
54  sim.init(target, X);
56 
57  // Get the new image of the projected planar image target
59  sim.setCleanPreviousImage(true);
60  sim.setCameraPosition(cMo);
61  sim.getImage(I, cam);
63 
65  try {
66  vpImageIo::write(I, "./rendered_image.jpg");
67  }
68  catch (...) {
69  std::cout << "Unsupported image format" << std::endl;
70  }
72 
73 #if defined(VISP_HAVE_X11)
74  vpDisplayX d(I);
75 #elif defined(VISP_HAVE_GDI)
76  vpDisplayGDI d(I);
77 #elif defined(HAVE_OPENCV_HIGHGUI)
78  vpDisplayOpenCV d(I);
79 #else
80  std::cout << "No image viewer is available..." << std::endl;
81 #endif
82 
83  vpDisplay::setTitle(I, "Planar image projection");
86  std::cout << "A click to quit..." << std::endl;
88  }
89  catch (const vpException &e) {
90  std::cout << "Catch an exception: " << e << std::endl;
91  }
92 }
Generic class defining intrinsic camera parameters.
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void setTitle(const vpImage< unsigned char > &I, const std::string &windowtitle)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Implementation of an homogeneous matrix and operations on such kind of matrices.
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
Class which enables to project an image in the 3D space and get the view of a virtual camera.
void getImage(vpImage< unsigned char > &I, const vpCameraParameters &cam)
void init(const vpImage< unsigned char > &I, vpColVector *X)
void setCleanPreviousImage(const bool &clean, const vpColor &color=vpColor::white)
void setInterpolationType(const vpInterpolationType interplt)
void setCameraPosition(const vpHomogeneousMatrix &cMt)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static double rad(double deg)
Definition: vpMath.h:129