Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testKeyPoint-4.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Description:
31 * Test keypoint matching and pose estimation with mostly OpenCV functions
32 * calls to detect potential memory leaks in testKeyPoint-2.cpp.
33 */
34
41
42#include <iostream>
43
44#include <visp3/core/vpConfig.h>
45
46#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && \
47 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
48 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES)))
49
50#include <visp3/core/vpHomogeneousMatrix.h>
51#include <visp3/core/vpImage.h>
52#include <visp3/core/vpIoTools.h>
53#include <visp3/gui/vpDisplayFactory.h>
54#include <visp3/io/vpImageIo.h>
55#include <visp3/io/vpParseArgv.h>
56#include <visp3/io/vpVideoReader.h>
57#include <visp3/mbt/vpMbEdgeTracker.h>
58#include <visp3/vision/vpKeyPoint.h>
59
60// List of allowed command line options
61#define GETOPTARGS "cdh"
62
63#ifdef ENABLE_VISP_NAMESPACE
64using namespace VISP_NAMESPACE_NAME;
65#endif
66
67void usage(const char *name, const char *badparam);
68bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
69
78void usage(const char *name, const char *badparam)
79{
80 fprintf(stdout, "\n\
81Test keypoints matching.\n\
82\n\
83SYNOPSIS\n\
84 %s [-c] [-d] [-h]\n",
85 name);
86
87 fprintf(stdout, "\n\
88OPTIONS: \n\
89\n\
90 -c\n\
91 Disable the mouse click. Useful to automate the \n\
92 execution of this program without human intervention.\n\
93\n\
94 -d \n\
95 Turn off the display.\n\
96\n\
97 -h\n\
98 Print the help.\n");
99
100 if (badparam)
101 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
102}
103
115bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
116{
117 const char *optarg_;
118 int c;
119 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120
121 switch (c) {
122 case 'c':
123 click_allowed = false;
124 break;
125 case 'd':
126 display = false;
127 break;
128 case 'h':
129 usage(argv[0], nullptr);
130 return false;
131
132 default:
133 usage(argv[0], optarg_);
134 return false;
135 }
136 }
137
138 if ((c == 1) || (c == -1)) {
139 // standalone param or error
140 usage(argv[0], nullptr);
141 std::cerr << "ERROR: " << std::endl;
142 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
143 return false;
144 }
145
146 return true;
147}
148
149template <typename Type>
150void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &I,
151 vpImage<Type> &Imatch, vpImage<Type> &Iref)
152{
153#if defined(VISP_HAVE_DATASET)
154#if VISP_HAVE_DATASET_VERSION >= 0x030600
155 std::string ext("png");
156#else
157 std::string ext("pgm");
158#endif
159#else
160 // We suppose that the user will download a recent dataset
161 std::string ext("png");
162#endif
163 // Set the path location of the image sequence
164 std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
165
166 // Build the name of the image files
167 std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
168 vpImageIo::read(I, filenameRef);
169 Iref = I;
170 std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
171
172 vpDisplay *display = nullptr, *display2 = nullptr;
173
174 if (opt_display) {
175 Imatch.resize(I.getHeight(), 2 * I.getWidth());
176 Imatch.insert(I, vpImagePoint(0, 0));
177
178#ifdef VISP_HAVE_DISPLAY
179 display = vpDisplayFactory::allocateDisplay(I, 0, 0, "ORB keypoints matching");
180 display->setDownScalingFactor(vpDisplay::SCALE_AUTO);
181 display2 = vpDisplayFactory::allocateDisplay(Imatch, 0, static_cast<int>(I.getHeight()) / vpDisplay::getDownScalingFactor(I) + 40, "ORB keypoints matching");
182 display2->setDownScalingFactor(vpDisplay::SCALE_AUTO);
183#else
184 std::cout << "No image viewer is available..." << std::endl;
185#endif
186 }
187
190 // Load config for tracker
191 std::string tracker_config_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.xml");
192
193#if defined(VISP_HAVE_PUGIXML)
194 tracker.loadConfigFile(tracker_config_file);
195 tracker.getCameraParameters(cam);
196#else
197 // Corresponding parameters manually set to have an example code
198 vpMe me;
199 me.setMaskSize(5);
200 me.setMaskNumber(180);
201 me.setRange(8);
203 me.setThreshold(20);
204 me.setMu1(0.5);
205 me.setMu2(0.5);
206 me.setSampleStep(4);
207 me.setNbTotalSample(250);
208 tracker.setMovingEdge(me);
209 cam.initPersProjWithoutDistortion(547.7367575, 542.0744058, 338.7036994, 234.5083345);
210 tracker.setCameraParameters(cam);
211 tracker.setNearClippingDistance(0.01);
212 tracker.setFarClippingDistance(100.0);
213 tracker.setClipping(tracker.getClipping() | vpMbtPolygon::FOV_CLIPPING);
214#endif
215
216 tracker.setAngleAppear(vpMath::rad(89));
217 tracker.setAngleDisappear(vpMath::rad(89));
218
219 // Load CAO model
220 std::string cao_model_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.cao");
221 tracker.loadModel(cao_model_file);
222
223 // Initialize the pose
224 std::string init_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.init");
225 if (opt_display && opt_click_allowed) {
226 tracker.initClick(I, init_file);
227 }
228 else {
229 vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
230 tracker.initFromPose(I, cMoi);
231 }
232
233 // Get the init pose
235 tracker.getPose(cMo);
236
237 // Init keypoints
238 cv::Ptr<cv::FeatureDetector> detector;
239 cv::Ptr<cv::DescriptorExtractor> extractor;
240 cv::Ptr<cv::DescriptorMatcher> matcher;
241
242#if defined(VISP_HAVE_OPENCV) && \
243 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
244 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
245#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
246 detector = cv::ORB::create(500, 1.2f, 1);
247 extractor = cv::ORB::create(500, 1.2f, 1);
248#elif (VISP_HAVE_OPENCV_VERSION >= 0x020301)
249 detector = cv::FeatureDetector::create("ORB");
250 extractor = cv::DescriptorExtractor::create("ORB");
251#endif
252#endif
253 matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
254
255 // Detect keypoints on the current image
256 std::vector<cv::KeyPoint> trainKeyPoints;
257 cv::Mat matImg;
258 vpImageConvert::convert(I, matImg);
259 detector->detect(matImg, trainKeyPoints);
260
261 // Keep only keypoints on the cube
262 std::vector<vpPolygon> polygons;
263 std::vector<std::vector<vpPoint> > roisPt;
264 std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
265 polygons = pair.first;
266 roisPt = pair.second;
267
268 // Compute the 3D coordinates
269 std::vector<cv::Point3f> points3f;
270 vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
271
272 // Extract descriptors
273 cv::Mat trainDescriptors;
274 extractor->compute(matImg, trainKeyPoints, trainDescriptors);
275
276 if (trainKeyPoints.size() != static_cast<size_t>(trainDescriptors.rows) || trainKeyPoints.size() != points3f.size()) {
277 throw(vpException(vpException::fatalError, "Problem with training data size !"));
278 }
279
280 // Init reader for getting the input image sequence
282 g.setFileName(filenameCur);
283 g.open(I);
284 g.acquire(I);
285
286 bool opt_click = false;
288 while (g.getFrameIndex() < 30) {
289 g.acquire(I);
290
291 vpImageConvert::convert(I, matImg);
292 std::vector<cv::KeyPoint> queryKeyPoints;
293 detector->detect(matImg, queryKeyPoints);
294
295 cv::Mat queryDescriptors;
296 extractor->compute(matImg, queryKeyPoints, queryDescriptors);
297
298 std::vector<std::vector<cv::DMatch> > knn_matches;
299 std::vector<cv::DMatch> matches;
300 matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
301 for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
302 ++it) {
303 if (it->size() > 1) {
304 double ratio = (*it)[0].distance / (*it)[1].distance;
305 if (ratio < 0.85) {
306 matches.push_back((*it)[0]);
307 }
308 }
309 }
310
311 vpPose estimated_pose;
312 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
313 vpPoint pt(points3f[static_cast<size_t>(it->trainIdx)].x, points3f[static_cast<size_t>(it->trainIdx)].y,
314 points3f[static_cast<size_t>(it->trainIdx)].z);
315
316 double x = 0.0, y = 0.0;
317 vpPixelMeterConversion::convertPoint(cam, queryKeyPoints[static_cast<size_t>(it->queryIdx)].pt.x,
318 queryKeyPoints[static_cast<size_t>(it->queryIdx)].pt.y, x, y);
319 pt.set_x(x);
320 pt.set_y(y);
321
322 estimated_pose.addPoint(pt);
323 }
324
325 bool is_pose_estimated = false;
326 if (estimated_pose.npt >= 4) {
327 try {
328 unsigned int nb_inliers = static_cast<unsigned int>(0.7 * estimated_pose.npt);
329 estimated_pose.setRansacNbInliersToReachConsensus(nb_inliers);
330 estimated_pose.setRansacThreshold(0.001);
331 estimated_pose.setRansacMaxTrials(500);
332 if (estimated_pose.computePose(vpPose::RANSAC, cMo)) {
333 is_pose_estimated = true; // success
334 }
335 else {
336 is_pose_estimated = false;
337 }
338 }
339 catch (...) {
340 is_pose_estimated = false;
341 }
342 }
343
344 if (opt_display) {
346
347 Imatch.insert(I, vpImagePoint(0, Iref.getWidth()));
348 vpDisplay::display(Imatch);
349 for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
350 vpImagePoint leftPt(trainKeyPoints[static_cast<size_t>(it->trainIdx)].pt.y, trainKeyPoints[static_cast<size_t>(it->trainIdx)].pt.x);
351 vpImagePoint rightPt(queryKeyPoints[static_cast<size_t>(it->queryIdx)].pt.y,
352 queryKeyPoints[static_cast<size_t>(it->queryIdx)].pt.x + Iref.getWidth());
353 vpDisplay::displayLine(Imatch, leftPt, rightPt, vpColor::green);
354 }
355
356 if (is_pose_estimated) {
357 tracker.setPose(I, cMo);
358 tracker.display(I, cMo, cam, vpColor::red);
359 vpDisplay::displayFrame(I, cMo, cam, 0.05, vpColor::none);
360 }
361
362 vpDisplay::flush(Imatch);
364 }
365
366 // Click requested to process next image
367 if (opt_click_allowed && opt_display) {
368 if (opt_click) {
369 vpDisplay::getClick(I, button, true);
370 if (button == vpMouseButton::button3) {
371 opt_click = false;
372 }
373 }
374 else {
375 // Use right click to enable/disable step by step tracking
376 if (vpDisplay::getClick(I, button, false)) {
377 if (button == vpMouseButton::button3) {
378 opt_click = true;
379 }
380 else if (button == vpMouseButton::button1) {
381 break;
382 }
383 }
384 }
385 }
386 }
387
388 if (display) {
389 delete display;
390 }
391 if (display2) {
392 delete display2;
393 }
394}
395
396int main(int argc, const char **argv)
397{
398 try {
399 std::string env_ipath;
400 bool opt_click_allowed = true;
401 bool opt_display = true;
402
403 // Read the command line options
404 if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
405 return EXIT_FAILURE;
406 }
407
408 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
409 // environment variable value
411
412 if (env_ipath.empty()) {
413 std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
414 "variable value."
415 << std::endl;
416 return EXIT_FAILURE;
417 }
418
419 {
420 vpImage<unsigned char> I, Imatch, Iref;
421
422 std::cout << "-- Test on gray level images" << std::endl;
423 run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
424 }
425
426 {
427 vpImage<vpRGBa> I, Imatch, Iref;
428
429 std::cout << "-- Test on color images" << std::endl;
430 run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
431 }
432
433 }
434 catch (const vpException &e) {
435 std::cerr << e.what() << std::endl;
436 return EXIT_FAILURE;
437 }
438
439 std::cout << "testKeyPoint-4 is ok !" << std::endl;
440 return EXIT_SUCCESS;
441}
442
443#else
444int main()
445{
446 std::cerr << "You need OpenCV library." << std::endl;
447
448 return EXIT_SUCCESS;
449}
450
451#endif
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition vpColor.h:198
static const vpColor none
Definition vpColor.h:210
static const vpColor green
Definition vpColor.h:201
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 displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
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)
unsigned int getDownScalingFactor()
Definition vpDisplay.h:218
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ fatalError
Fatal error.
Definition vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
Definition vpImage.h:131
unsigned int getWidth() const
Definition vpImage.h:242
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition vpImage.h:544
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition vpImage.h:639
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void compute3DForPointsInPolygons(const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, std::vector< cv::KeyPoint > &candidates, const std::vector< vpPolygon > &polygons, const std::vector< std::vector< vpPoint > > &roisPt, std::vector< cv::Point3f > &points, cv::Mat *descriptors=nullptr)
static double rad(double deg)
Definition vpMath.h:129
Make the complete tracking of an object by using its CAD model.
Definition vpMe.h:143
void setMu1(const double &mu_1)
Definition vpMe.h:408
void setRange(const unsigned int &range)
Definition vpMe.h:438
void setLikelihoodThresholdType(const vpLikelihoodThresholdType likelihood_threshold_type)
Definition vpMe.h:531
void setNbTotalSample(const int &ntotal_sample)
Definition vpMe.h:422
void setMaskNumber(const unsigned int &mask_number)
Definition vpMe.cpp:555
void setThreshold(const double &threshold)
Definition vpMe.h:489
void setSampleStep(const double &sample_step)
Definition vpMe.h:445
void setMaskSize(const unsigned int &mask_size)
Definition vpMe.cpp:563
void setMu2(const double &mu_2)
Definition vpMe.h:415
@ NORMALIZED_THRESHOLD
Definition vpMe.h:154
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition vpPose.h:82
void setRansacMaxTrials(const int &rM)
Definition vpPose.h:416
void addPoint(const vpPoint &P)
Definition vpPose.cpp:96
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition vpPose.h:397
@ RANSAC
Definition vpPose.h:92
unsigned int npt
Number of point used in pose computation.
Definition vpPose.h:118
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, FuncCheckValidityPose func=nullptr)
Definition vpPose.cpp:385
void setRansacThreshold(const double &t)
Definition vpPose.h:402
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
long getFrameIndex() const
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.