Visual Servoing Platform  version 3.6.1 under development (2025-02-01)
tutorial-pose-from-points-live.cpp
1 
2 #include <iostream>
3 
4 #include <visp3/core/vpConfig.h>
5 
7 // Comment / uncomment following lines to use the specific 3rd party compatible with your camera
8 // #undef VISP_HAVE_V4L2
9 // #undef VISP_HAVE_DC1394
10 // #undef VISP_HAVE_CMU1394
11 // #undef VISP_HAVE_FLYCAPTURE
12 // #undef VISP_HAVE_REALSENSE2
13 // #undef HAVE_OPENCV_HIGHGUI
14 // #undef HAVE_OPENCV_VIDEOIO
16 
17 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && defined(VISP_HAVE_PUGIXML) && \
18  (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
19  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)) || \
20  ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
21 
22 #ifdef VISP_HAVE_MODULE_SENSOR
23 #include <visp3/sensor/vp1394CMUGrabber.h>
24 #include <visp3/sensor/vp1394TwoGrabber.h>
25 #include <visp3/sensor/vpFlyCaptureGrabber.h>
26 #include <visp3/sensor/vpRealSense2.h>
27 #include <visp3/sensor/vpV4l2Grabber.h>
28 #endif
29 #include <visp3/core/vpXmlParserCamera.h>
30 #include <visp3/gui/vpDisplayGDI.h>
31 #include <visp3/gui/vpDisplayOpenCV.h>
32 #include <visp3/gui/vpDisplayX.h>
33 
34 #if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
35 #include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
36 #elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
37 #include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
38 #endif
39 
40 #include "pose_helper.h"
41 
42 int main(int argc, char **argv)
43 {
44 #ifdef ENABLE_VISP_NAMESPACE
45  using namespace VISP_NAMESPACE_NAME;
46 #endif
47  try {
48  std::string opt_intrinsic_file; // xml file obtained from camera calibration
49  std::string opt_camera_name; // corresponding camera name in the xml calibration file
50  double opt_square_width = 0.12;
51  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
52 
53  for (int i = 1; i < argc; i++) {
54  if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
55  opt_intrinsic_file = std::string(argv[++i]);
56  }
57  else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
58  opt_camera_name = std::string(argv[++i]);
59  }
60  else if (std::string(argv[i]) == "--camera-device" && i + 1 < argc) {
61  opt_device = atoi(argv[++i]);
62  }
63  else if (std::string(argv[i]) == "--square-width" && i + 1 < argc) {
64  opt_device = atoi(argv[++i]);
65  }
66  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
67  std::cout << "\nUsage: " << argv[0]
68  << " [--camera-device <camera device> (default: 0)]"
69  << " [--intrinsic <xml calibration file> (default: empty)]"
70  << " [--camera-name <camera name in xml calibration file> (default: empty)]"
71  << " [--square-width <square width in meter (default: 0.12)]"
72  << " [--help] [-h]\n"
73  << std::endl
74  << "Example using default camera parameters and square size:\n"
75  << " " << argv[0] << "\n"
76  << std::endl
77  << "Example fully tuned for a 0.1m x 0.1m square:\n"
78  << " " << argv[0] << " --intrinsic camera.xml --camera-name Camera --square-width 0.1\n"
79  << std::endl;
80  return EXIT_SUCCESS;
81  }
82  }
83 
86 
88 #if defined(VISP_HAVE_V4L2)
89  vpV4l2Grabber g;
90  std::ostringstream device;
91  device << "/dev/video" << opt_device;
92  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
93  g.setDevice(device.str());
94  g.setScale(1);
95  g.open(I);
96  cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
97 #elif defined(VISP_HAVE_DC1394)
98  (void)opt_device; // To avoid non used warning
99  std::cout << "Use DC1394 grabber" << std::endl;
101  g.open(I);
102  cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
103 #elif defined(VISP_HAVE_CMU1394)
104  (void)opt_device; // To avoid non used warning
105  std::cout << "Use CMU1394 grabber" << std::endl;
107  g.open(I);
108  cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
109 #elif defined(VISP_HAVE_FLYCAPTURE)
110  (void)opt_device; // To avoid non used warning
111  std::cout << "Use FlyCapture grabber" << std::endl;
113  g.open(I);
114  cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
115 #elif defined(VISP_HAVE_REALSENSE2)
116  (void)opt_device; // To avoid non used warning
117  std::cout << "Use Realsense 2 grabber" << std::endl;
118  vpRealSense2 g;
119  rs2::config config;
120  config.disable_stream(RS2_STREAM_DEPTH);
121  config.disable_stream(RS2_STREAM_INFRARED);
122  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
123  g.open(config);
124  g.acquire(I);
125 
126  std::cout << "Read camera parameters from Realsense device" << std::endl;
128 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
129  std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
130  cv::VideoCapture g(opt_device); // Open the default camera
131  if (!g.isOpened()) { // Check if we succeeded
132  std::cout << "Failed to open the camera" << std::endl;
133  return EXIT_FAILURE;
134  }
135  cv::Mat frame;
136  g >> frame; // get a new frame from camera
137  vpImageConvert::convert(frame, I);
138  cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
139 #endif
141 
142  // Parameters of our camera
143  vpXmlParserCamera parser;
144  if (!opt_intrinsic_file.empty() && !opt_camera_name.empty()) {
145  std::cout << "Intrinsic file: " << opt_intrinsic_file << std::endl;
146  std::cout << "Camera name : " << opt_camera_name << std::endl;
147  if (parser.parse(cam, opt_intrinsic_file, opt_camera_name, vpCameraParameters::perspectiveProjWithDistortion) ==
149  std::cout << "Succeed to read camera parameters from xml file" << std::endl;
150  }
151  else {
152  std::cout << "Unable to read camera parameters from xml file" << std::endl;
153  }
154  }
155 
156  std::cout << "Square width : " << opt_square_width << std::endl;
157  std::cout << cam << std::endl;
158 
159  // The pose container
161 
162  std::vector<vpDot2> dot(4);
163  std::vector<vpPoint> point; // 3D coordinates of the points
164  std::vector<vpImagePoint> ip; // 2D coordinates of the points in pixels
165  double L = opt_square_width / 2.;
166  point.push_back(vpPoint(-L, -L, 0));
167  point.push_back(vpPoint(L, -L, 0));
168  point.push_back(vpPoint(L, L, 0));
169  point.push_back(vpPoint(-L, L, 0));
170 
171 #if defined(VISP_HAVE_X11)
172  vpDisplayX d(I);
173 #elif defined(VISP_HAVE_GDI)
174  vpDisplayGDI d(I);
175 #elif defined(HAVE_OPENCV_HIGHGUI)
176  vpDisplayOpenCV d(I);
177 #endif
178 
179  bool quit = false;
180  bool apply_cv = false; // apply computer vision
181  bool init_cv = true; // initialize tracking and pose computation
182 
183  while (!quit) {
184  double t_begin = vpTime::measureTimeMs();
185  // Image Acquisition
186 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
187  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
188  g.acquire(I);
189 #elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
190  g >> frame;
191  vpImageConvert::convert(frame, I);
192 #endif
194  if (apply_cv) {
195  try {
196  ip = track(I, dot, init_cv);
197  computePose(point, ip, cam, init_cv, cMo);
198  vpDisplay::displayFrame(I, cMo, cam, opt_square_width, vpColor::none, 3);
199  if (init_cv)
200  init_cv = false; // turn off the computer vision initialisation specific stuff
201 
202  { // Display estimated pose in [m] and [deg]
203  vpPoseVector pose(cMo);
204  std::stringstream ss;
205  ss << "Translation: " << std::setprecision(5) << pose[0] << " " << pose[1] << " " << pose[2] << " [m]";
206  vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
207  ss.str(""); // erase ss
208  ss << "Rotation tu: " << std::setprecision(4) << vpMath::deg(pose[3]) << " " << vpMath::deg(pose[4]) << " "
209  << vpMath::deg(pose[5]) << " [deg]";
210  vpDisplay::displayText(I, 80, 20, ss.str(), vpColor::red);
211  }
212  }
213  catch (...) {
214  std::cout << "Computer vision failure." << std::endl;
215  apply_cv = false;
216  init_cv = true;
217  }
218  }
219  vpDisplay::displayText(I, 20, 20, "Right click: quit", vpColor::red);
220  if (apply_cv) {
221  vpDisplay::displayText(I, 40, 20, "Computer vision in progress...", vpColor::red);
222  }
223  else {
224  vpDisplay::displayText(I, 40, 20, "Left click : start", vpColor::red);
225  }
227  if (vpDisplay::getClick(I, button, false)) {
228  if (button == vpMouseButton::button3) {
229  quit = true;
230  }
231  else if (button == vpMouseButton::button1) {
232  apply_cv = true;
233  }
234  }
235  {
236  std::stringstream ss;
237  ss << "Time: " << vpTime::measureTimeMs() - t_begin << " ms";
238  vpDisplay::displayText(I, 20, I.getWidth() - 100, ss.str(), vpColor::red);
239  }
240  vpDisplay::flush(I);
241  }
242  }
243  catch (const vpException &e) {
244  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
245  }
246 }
247 
248 #else
249 
250 int main()
251 {
252  std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
253 }
254 
255 #endif
Firewire cameras video capture based on CMU 1394 Digital Camera SDK.
void open(vpImage< unsigned char > &I)
Class for firewire ieee1394 video devices using libdc1394-2.x api.
void open(vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition: vpColor.h:198
static const vpColor none
Definition: vpColor.h:210
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 displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
void open(vpImage< unsigned char > &I)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static double deg(double rad)
Definition: vpMath.h:119
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:79
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:203
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
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)
VISP_EXPORT double measureTimeMs()