Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-pose-from-points-live.cpp
1 
2 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_SENSOR
4 #include <visp3/sensor/vp1394CMUGrabber.h>
5 #include <visp3/sensor/vp1394TwoGrabber.h>
6 #include <visp3/sensor/vpFlyCaptureGrabber.h>
7 #include <visp3/sensor/vpRealSense2.h>
8 #include <visp3/sensor/vpV4l2Grabber.h>
9 #endif
10 #include <visp3/core/vpXmlParserCamera.h>
11 #include <visp3/gui/vpDisplayGDI.h>
12 #include <visp3/gui/vpDisplayOpenCV.h>
13 #include <visp3/gui/vpDisplayX.h>
14 
15 #if defined(HAVE_OPENCV_VIDEOIO)
16 #include <opencv2/videoio.hpp>
17 #endif
18 
19 #include "pose_helper.h"
20 
21 // Comment / uncomment following lines to use the specific 3rd party compatible with your camera
23 // #undef VISP_HAVE_V4L2
24 // #undef VISP_HAVE_DC1394
25 // #undef VISP_HAVE_CMU1394
26 // #undef VISP_HAVE_FLYCAPTURE
27 // #undef VISP_HAVE_REALSENSE2
28 // #undef HAVE_OPENCV_VIDEOIO
30 
31 int main(int argc, char **argv)
32 {
33 #if (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV)) && defined(VISP_HAVE_PUGIXML) && \
34  (defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
35  defined(HAVE_OPENCV_VIDEOIO) || defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2))
36 #ifdef ENABLE_VISP_NAMESPACE
37  using namespace VISP_NAMESPACE_NAME;
38 #endif
39  try {
40  std::string opt_intrinsic_file; // xml file obtained from camera calibration
41  std::string opt_camera_name; // corresponding camera name in the xml calibration file
42  double opt_square_width = 0.12;
43  int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
44 
45  for (int i = 0; i < argc; i++) {
46  if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
47  opt_intrinsic_file = std::string(argv[i + 1]);
48  }
49  else if (std::string(argv[i]) == "--camera_name" && i + 1 < argc) {
50  opt_camera_name = std::string(argv[i + 1]);
51  }
52  else if (std::string(argv[i]) == "--camera_device" && i + 1 < argc) {
53  opt_device = atoi(argv[i + 1]);
54  }
55  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
56  std::cout << "\nUsage: " << argv[0] << " [--camera_device <camera device> (default: 0)]"
57  << " [--intrinsic <xml calibration file> (default: empty)]"
58  " [--camera_name <camera name in xml calibration file> (default: empty)]"
59  " [--square_width <square width in meter (default: 0.12)] [--help] [-h]\n"
60  << "\nExample using default camera parameters and square size:\n"
61  << " " << argv[0] << "\n"
62  << "\nExample fully tuned for a 0.1m x 0.1m square:\n"
63  << " " << argv[0] << " --intrinsic camera.xml --camera_name Camera --square_width 0.1\n"
64  << std::endl;
65  return EXIT_SUCCESS;
66  }
67  }
68 
70 
71  // Parameters of our camera
72  vpCameraParameters cam(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
73  vpXmlParserCamera parser;
74  if (!opt_intrinsic_file.empty() && !opt_camera_name.empty()) {
75  std::cout << "Intrinsic file: " << opt_intrinsic_file << std::endl;
76  std::cout << "Camera name : " << opt_camera_name << std::endl;
77  if (parser.parse(cam, opt_intrinsic_file, opt_camera_name, vpCameraParameters::perspectiveProjWithDistortion) ==
79  std::cout << "Succeed to read camera parameters from xml file" << std::endl;
80  }
81  else {
82  std::cout << "Unable to read camera parameters from xml file" << std::endl;
83  }
84  }
85 
87 #if defined(VISP_HAVE_V4L2)
88  vpV4l2Grabber g;
89  std::ostringstream device;
90  device << "/dev/video" << opt_device;
91  std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
92  g.setDevice(device.str());
93  g.setScale(1);
94  g.open(I);
95 #elif defined(VISP_HAVE_DC1394)
96  (void)opt_device; // To avoid non used warning
97  std::cout << "Use DC1394 grabber" << std::endl;
99  g.open(I);
100 #elif defined(VISP_HAVE_CMU1394)
101  (void)opt_device; // To avoid non used warning
102  std::cout << "Use CMU1394 grabber" << std::endl;
104  g.open(I);
105 #elif defined(VISP_HAVE_FLYCAPTURE)
106  (void)opt_device; // To avoid non used warning
107  std::cout << "Use FlyCapture grabber" << std::endl;
109  g.open(I);
110 #elif defined(VISP_HAVE_REALSENSE2)
111  (void)opt_device; // To avoid non used warning
112  std::cout << "Use Realsense 2 grabber" << std::endl;
113  vpRealSense2 g;
114  rs2::config config;
115  config.disable_stream(RS2_STREAM_DEPTH);
116  config.disable_stream(RS2_STREAM_INFRARED);
117  config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
118  g.open(config);
119  g.acquire(I);
120 
121  std::cout << "Read camera parameters from Realsense device" << std::endl;
123 #elif defined(HAVE_OPENCV_VIDEOIO)
124  std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
125  cv::VideoCapture g(opt_device); // Open the default camera
126  if (!g.isOpened()) { // Check if we succeeded
127  std::cout << "Failed to open the camera" << std::endl;
128  return EXIT_FAILURE;
129  }
130  cv::Mat frame;
131  g >> frame; // get a new frame from camera
132  vpImageConvert::convert(frame, I);
133 #endif
135 
136  std::cout << "Square width : " << opt_square_width << std::endl;
137  std::cout << cam << std::endl;
138 
139  // The pose container
141 
142  std::vector<vpDot2> dot(4);
143  std::vector<vpPoint> point; // 3D coordinates of the points
144  std::vector<vpImagePoint> ip; // 2D coordinates of the points in pixels
145  double L = opt_square_width / 2.;
146  point.push_back(vpPoint(-L, -L, 0));
147  point.push_back(vpPoint(L, -L, 0));
148  point.push_back(vpPoint(L, L, 0));
149  point.push_back(vpPoint(-L, L, 0));
150 
151 #if defined(VISP_HAVE_X11)
152  vpDisplayX d(I);
153 #elif defined(VISP_HAVE_GDI)
154  vpDisplayGDI d(I);
155 #elif defined(HAVE_OPENCV_HIGHGUI)
156  vpDisplayOpenCV d(I);
157 #endif
158 
159  bool quit = false;
160  bool apply_cv = false; // apply computer vision
161  bool init_cv = true; // initialize tracking and pose computation
162 
163  while (!quit) {
164  double t_begin = vpTime::measureTimeMs();
165  // Image Acquisition
166 #if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
167  defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
168  g.acquire(I);
169 #elif defined(HAVE_OPENCV_VIDEOIO)
170  g >> frame;
171  vpImageConvert::convert(frame, I);
172 #endif
174  if (apply_cv) {
175  try {
176  ip = track(I, dot, init_cv);
177  computePose(point, ip, cam, init_cv, cMo);
178  vpDisplay::displayFrame(I, cMo, cam, opt_square_width, vpColor::none, 3);
179  if (init_cv)
180  init_cv = false; // turn off the computer vision initialisation specific stuff
181 
182  { // Display estimated pose in [m] and [deg]
183  vpPoseVector pose(cMo);
184  std::stringstream ss;
185  ss << "Translation: " << std::setprecision(5) << pose[0] << " " << pose[1] << " " << pose[2] << " [m]";
186  vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
187  ss.str(""); // erase ss
188  ss << "Rotation tu: " << std::setprecision(4) << vpMath::deg(pose[3]) << " " << vpMath::deg(pose[4]) << " "
189  << vpMath::deg(pose[5]) << " [deg]";
190  vpDisplay::displayText(I, 80, 20, ss.str(), vpColor::red);
191  }
192  }
193  catch (...) {
194  std::cout << "Computer vision failure." << std::endl;
195  apply_cv = false;
196  init_cv = true;
197  }
198  }
199  vpDisplay::displayText(I, 20, 20, "Right click: quit", vpColor::red);
200  if (apply_cv) {
201  vpDisplay::displayText(I, 40, 20, "Computer vision in progress...", vpColor::red);
202  }
203  else {
204  vpDisplay::displayText(I, 40, 20, "Left click : start", vpColor::red);
205  }
207  if (vpDisplay::getClick(I, button, false)) {
208  if (button == vpMouseButton::button3) {
209  quit = true;
210  }
211  else if (button == vpMouseButton::button1) {
212  apply_cv = true;
213  }
214  }
215  {
216  std::stringstream ss;
217  ss << "Time: " << vpTime::measureTimeMs() - t_begin << " ms";
218  vpDisplay::displayText(I, 20, I.getWidth() - 100, ss.str(), vpColor::red);
219  }
220  vpDisplay::flush(I);
221  }
222  }
223  catch (const vpException &e) {
224  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
225  }
226 #elif (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV))
227  (void)argc;
228  (void)argv;
229  std::cout << "Install a 3rd party dedicated to frame grabbing (dc1394, cmu1394, v4l2, OpenCV, FlyCapture, "
230  "Realsense2), configure and build ViSP again to use this example"
231  << std::endl;
232 #else
233  (void)argc;
234  (void)argv;
235  std::cout << "Install a 3rd party dedicated to image display (X11, GDI, OpenCV), configure and build ViSP again to "
236  "use this example"
237  << std::endl;
238 #endif
239  }
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.
@ perspectiveProjWithDistortion
Perspective projection with distortion model.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
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()