Visual Servoing Platform  version 3.6.1 under development (2024-05-26)

Test keypoint matching with mostly OpenCV functions calls to detect potential memory leaks in testKeyPoint.cpp.

* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2023 by Inria. All rights reserved.
* This software is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file LICENSE.txt at the root directory of this source
* distribution for additional information about the GNU GPL.
* For using ViSP with software that can not be combined with the GNU
* GPL, please contact Inria about acquiring a ViSP Professional
* Edition License.
* See for more information.
* This software was developed at:
* Inria Rennes - Bretagne Atlantique
* Campus Universitaire de Beaulieu
* 35042 Rennes Cedex
* France
* If you have questions regarding the use of this file, please contact
* Inria at
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* Description:
* Test keypoint matching with mostly OpenCV functions calls
* to detect potential memory leaks in testKeyPoint.cpp.
#include <iostream>
#include <visp3/core/vpConfig.h>
#include <opencv2/features2d/features2d.hpp>
#include <visp3/core/vpImage.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayOpenCV.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpImageIo.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/io/vpVideoReader.h>
// List of allowed command line options
#define GETOPTARGS "cdh"
void usage(const char *name, const char *badparam);
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display);
void usage(const char *name, const char *badparam)
fprintf(stdout, "\n\
Test keypoints matching.\n\
%s [-c] [-d] [-h]\n",
fprintf(stdout, "\n\
Disable the mouse click. Useful to automate the \n\
execution of this program without human intervention.\n\
-d \n\
Turn off the display.\n\
Print the help.\n");
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
bool getOptions(int argc, const char **argv, bool &click_allowed, bool &display)
const char *optarg_;
int c;
while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
switch (c) {
case 'c':
click_allowed = false;
case 'd':
display = false;
case 'h':
usage(argv[0], nullptr);
return false;
usage(argv[0], optarg_);
return false;
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], nullptr);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
return false;
return true;
template <typename Type>
void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iref,
vpImage<Type> &Icur, vpImage<Type> &Imatch)
std::string ext("png");
std::string ext("pgm");
// Set the path location of the image sequence
std::string dirname = vpIoTools::createFilePath(env_ipath, "mbt/cube");
// Build the name of the image files
std::string filenameRef = vpIoTools::createFilePath(dirname, "image0000." + ext);
vpImageIo::read(Iref, filenameRef);
std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d." + ext);
// Init keypoints
cv::Ptr<cv::FeatureDetector> detector;
cv::Ptr<cv::DescriptorExtractor> extractor;
cv::Ptr<cv::DescriptorMatcher> matcher;
#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
detector = cv::ORB::create();
extractor = cv::ORB::create();
detector = cv::FeatureDetector::create("ORB");
extractor = cv::DescriptorExtractor::create("ORB");
matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
std::vector<cv::KeyPoint> trainKeyPoints;
cv::Mat matImg, trainDescriptors;
vpImageConvert::convert(Iref, matImg);
detector->detect(matImg, trainKeyPoints);
extractor->compute(matImg, trainKeyPoints, trainDescriptors);
Imatch.resize(Icur.getHeight(), 2 * Icur.getWidth());
Imatch.insert(Iref, vpImagePoint(0, 0));
#if defined(VISP_HAVE_X11)
#elif defined(VISP_HAVE_GTK)
#elif defined(VISP_HAVE_GDI)
#elif defined(HAVE_OPENCV_HIGHGUI)
if (opt_display) {
display.init(Imatch, 0, 0, "ORB keypoints matching");
bool opt_click = false;
while (!g.end()) {
Imatch.insert(Icur, vpImagePoint(0, Icur.getWidth()));
if (opt_display) {
vpImageConvert::convert(Icur, matImg);
std::vector<cv::KeyPoint> queryKeyPoints;
detector->detect(matImg, queryKeyPoints);
cv::Mat queryDescriptors;
extractor->compute(matImg, queryKeyPoints, queryDescriptors);
std::vector<std::vector<cv::DMatch> > knn_matches;
std::vector<cv::DMatch> matches;
matcher->knnMatch(queryDescriptors, trainDescriptors, knn_matches, 2);
for (std::vector<std::vector<cv::DMatch> >::const_iterator it = knn_matches.begin(); it != knn_matches.end();
++it) {
if (it->size() > 1) {
double ratio = (*it)[0].distance / (*it)[1].distance;
if (ratio < 0.85) {
if (opt_display) {
for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
vpImagePoint leftPt(trainKeyPoints[(size_t)it->trainIdx].pt.y, trainKeyPoints[(size_t)it->trainIdx].pt.x);
vpImagePoint rightPt(queryKeyPoints[(size_t)it->queryIdx].pt.y,
queryKeyPoints[(size_t)it->queryIdx].pt.x + Iref.getWidth());
vpDisplay::displayLine(Imatch, leftPt, rightPt, vpColor::green);
// Click requested to process next image
if (opt_click_allowed && opt_display) {
if (opt_click) {
vpDisplay::getClick(Imatch, button, true);
if (button == vpMouseButton::button3) {
opt_click = false;
} else {
// Use right click to enable/disable step by step tracking
if (vpDisplay::getClick(Imatch, button, false)) {
if (button == vpMouseButton::button3) {
opt_click = true;
} else if (button == vpMouseButton::button1) {
int main(int argc, const char **argv)
try {
std::string env_ipath;
bool opt_click_allowed = true;
bool opt_display = true;
// Read the command line options
if (getOptions(argc, argv, opt_click_allowed, opt_display) == false) {
// Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
// environment variable value
if (env_ipath.empty()) {
std::cerr << "Please set the VISP_INPUT_IMAGE_PATH environment "
"variable value."
<< std::endl;
vpImage<unsigned char> Iref, Icur, Imatch;
std::cout << "-- Test on gray level images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
vpImage<vpRGBa> Iref, Icur, Imatch;
std::cout << "-- Test on color images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, Iref, Icur, Imatch);
} catch (const vpException &e) {
std::cerr << e.what() << std::endl;
std::cout << "testKeyPoint-3 is ok !" << std::endl;
int main()
std::cerr << "You need OpenCV library." << std::endl;
static const vpColor green
Definition: vpColor.h:214
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:128
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:128
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 flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.h:179
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * what() const
Definition: vpException.cpp:70
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)
Definition: vpImageIo.cpp:143
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Definition of the vpImage class member functions.
Definition: vpImage.h:69
unsigned int getWidth() const
Definition: vpImage.h:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:798
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1609
unsigned int getHeight() const
Definition: vpImage.h:184
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1834
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2197
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void display(vpImage< unsigned char > &I, const std::string &title)
Display a gray-scale image.