Visual Servoing Platform  version 3.3.0
testKeyPoint-4.cpp

Test keypoint matching and pose estimation with mostly OpenCV functions calls to detect potential memory leaks in testKeyPoint-2.cpp.

/****************************************************************************
*
* ViSP, open source Visual Servoing Platform software.
* Copyright (C) 2005 - 2019 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 http://visp.inria.fr 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 visp@inria.fr
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Description:
* Test keypoint matching and pose estimation with mostly OpenCV functions
*calls to detect potential memory leaks in testKeyPoint-2.cpp.
*
* Authors:
* Souriya Trinh
*
*****************************************************************************/
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <visp3/core/vpHomogeneousMatrix.h>
#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>
#include <visp3/mbt/vpMbEdgeTracker.h>
#include <visp3/vision/vpKeyPoint.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\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n", name);
fprintf(stdout, "\n\
OPTIONS: \n\
\n\
-c\n\
Disable the mouse click. Useful to automate the \n\
execution of this program without human intervention.\n\
\n\
-d \n\
Turn off the display.\n\
\n\
-h\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;
break;
case 'd':
display = false;
break;
case 'h':
usage(argv[0], NULL);
return false;
break;
default:
usage(argv[0], optarg_);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
// standalone param or error
usage(argv[0], NULL);
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,
{
// 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.pgm");
vpImageIo::read(I, filenameRef);
Iref = I;
std::string filenameCur = vpIoTools::createFilePath(dirname, "image%04d.pgm");
#if defined VISP_HAVE_X11
vpDisplayX display, display2;
#elif defined VISP_HAVE_GTK
vpDisplayGTK display, display2;
#elif defined VISP_HAVE_GDI
vpDisplayGDI display, display2;
#else
vpDisplayOpenCV display, display2;
#endif
if (opt_display) {
display.setDownScalingFactor(vpDisplay::SCALE_AUTO);
display.init(I, 0, 0, "ORB keypoints matching");
Imatch.resize(I.getHeight(), 2 * I.getWidth());
Imatch.insert(I, vpImagePoint(0, 0));
display2.init(Imatch, 0, (int)I.getHeight() / vpDisplay::getDownScalingFactor(I) + 70, "ORB keypoints matching");
}
vpMbEdgeTracker tracker;
// Load config for tracker
std::string tracker_config_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.xml");
#ifdef VISP_HAVE_PUGIXML
tracker.loadConfigFile(tracker_config_file);
tracker.getCameraParameters(cam);
#else
vpMe me;
me.setMaskSize(5);
me.setMaskNumber(180);
me.setRange(8);
me.setThreshold(10000);
me.setMu1(0.5);
me.setMu2(0.5);
tracker.setMovingEdge(me);
cam.initPersProjWithoutDistortion(547.7367575, 542.0744058, 338.7036994, 234.5083345);
tracker.setCameraParameters(cam);
tracker.setNearClippingDistance(0.01);
tracker.setFarClippingDistance(100.0);
#endif
// Load CAO model
std::string cao_model_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.cao");
tracker.loadModel(cao_model_file);
// Initialize the pose
std::string init_file = vpIoTools::createFilePath(env_ipath, "mbt/cube.init");
if (opt_display && opt_click_allowed) {
tracker.initClick(I, init_file);
} else {
vpHomogeneousMatrix cMoi(0.02044769891, 0.1101505452, 0.5078963719, 2.063603907, 1.110231561, -0.4392789872);
tracker.initFromPose(I, cMoi);
}
// Get the init pose
tracker.getPose(cMo);
// 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(500, 1.2f, 1);
extractor = cv::ORB::create(500, 1.2f, 1);
#elif (VISP_HAVE_OPENCV_VERSION >= 0x020301)
detector = cv::FeatureDetector::create("ORB");
extractor = cv::DescriptorExtractor::create("ORB");
#endif
matcher = cv::DescriptorMatcher::create("BruteForce-Hamming");
#if (VISP_HAVE_OPENCV_VERSION >= 0x020400 && VISP_HAVE_OPENCV_VERSION < 0x030000)
detector->set("nLevels", 1);
#endif
// Detect keypoints on the current image
std::vector<cv::KeyPoint> trainKeyPoints;
cv::Mat matImg;
detector->detect(matImg, trainKeyPoints);
// Keep only keypoints on the cube
std::vector<vpPolygon> polygons;
std::vector<std::vector<vpPoint> > roisPt;
std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > pair = tracker.getPolygonFaces(false);
polygons = pair.first;
roisPt = pair.second;
// Compute the 3D coordinates
std::vector<cv::Point3f> points3f;
vpKeyPoint::compute3DForPointsInPolygons(cMo, cam, trainKeyPoints, polygons, roisPt, points3f);
// Extract descriptors
cv::Mat trainDescriptors;
extractor->compute(matImg, trainKeyPoints, trainDescriptors);
if (trainKeyPoints.size() != (size_t)trainDescriptors.rows || trainKeyPoints.size() != points3f.size()) {
throw(vpException(vpException::fatalError, "Problem with training data size !"));
}
// Init reader for getting the input image sequence
g.setFileName(filenameCur);
g.open(I);
g.acquire(I);
bool opt_click = false;
while ((opt_display && !g.end()) || (!opt_display && g.getFrameIndex() < 30)) {
g.acquire(I);
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) {
matches.push_back((*it)[0]);
}
}
}
vpPose estimated_pose;
for (std::vector<cv::DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it) {
vpPoint pt(points3f[(size_t)(it->trainIdx)].x, points3f[(size_t)(it->trainIdx)].y,
points3f[(size_t)(it->trainIdx)].z);
double x = 0.0, y = 0.0;
vpPixelMeterConversion::convertPoint(cam, queryKeyPoints[(size_t)(it->queryIdx)].pt.x,
queryKeyPoints[(size_t)(it->queryIdx)].pt.y, x, y);
pt.set_x(x);
pt.set_y(y);
estimated_pose.addPoint(pt);
}
bool is_pose_estimated = false;
if (estimated_pose.npt >= 4) {
try {
unsigned int nb_inliers = (unsigned int)(0.6 * estimated_pose.npt);
estimated_pose.setRansacNbInliersToReachConsensus(nb_inliers);
estimated_pose.setRansacThreshold(0.01);
estimated_pose.setRansacMaxTrials(500);
estimated_pose.computePose(vpPose::RANSAC, cMo);
is_pose_estimated = true;
} catch (...) {
is_pose_estimated = false;
}
}
if (opt_display) {
Imatch.insert(I, vpImagePoint(0, Iref.getWidth()));
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);
}
if (is_pose_estimated) {
tracker.setPose(I, cMo);
tracker.display(I, cMo, cam, vpColor::red);
}
}
// Click requested to process next image
if (opt_click_allowed && opt_display) {
if (opt_click) {
vpDisplay::getClick(I, button, true);
if (button == vpMouseButton::button3) {
opt_click = false;
}
} else {
// Use right click to enable/disable step by step tracking
if (vpDisplay::getClick(I, button, false)) {
if (button == vpMouseButton::button3) {
opt_click = true;
} else if (button == vpMouseButton::button1) {
break;
}
}
}
}
}
}
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) {
exit(-1);
}
// 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;
return -1;
}
{
vpImage<unsigned char> I, Imatch, Iref;
std::cout << "-- Test on gray level images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
}
{
vpImage<vpRGBa> I, Imatch, Iref;
std::cout << "-- Test on color images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, I, Imatch, Iref);
}
} catch (const vpException &e) {
std::cerr << e.what() << std::endl;
return -1;
}
std::cout << "testKeyPoint-4 is ok !" << std::endl;
return 0;
}
#else
int main()
{
std::cerr << "You need OpenCV library." << std::endl;
return 0;
}
#endif
vpMbEdgeTracker::setClipping
virtual void setClipping(const unsigned int &flags)
Definition: vpMbEdgeTracker.cpp:2694
vpDisplayX
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
vpImage::insert
void insert(const vpImage< Type > &src, const vpImagePoint &topLeft)
Definition: vpImage.h:1230
vpMbTracker::setAngleDisappear
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:476
vpDisplay::displayLine
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
Definition: vpDisplay_uchar.cpp:400
vpIoTools::getViSPImagesDataPath
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1292
vpMbTracker::getCameraParameters
virtual void getCameraParameters(vpCameraParameters &cam) const
Definition: vpMbTracker.h:244
vpMbEdgeTracker
Make the complete tracking of an object by using its CAD model.
Definition: vpMbEdgeTracker.h:259
vpImageConvert::convert
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition: vpImageConvert.cpp:79
vpMath::rad
static double rad(double deg)
Definition: vpMath.h:108
vpMbTracker::getClipping
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:252
vpCameraParameters
Generic class defining intrinsic camera parameters.
Definition: vpCameraParameters.h:234
vpMbEdgeTracker::setMovingEdge
void setMovingEdge(const vpMe &me)
Definition: vpMbEdgeTracker.cpp:133
vpMouseButton::button1
@ button1
Definition: vpMouseButton.h:53
vpMe::setMu2
void setMu2(const double &mu_2)
Definition: vpMe.h:248
vpDisplay::SCALE_AUTO
@ SCALE_AUTO
Definition: vpDisplay.h:177
vpMbEdgeTracker::loadConfigFile
void loadConfigFile(const std::string &configFile)
Definition: vpMbEdgeTracker.cpp:1224
vpImageIo::read
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:243
vpVideoReader::end
bool end()
Definition: vpVideoReader.h:260
vpException::fatalError
@ fatalError
Fatal error.
Definition: vpException.h:96
vpMe::setThreshold
void setThreshold(const double &t)
Definition: vpMe.h:300
vpPose::setRansacNbInliersToReachConsensus
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:247
vpMbEdgeTracker::setNearClippingDistance
virtual void setNearClippingDistance(const double &dist)
Definition: vpMbEdgeTracker.cpp:2662
vpDisplayGDI
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
vpMbEdgeTracker::display
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, unsigned int thickness=1, bool displayFullModel=false)
Definition: vpMbEdgeTracker.cpp:1291
vpMbTracker::getPose
virtual void getPose(vpHomogeneousMatrix &cMo) const
Definition: vpMbTracker.h:410
vpPolygon3D::FOV_CLIPPING
@ FOV_CLIPPING
Definition: vpPolygon3D.h:70
vpPose::addPoint
void addPoint(const vpPoint &P)
Definition: vpPose.cpp:149
vpImage::getHeight
unsigned int getHeight() const
Definition: vpImage.h:222
vpColor::none
static const vpColor none
Definition: vpColor.h:191
vpMbTracker::initClick
virtual void initClick(const vpImage< unsigned char > &I, const std::string &initFile, bool displayHelp=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Definition: vpMbTracker.cpp:548
vpMe
Definition: vpMe.h:61
vpPose::RANSAC
@ RANSAC
Definition: vpPose.h:90
vpMe::setRange
void setRange(const unsigned int &r)
Definition: vpMe.h:271
vpDisplay::displayFrame
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))
Definition: vpDisplay_uchar.cpp:384
vpMbTracker::initFromPose
virtual void initFromPose(const vpImage< unsigned char > &I, const std::string &initFile)
Definition: vpMbTracker.cpp:1135
vpPose::computePose
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=NULL)
Definition: vpPose.cpp:374
vpVideoReader::getFrameIndex
long getFrameIndex() const
Definition: vpVideoReader.h:295
vpMbEdgeTracker::setFarClippingDistance
virtual void setFarClippingDistance(const double &dist)
Definition: vpMbEdgeTracker.cpp:2632
vpDetectorAprilTag::detect
bool detect(const vpImage< unsigned char > &I)
Definition: vpDetectorAprilTag.cpp:762
vpDisplayOpenCV
The vpDisplayOpenCV allows to display image using the OpenCV library. Thus to enable this class OpenC...
Definition: vpDisplayOpenCV.h:142
vpImage::getWidth
unsigned int getWidth() const
Definition: vpImage.h:280
vpVideoReader::open
void open(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:175
vpParseArgv::parse
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
vpVideoReader
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
Definition: vpVideoReader.h:172
vpPose::setRansacMaxTrials
void setRansacMaxTrials(const int &rM)
Definition: vpPose.h:257
vpVideoReader::setFileName
void setFileName(const std::string &filename)
Definition: vpVideoReader.cpp:92
vpDisplay::display
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:740
vpDisplayGTK
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:138
vpDisplayOpenCV::init
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayOpenCV.cpp:347
vpMe::setMaskNumber
void setMaskNumber(const unsigned int &a)
Definition: vpMe.cpp:454
vpException::what
const char * what() const
Definition: vpException.cpp:102
vpPixelMeterConversion::convertPoint
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Definition: vpPixelMeterConversion.h:103
vpMbTracker::setAngleAppear
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:465
vpIoTools::createFilePath
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1537
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
vpMouseButton::button3
@ button3
Definition: vpMouseButton.h:55
vpPose::setRansacThreshold
void setRansacThreshold(const double &t)
Definition: vpPose.h:248
vpCameraParameters::initPersProjWithoutDistortion
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Definition: vpCameraParameters.cpp:182
vpMe::setNbTotalSample
void setNbTotalSample(const int &nb)
Definition: vpMe.h:255
vpDisplay::getDownScalingFactor
unsigned int getDownScalingFactor()
Definition: vpDisplay.h:229
vpMbEdgeTracker::setCameraParameters
virtual void setCameraParameters(const vpCameraParameters &cam)
Definition: vpMbEdgeTracker.h:405
vpPose
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:81
vpDisplay::setDownScalingFactor
virtual void setDownScalingFactor(unsigned int scale)
Definition: vpDisplay.cpp:231
vpColor::green
static const vpColor green
Definition: vpColor.h:182
vpMe::setSampleStep
void setSampleStep(const double &s)
Definition: vpMe.h:278
vpDisplay::flush
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay_uchar.cpp:716
vpPose::npt
unsigned int npt
Number of point used in pose computation.
Definition: vpPose.h:109
vpImage
Definition of the vpImage class member functions.
Definition: vpImage.h:160
vpColor::red
static const vpColor red
Definition: vpColor.h:179
vpPoint
Class that defines what is a point.
Definition: vpPoint.h:59
vpDisplay::getClick
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
Definition: vpDisplay_uchar.cpp:765
vpKeyPoint::compute3DForPointsInPolygons
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=NULL)
Definition: vpKeyPoint.cpp:632
vpHomogeneousMatrix
Implementation of an homogeneous matrix and operations on such kind of matrices.
Definition: vpHomogeneousMatrix.h:150
vpMe::setMu1
void setMu1(const double &mu_1)
Definition: vpMe.h:241
vpMouseButton::vpMouseButtonType
vpMouseButtonType
Definition: vpMouseButton.h:52
vpMe::setMaskSize
void setMaskSize(const unsigned int &a)
Definition: vpMe.cpp:461
vpException
error that can be emited by ViSP classes.
Definition: vpException.h:72
vpVideoReader::acquire
void acquire(vpImage< vpRGBa > &I)
Definition: vpVideoReader.cpp:244
vpImage::resize
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:915
vpMbTracker::loadModel
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix())
Definition: vpMbTracker.cpp:1466
vpMbEdgeTracker::setPose
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)
Definition: vpMbEdgeTracker.cpp:1192
vpMbTracker::getPolygonFaces
virtual std::pair< std::vector< vpPolygon >, std::vector< std::vector< vpPoint > > > getPolygonFaces(bool orderPolygons=true, bool useVisibility=true, bool clipPolygon=false)
Definition: vpMbTracker.cpp:2602