Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
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_DISPLAY) && 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)) || defined(VISP_HAVE_OPENCV) && \
20 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || \
21 ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
22
23#ifdef VISP_HAVE_MODULE_SENSOR
24#include <visp3/sensor/vp1394CMUGrabber.h>
25#include <visp3/sensor/vp1394TwoGrabber.h>
26#include <visp3/sensor/vpFlyCaptureGrabber.h>
27#include <visp3/sensor/vpRealSense2.h>
28#include <visp3/sensor/vpV4l2Grabber.h>
29#endif
30#include <visp3/core/vpXmlParserCamera.h>
31#include <visp3/gui/vpDisplayFactory.h>
32
33#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
34#include <opencv2/highgui/highgui.hpp> // for cv::VideoCapture
35#elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
36#include <opencv2/videoio/videoio.hpp> // for cv::VideoCapture
37#endif
38
39#include "pose_helper.h"
40
41void usage(const char **argv, int error)
42{
43 std::cout << "Synopsis" << std::endl
44 << " " << argv[0]
45 << " [--camera-device <id>]"
46#if defined(VISP_HAVE_PUGIXML)
47 << " [--intrinsic <xmlfile>]"
48 << " [--camera-name <name>]"
49#endif
50 << " [--square-width <width>]"
51 << " [--help, -h]" << std::endl
52 << std::endl;
53 std::cout << "Description" << std::endl
54 << " Compute the pose of a square from its 4 corners." << std::endl
55 << std::endl
56 << " --camera-device <id>" << std::endl
57 << " Camera device id." << std::endl
58 << " Default: 0" << std::endl
59 << std::endl
60#if defined(VISP_HAVE_PUGIXML)
61 << " --intrinsic <xmlfile>" << std::endl
62 << " Camera intrinsic parameters file in xml format." << std::endl
63 << " Default: empty" << std::endl
64 << std::endl
65 << " --camera-name <name>" << std::endl
66 << " Camera name in the intrinsic parameters file in xml format." << std::endl
67 << " Default: empty" << std::endl
68 << std::endl
69#endif
70 << " --square-width <width>" << std::endl
71 << " Square width in meter." << std::endl
72 << " Default: 0.12" << std::endl
73 << std::endl
74 << " --help, -h" << std::endl
75 << " Print this helper message." << std::endl
76 << std::endl;
77 std::cout
78 << std::endl
79 << "Example using default camera parameters and square size:\n"
80 << " " << argv[0] << "\n"
81 << std::endl
82 << "Example fully tuned for a 0.1m x 0.1m square:\n"
83 << " " << argv[0] << " --intrinsic camera.xml --camera-name Camera --square-width 0.1\n"
84 << std::endl;
85
86 if (error) {
87 std::cout << "Error" << std::endl
88 << " "
89 << "Unsupported parameter " << argv[error] << std::endl;
90 }
91}
92
93int main(int argc, const char **argv)
94{
95#ifdef ENABLE_VISP_NAMESPACE
96 using namespace VISP_NAMESPACE_NAME;
97#endif
98#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
99 std::shared_ptr<vpDisplay> display;
100#else
101 vpDisplay *display = nullptr;
102#endif
103 try {
104 std::string opt_intrinsic_file; // xml file obtained from camera calibration
105 std::string opt_camera_name; // corresponding camera name in the xml calibration file
106 double opt_square_width = 0.12;
107 int opt_device = 0; // For OpenCV and V4l2 grabber to set the camera device
108
109 for (int i = 1; i < argc; i++) {
110 if (std::string(argv[i]) == "--camera-device" && i + 1 < argc) {
111 opt_device = atoi(argv[++i]);
112 }
113 else if (std::string(argv[i]) == "--intrinsic" && i + 1 < argc) {
114 opt_intrinsic_file = std::string(argv[++i]);
115 }
116 else if (std::string(argv[i]) == "--camera-name" && i + 1 < argc) {
117 opt_camera_name = std::string(argv[++i]);
118 }
119 else if (std::string(argv[i]) == "--square-width" && i + 1 < argc) {
120 opt_device = atoi(argv[++i]);
121 }
122 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
123 usage(argv, 0);
124 return EXIT_SUCCESS;
125 }
126 else {
127 usage(argv, i);
128 return EXIT_FAILURE;
129 }
130 }
131
134
136#if defined(VISP_HAVE_V4L2)
138 std::ostringstream device;
139 device << "/dev/video" << opt_device;
140 std::cout << "Use Video 4 Linux grabber on device " << device.str() << std::endl;
141 g.setDevice(device.str());
142 g.setScale(1);
143 g.open(I);
144 cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
145#elif defined(VISP_HAVE_DC1394)
146 (void)opt_device; // To avoid non used warning
147 std::cout << "Use DC1394 grabber" << std::endl;
149 g.open(I);
150 cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
151#elif defined(VISP_HAVE_CMU1394)
152 (void)opt_device; // To avoid non used warning
153 std::cout << "Use CMU1394 grabber" << std::endl;
155 g.open(I);
156 cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
157#elif defined(VISP_HAVE_FLYCAPTURE)
158 (void)opt_device; // To avoid non used warning
159 std::cout << "Use FlyCapture grabber" << std::endl;
161 g.open(I);
162 cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
163#elif defined(VISP_HAVE_REALSENSE2)
164 (void)opt_device; // To avoid non used warning
165 std::cout << "Use Realsense 2 grabber" << std::endl;
166 vpRealSense2 g;
167 rs2::config config;
168 config.disable_stream(RS2_STREAM_DEPTH);
169 config.disable_stream(RS2_STREAM_INFRARED);
170 config.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_RGBA8, 30);
171 g.open(config);
172 g.acquire(I);
173
174 std::cout << "Read camera parameters from Realsense device" << std::endl;
176#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
177 std::cout << "Use OpenCV grabber on device " << opt_device << std::endl;
178 cv::VideoCapture g(opt_device); // Open the default camera
179 if (!g.isOpened()) { // Check if we succeeded
180 std::cout << "Failed to open the camera" << std::endl;
181 return EXIT_FAILURE;
182 }
183 cv::Mat frame;
184 g >> frame; // get a new frame from camera
185 vpImageConvert::convert(frame, I);
186 cam.initPersProjWithoutDistortion(840, 840, I.getWidth() / 2, I.getHeight() / 2); // Default parameters
187#endif
189
190#if defined(VISP_HAVE_PUGIXML)
191 // Parameters of our camera
193 if (!opt_intrinsic_file.empty() && !opt_camera_name.empty()) {
194 std::cout << "Intrinsic file: " << opt_intrinsic_file << std::endl;
195 std::cout << "Camera name : " << opt_camera_name << std::endl;
196 if (parser.parse(cam, opt_intrinsic_file, opt_camera_name, vpCameraParameters::perspectiveProjWithDistortion) ==
198 std::cout << "Succeed to read camera parameters from xml file" << std::endl;
199 }
200 else {
201 std::cout << "Unable to read camera parameters from xml file" << std::endl;
202 }
203 }
204#endif
205
206 std::cout << "Square width : " << opt_square_width << std::endl;
207 std::cout << cam << std::endl;
208
209 // The pose container
211
212 std::vector<vpDot2> dot(4);
213 std::vector<vpPoint> point; // 3D coordinates of the points
214 std::vector<vpImagePoint> ip; // 2D coordinates of the points in pixels
215 double L = opt_square_width / 2.;
216 point.push_back(vpPoint(-L, -L, 0));
217 point.push_back(vpPoint(L, -L, 0));
218 point.push_back(vpPoint(L, L, 0));
219 point.push_back(vpPoint(-L, L, 0));
220
221#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
223#else
225#endif
226
227 bool quit = false;
228 bool apply_cv = false; // apply computer vision
229 bool init_cv = true; // initialize tracking and pose computation
230
231 while (!quit) {
232 double t_begin = vpTime::measureTimeMs();
233 // Image Acquisition
234#if defined(VISP_HAVE_V4L2) || defined(VISP_HAVE_DC1394) || defined(VISP_HAVE_CMU1394) || \
235 defined(VISP_HAVE_FLYCAPTURE) || defined(VISP_HAVE_REALSENSE2)
236 g.acquire(I);
237#elif ((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI))|| ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO))
238 g >> frame;
239 vpImageConvert::convert(frame, I);
240#endif
242 if (apply_cv) {
243 try {
244 ip = track(I, dot, init_cv);
245 computePose(point, ip, cam, init_cv, cMo);
246 vpDisplay::displayFrame(I, cMo, cam, opt_square_width, vpColor::none, 3);
247 if (init_cv)
248 init_cv = false; // turn off the computer vision initialisation specific stuff
249
250 { // Display estimated pose in [m] and [deg]
251 vpPoseVector pose(cMo);
252 std::stringstream ss;
253 ss << "Translation: " << std::setprecision(5) << pose[0] << " " << pose[1] << " " << pose[2] << " [m]";
254 vpDisplay::displayText(I, 60, 20, ss.str(), vpColor::red);
255 ss.str(""); // erase ss
256 ss << "Rotation tu: " << std::setprecision(4) << vpMath::deg(pose[3]) << " " << vpMath::deg(pose[4]) << " "
257 << vpMath::deg(pose[5]) << " [deg]";
258 vpDisplay::displayText(I, 80, 20, ss.str(), vpColor::red);
259 }
260 }
261 catch (...) {
262 std::cout << "Computer vision failure." << std::endl;
263 apply_cv = false;
264 init_cv = true;
265 }
266 }
267 vpDisplay::displayText(I, 20, 20, "Right click: quit", vpColor::red);
268 if (apply_cv) {
269 vpDisplay::displayText(I, 40, 20, "Computer vision in progress...", vpColor::red);
270 }
271 else {
272 vpDisplay::displayText(I, 40, 20, "Left click : start", vpColor::red);
273 }
275 if (vpDisplay::getClick(I, button, false)) {
276 if (button == vpMouseButton::button3) {
277 quit = true;
278 }
279 else if (button == vpMouseButton::button1) {
280 apply_cv = true;
281 }
282 }
283 {
284 std::stringstream ss;
285 ss << "Time: " << vpTime::measureTimeMs() - t_begin << " ms";
286 vpDisplay::displayText(I, 20, I.getWidth() - 100, ss.str(), vpColor::red);
287 }
289 }
290 }
291 catch (const vpException &e) {
292 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
293 }
294#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
295 if (display != nullptr) {
296 delete display;
297 }
298#endif
299}
300
301#else
302
303int main()
304{
305 std::cout << "There are missing 3rd parties to run this tutorial" << std::endl;
306}
307
308#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.
@ 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
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
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
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
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.
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.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()