Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
photometricVisualServoing.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
36
37#include <visp3/core/vpConfig.h>
38#include <visp3/core/vpImage.h>
39#include <visp3/core/vpImageTools.h>
40#include <visp3/io/vpImageIo.h>
41
42#include <visp3/core/vpCameraParameters.h>
43#include <visp3/core/vpTime.h>
44#include <visp3/robot/vpSimulatorCamera.h>
45
46#include <visp3/core/vpHomogeneousMatrix.h>
47#include <visp3/core/vpMath.h>
48#include <visp3/gui/vpDisplayFactory.h>
49
50#include <visp3/io/vpParseArgv.h>
51#include <visp3/visual_features/vpFeatureLuminance.h>
52#include <visp3/vs/vpServo.h>
53
54#include <stdlib.h>
55#include <visp3/robot/vpImageSimulator.h>
56#define Z 1
57
58#include <visp3/core/vpIoTools.h>
59#include <visp3/io/vpParseArgv.h>
60
61// List of allowed command line options
62#define GETOPTARGS "cdi:n:h"
63
64#ifdef ENABLE_VISP_NAMESPACE
65using namespace VISP_NAMESPACE_NAME;
66#endif
67
68void usage(const char *name, const char *badparam, const std::string &ipath, int niter);
69bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, int &niter);
70
81void usage(const char *name, const char *badparam, const std::string &ipath, int niter)
82{
83 fprintf(stdout, "\n\
84Tracking of Surf key-points.\n\
85\n\
86SYNOPSIS\n\
87 %s [-i <input image path>] [-c] [-d] [-n <number of iterations>] [-h]\n",
88 name);
89
90 fprintf(stdout, "\n\
91OPTIONS: Default\n\
92 -i <input image path> %s\n\
93 Set image input path.\n\
94 From this path read \"doisneau/doisneau.jpg\"\n\
95 images. \n\
96 Setting the VISP_INPUT_IMAGE_PATH environment\n\
97 variable produces the same behaviour than using\n\
98 this option.\n\
99\n\
100 -c\n\
101 Disable the mouse click. Useful to automate the \n\
102 execution of this program without human intervention.\n\
103\n\
104 -d \n\
105 Turn off the display.\n\
106\n\
107 -n %%d %d\n\
108 Number of iterations.\n\
109\n\
110 -h\n\
111 Print the help.\n",
112 ipath.c_str(), niter);
113
114 if (badparam)
115 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
116}
131bool getOptions(int argc, const char **argv, std::string &ipath, bool &click_allowed, bool &display, int &niter)
132{
133 const char *optarg_;
134 int c;
135 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136
137 switch (c) {
138 case 'c':
139 click_allowed = false;
140 break;
141 case 'd':
142 display = false;
143 break;
144 case 'i':
145 ipath = optarg_;
146 break;
147 case 'n':
148 niter = atoi(optarg_);
149 break;
150 case 'h':
151 usage(argv[0], nullptr, ipath, niter);
152 return false;
153
154 default:
155 usage(argv[0], optarg_, ipath, niter);
156 return false;
157 }
158 }
159
160 if ((c == 1) || (c == -1)) {
161 // standalone param or error
162 usage(argv[0], nullptr, ipath, niter);
163 std::cerr << "ERROR: " << std::endl;
164 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
165 return false;
166 }
167
168 return true;
169}
170
171int main(int argc, const char **argv)
172{
173#if (defined(VISP_HAVE_LAPACK) || defined(VISP_HAVE_EIGEN3) || defined(VISP_HAVE_OPENCV))
174#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
175 std::shared_ptr<vpDisplay> d, d1;
176#else
177 vpDisplay *d = nullptr;
178 vpDisplay *d1 = nullptr;
179#endif
180 try {
181 std::string env_ipath;
182 std::string opt_ipath;
183 std::string ipath;
184 std::string filename;
185 bool opt_click_allowed = true;
186 bool opt_display = true;
187 int opt_niter = 400;
188
189 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
190 // environment variable value
192
193 // Set the default input path
194 if (!env_ipath.empty())
195 ipath = env_ipath;
196
197 // Read the command line options
198 if (getOptions(argc, argv, opt_ipath, opt_click_allowed, opt_display, opt_niter) == false) {
199 return EXIT_FAILURE;
200 }
201
202 // Get the option values
203 if (!opt_ipath.empty())
204 ipath = opt_ipath;
205
206 // Compare ipath and env_ipath. If they differ, we take into account
207 // the input path coming from the command line option
208 if (!opt_ipath.empty() && !env_ipath.empty()) {
209 if (ipath != env_ipath) {
210 std::cout << std::endl << "WARNING: " << std::endl;
211 std::cout << " Since -i <visp image path=" << ipath << "> "
212 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
213 << " we skip the environment variable." << std::endl;
214 }
215 }
216
217 // Test if an input path is set
218 if (opt_ipath.empty() && env_ipath.empty()) {
219 usage(argv[0], nullptr, ipath, opt_niter);
220 std::cerr << std::endl << "ERROR:" << std::endl;
221 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
222 << " environment variable to specify the location of the " << std::endl
223 << " image path where test images are located." << std::endl
224 << std::endl;
225 return EXIT_FAILURE;
226 }
227
228 vpImage<unsigned char> Itexture;
229 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
230 vpImageIo::read(Itexture, filename);
231
232 vpColVector X[4];
233 for (int i = 0; i < 4; i++)
234 X[i].resize(3);
235 // Top left corner
236 X[0][0] = -0.3;
237 X[0][1] = -0.215;
238 X[0][2] = 0;
239
240 // Top right corner
241 X[1][0] = 0.3;
242 X[1][1] = -0.215;
243 X[1][2] = 0;
244
245 // Bottom right corner
246 X[2][0] = 0.3;
247 X[2][1] = 0.215;
248 X[2][2] = 0;
249
250 // Bottom left corner
251 X[3][0] = -0.3;
252 X[3][1] = 0.215;
253 X[3][2] = 0;
254
256
258 sim.init(Itexture, X);
259
260 vpCameraParameters cam(870, 870, 160, 120);
261
262 // ----------------------------------------------------------
263 // Create the framegraber (here a simulated image)
264 vpImage<unsigned char> I(240, 320, 0);
266
267 // camera desired position
269 cdMo[2][3] = 1;
270
271 // set the robot at the desired position
272 sim.setCameraPosition(cdMo);
273 sim.getImage(I, cam); // and aquire the image Id
274 Id = I;
275
276#if defined(VISP_HAVE_DISPLAY)
277 if (opt_display) {
278#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
279 d = vpDisplayFactory::createDisplay(I, 20, 10, "Photometric visual servoing : s");
280#else
281 d = vpDisplayFactory::allocateDisplay(I, 20, 10, "Photometric visual servoing : s");
282#endif
285 }
286 if (opt_display && opt_click_allowed) {
287 std::cout << "Click in the image to continue..." << std::endl;
289 }
290#endif
291
292 // ----------------------------------------------------------
293 // position the robot at the initial position
294 // ----------------------------------------------------------
295
296 // camera desired position
298 cMo.buildFrom(0, 0, 1.2, vpMath::rad(15), vpMath::rad(-5), vpMath::rad(20));
299 vpHomogeneousMatrix wMo; // Set to identity
300 vpHomogeneousMatrix wMc; // Camera position in the world frame
301
302 // set the robot at the desired position
303 sim.setCameraPosition(cMo);
304 I = 0u;
305 sim.getImage(I, cam); // and aquire the image Id
306
307#if defined(VISP_HAVE_DISPLAY)
308 if (opt_display) {
311 }
312 if (opt_display && opt_click_allowed) {
313 std::cout << "Click in the image to continue..." << std::endl;
315 }
316#endif
317
319 Idiff = I;
320
321 vpImageTools::imageDifference(I, Id, Idiff);
322
323 // Affiche de l'image de difference
324#if defined(VISP_HAVE_DISPLAY)
325 if (opt_display) {
326#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
327 d1 = vpDisplayFactory::createDisplay(Idiff, 40 + static_cast<int>(I.getWidth()), 10, "photometric visual servoing : s-s* ");
328#else
329 d1 = vpDisplayFactory::allocateDisplay(Idiff, 40 + static_cast<int>(I.getWidth()), 10, "photometric visual servoing : s-s* ");
330#endif
331 vpDisplay::display(Idiff);
332 vpDisplay::flush(Idiff);
333 }
334#endif
335 // create the robot (here a simulated free flying camera)
336 vpSimulatorCamera robot;
337 robot.setSamplingTime(0.04);
338 wMc = wMo * cMo.inverse();
339 robot.setPosition(wMc);
340
341 // ------------------------------------------------------
342 // Visual feature, interaction matrix, error
343 // s, Ls, Lsd, Lt, Lp, etc
344 // ------------------------------------------------------
345
346 // current visual feature built from the image
347 // (actually, this is the image...)
349 sI.init(I.getHeight(), I.getWidth(), Z);
350 sI.setCameraParameters(cam);
351 sI.buildFrom(I);
352
353 // desired visual feature built from the image
355 sId.init(I.getHeight(), I.getWidth(), Z);
356 sId.setCameraParameters(cam);
357 sId.buildFrom(Id);
358
359 // Create visual-servoing task
360 vpServo servo;
361 // define the task
362 // - we want an eye-in-hand control law
363 // - robot is controlled in the camera frame
365 // add current and desired visual features
366 servo.addFeature(sI, sId);
367 // set the gain
368 servo.setLambda(30);
369 // compute interaction matrix at the desired position
371
372 // set a velocity control mode
373 robot.setRobotState(vpRobot::STATE_VELOCITY_CONTROL);
374
375 int iter = 1;
376 double normError = 0;
377 vpColVector v; // camera velocity send to the robot
378
379 vpChrono chrono;
380 chrono.start();
381 do {
382 std::cout << "--------------------------------------------" << iter++ << std::endl;
383
384 // Acquire the new image
385 sim.setCameraPosition(cMo);
386 sim.getImage(I, cam);
387#if defined(VISP_HAVE_DISPLAY)
388 if (opt_display) {
391 }
392#endif
393 vpImageTools::imageDifference(I, Id, Idiff);
394#if defined(VISP_HAVE_DISPLAY)
395 if (opt_display) {
396 vpDisplay::display(Idiff);
397 vpDisplay::flush(Idiff);
398 }
399#endif
400 // Compute current visual feature
401 sI.buildFrom(I);
402
403 v = servo.computeControlLaw(); // camera velocity send to the robot
404
405 normError = servo.getError().sumSquare();
406 std::cout << " |e| = " << normError << std::endl;
407 std::cout << " |v| = " << sqrt(v.sumSquare()) << std::endl;
408
409 // send the robot velocity
410 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
411 wMc = robot.getPosition();
412 cMo = wMc.inverse() * wMo;
413 } while (normError > 10000 && iter < opt_niter);
414
415 chrono.stop();
416 std::cout << "Time to convergence: " << chrono.getDurationMs() << " ms" << std::endl;
417
418 v = 0;
419 robot.setVelocity(vpRobot::CAMERA_FRAME, v);
420
421#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
422 if (d != nullptr) {
423 delete d;
424 }
425 if (d1 != nullptr) {
426 delete d1;
427 }
428#endif
429 return EXIT_SUCCESS;
430 }
431 catch (const vpException &e) {
432 std::cout << "Catch an exception: " << e << std::endl;
433#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
434 if (d != nullptr) {
435 delete d;
436 }
437 if (d1 != nullptr) {
438 delete d1;
439 }
440#endif
441 return EXIT_FAILURE;
442 }
443#else
444 (void)argc;
445 (void)argv;
446 std::cout << "Cannot run this example: install Lapack, Eigen3 or OpenCV" << std::endl;
447 return EXIT_SUCCESS;
448#endif
449}
Generic class defining intrinsic camera parameters.
void start(bool reset=true)
Definition vpTime.cpp:411
void stop()
Definition vpTime.cpp:426
double getDurationMs()
Definition vpTime.cpp:400
Implementation of column vector and the associated operations.
double sumSquare() const
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 flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition vpException.h:60
Class that defines the image luminance visual feature.
vpFeatureLuminance & buildFrom(vpImage< unsigned char > &I)
void init(unsigned int _nbr, unsigned int _nbc, double _Z)
void setCameraParameters(const vpCameraParameters &_cam)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
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 setInterpolationType(const vpInterpolationType interplt)
void setCameraPosition(const vpHomogeneousMatrix &cMt)
static void imageDifference(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Idiff)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static double rad(double deg)
Definition vpMath.h:129
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
virtual void setSamplingTime(const double &delta_t)
@ CAMERA_FRAME
Definition vpRobot.h:81
@ STATE_VELOCITY_CONTROL
Initialize the velocity controller.
Definition vpRobot.h:64
void setInteractionMatrixType(const vpServoIteractionMatrixType &interactionMatrixType, const vpServoInversionType &interactionMatrixInversion=PSEUDO_INVERSE)
Definition vpServo.cpp:380
@ EYEINHAND_CAMERA
Definition vpServo.h:176
void addFeature(vpBasicFeature &s_cur, vpBasicFeature &s_star, unsigned int select=vpBasicFeature::FEATURE_ALL)
Definition vpServo.cpp:331
void setLambda(double c)
Definition vpServo.h:1006
void setServo(const vpServoType &servo_type)
Definition vpServo.cpp:134
vpColVector getError() const
Definition vpServo.h:530
vpColVector computeControlLaw()
Definition vpServo.cpp:699
@ CURRENT
Definition vpServo.h:217
Class that defines the simplest robot: a free flying camera.
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.