Visual Servoing Platform  version 3.2.0
testKeyPoint-3.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test keypoint matching with mostly OpenCV functions calls
33  * to detect potential memory leaks in testKeyPoint.cpp.
34  *
35  * Authors:
36  * Souriya Trinh
37  *
38  *****************************************************************************/
39 
40 #include <iostream>
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
45 
46 #include <opencv2/core/core.hpp>
47 #include <opencv2/features2d/features2d.hpp>
48 #include <visp3/core/vpImage.h>
49 #include <visp3/core/vpIoTools.h>
50 #include <visp3/gui/vpDisplayGDI.h>
51 #include <visp3/gui/vpDisplayGTK.h>
52 #include <visp3/gui/vpDisplayOpenCV.h>
53 #include <visp3/gui/vpDisplayX.h>
54 #include <visp3/io/vpImageIo.h>
55 #include <visp3/io/vpParseArgv.h>
56 #include <visp3/io/vpVideoReader.h>
57 
58 // List of allowed command line options
59 #define GETOPTARGS "cdh"
60 
61 void usage(const char *name, const char *badparam);
62 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
63 
71 void usage(const char *name, const char *badparam)
72 {
73  fprintf(stdout, "\n\
74 Test keypoints matching.\n\
75 \n\
76 SYNOPSIS\n\
77  %s [-c] [-d] [-h]\n", name);
78 
79  fprintf(stdout, "\n\
80 OPTIONS: \n\
81 \n\
82  -c\n\
83  Disable the mouse click. Useful to automate the \n\
84  execution of this program without human intervention.\n\
85 \n\
86  -d \n\
87  Turn off the display.\n\
88 \n\
89  -h\n\
90  Print the help.\n");
91 
92  if (badparam)
93  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
94 }
95 
107 bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
108 {
109  const char *optarg_;
110  int c;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
112 
113  switch (c) {
114  case 'c':
115  click_allowed = false;
116  break;
117  case 'd':
118  display = false;
119  break;
120  case 'h':
121  usage(argv[0], NULL);
122  return false;
123  break;
124 
125  default:
126  usage(argv[0], optarg_);
127  return false;
128  break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
149 int main(int argc, const char **argv)
150 {
151  try {
152  std::string env_ipath;
153  bool opt_click_allowed = true;
154  bool opt_display = true;
155 
156  // Read the command line options
157  if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
158  exit(-1);
159  }
160 
161  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
162  // environment variable value
163  env_ipath = vpIoTools::getViSPImagesDataPath();
164 
165  if (env_ipath.empty()) {
166  std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
167  "variable value."
168  << std::endl;
169  return -1;
170  }
171 
172  vpImage<unsigned char> Iref, Icur, Imatch;
173 
174  // Set the path location of the image sequence
175  std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
176 
177  // Build the name of the image files
178  std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000.pgm");
179  vpImageIo::read(Iref, filenameRef);
180  std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
181 
182  // Init keypoints
183  cv::Ptr<cv::FeatureDetector> detector;
184  cv::Ptr<cv::DescriptorExtractor> extractor;
185  cv::Ptr<cv::DescriptorMatcher> matcher;
186 
187 #if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
188  detector = cv::ORB::create();
189  extractor = cv::ORB::create();
190 #else
191  detector = cv::FeatureDetector::create("ORB");
192  extractor = cv::DescriptorExtractor::create("ORB");
193 #endif
194  matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
195 
196  std::vector<cv::KeyPoint> trainKeyPoints;
197  cv::Mat matImg, trainDescriptors;
198  vpImageConvert::convert(Iref, matImg);
199  detector->detect(matImg, trainKeyPoints);
200  extractor->compute(matImg, trainKeyPoints, trainDescriptors);
201 
202  vpVideoReader g;
203  g.setFileName(filenameCur);
204  g.open(Icur);
205  g.acquire(Icur);
206 
207  Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
208  Imatch.insert(Iref, vpImagePoint(0, 0));
209 
210 #if defined VISP_HAVE_X11
211  vpDisplayX display;
212 #elif defined VISP_HAVE_GTK
213  vpDisplayGTK display;
214 #elif defined VISP_HAVE_GDI
215  vpDisplayGDI display;
216 #else
217  vpDisplayOpenCV display;
218 #endif
219 
220  if (opt_display) {
221  display.setDownScalingFactor(vpDisplay::SCALE_AUTO);
222  display.init(Imatch, 0, 0, "ORB keypoints matching");
223  }
224 
225  bool opt_click = false;
227  while (!g.end()) {
228  g.acquire(Icur);
229  Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
230 
231  if (opt_display) {
232  vpDisplay::display(Imatch);
233  }
234 
235  vpImageConvert::convert(Icur, matImg);
236  std::vector<cv::KeyPoint> queryKeyPoints;
237  detector->detect(matImg, queryKeyPoints);
238 
239  cv::Mat queryDescriptors;
240  extractor->compute(matImg, queryKeyPoints, queryDescriptors);
241 
242  std::vector<std::vector<cv::DMatch> > knn_matches;
243  std::vector<cv::DMatch> matches;
244  matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
245  for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
246  ++it) {
247  if (it->size() > 1) {
248  double ratio = (*it)[0].distance / (*it)[1].distance;
249  if (ratio < 0.85) {
250  matches.push_back((*it)[0]);
251  }
252  }
253  }
254 
255  if (opt_display) {
256  for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
257  vpImagePoint leftPt(trainKeyPoints[(size_t)it->trainIdx].pt.y, trainKeyPoints[(size_t)it->trainIdx].pt.x);
258  vpImagePoint rightPt(queryKeyPoints[(size_t)it->queryIdx].pt.y,
259  queryKeyPoints[(size_t)it->queryIdx].pt.x + Iref.getWidth());
260  vpDisplay::displayLine(Imatch, leftPt, rightPt, vpColor::green);
261  }
262 
263  vpDisplay::flush(Imatch);
264  }
265 
266  // Click requested to process next image
267  if (opt_click_allowed && opt_display) {
268  if (opt_click) {
269  vpDisplay::getClick(Imatch, button, true);
270  if (button == vpMouseButton::button3) {
271  opt_click = false;
272  }
273  } else {
274  // Use right click to enable/disable step by step tracking
275  if (vpDisplay::getClick(Imatch, button, false)) {
276  if (button == vpMouseButton::button3) {
277  opt_click = true;
278  } else if (button == vpMouseButton::button1) {
279  break;
280  }
281  }
282  }
283  }
284  }
285 
286  } catch (const vpException &e) {
287  std::cerr << e.what() << std::endl;
288  return -1;
289  }
290 
291  std::cout << "testKeyPoint-3 is ok !" << std::endl;
292  return 0;
293 }
294 #else
295 int main()
296 {
297  std::cerr << "You need OpenCV library." << std::endl;
298 
299  return 0;
300 }
301 
302 #endif
vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:150
vpIoTools::getViSPImagesDataPath
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1317
vpImageConvert::convert
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition: vpImageConvert.cpp:78
vpMouseButton::button1
Definition: vpMouseButton.h:52
vpDisplay::SCALE_AUTO
Definition: vpDisplay.h:176
vpImageIo::read
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:206
vpVideoReader::end
bool end()
Definition: vpVideoReader.h:257
vpDisplayGDI
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
vpImage::insert
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1187
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:141
vpVideoReader::open
void open(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:196
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:68
vpVideoReader
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
Definition: vpVideoReader.h:170
vpColor::green
static const vpColor green
Definition: vpColor.h:182
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:676
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:137
vpException::what
const char * what() const
Definition: vpException.cpp:101
vpImage::getHeight
unsigned int getHeight() const
Definition: vpImage.h:177
vpIoTools::createFilePath
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1542
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
vpMouseButton::button3
Definition: vpMouseButton.h:54
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:652
vpImage< unsigned char >
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:701
vpDisplay::displayLine
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
Definition: vpDisplay_uchar.cpp:391
vpMouseButton::vpMouseButtonType
vpMouseButtonType
Definition: vpMouseButton.h:51
vpImage::resize
void resize(const unsigned int h, const unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:865
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:70
vpVideoReader::acquire
void acquire(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:265
vpVideoReader::setFileName
void setFileName(const char *filename)
Definition: vpVideoReader.cpp:91
vpImage::getWidth
unsigned int getWidth() const
Definition: vpImage.h:238