Visual Servoing Platform  version 3.5.1 under development (2022-07-07)
testKeyPoint-6.cpp

Test descriptor extraction.

/****************************************************************************
*
* 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 descriptor computation.
*
* Authors:
* Souriya Trinh
*
*****************************************************************************/
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020301)
#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/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 keypoint descriptor extraction.\n\
\n\
SYNOPSIS\n\
%s [-c] [-d] [-h]\n",
name);
fprintf(stdout, "\n\
OPTIONS: \n\
\n\
-c\n\
Disable the mouse click. Useful to automaze the \n\
execution of this program without humain 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;
}
std::string getOpenCVType(int type)
{
std::string type_string = "";
switch (type) {
case CV_8U:
type_string = "CV_8U";
break;
case CV_8S:
type_string = "CV_8S";
break;
case CV_16U:
type_string = "CV_16U";
break;
case CV_16S:
type_string = "CV_16S";
break;
case CV_32S:
type_string = "CV_32S";
break;
case CV_32F:
type_string = "CV_32F";
break;
case CV_64F:
type_string = "CV_64F";
break;
default:
type_string = "Problem with type !";
break;
}
return type_string;
}
template <typename Type>
void run_test(const std::string &env_ipath, bool opt_click_allowed, bool opt_display, vpImage<Type> &Iinput,
{
// Set the path location of the image sequence
std::string dirname = vpIoTools::createFilePath(env_ipath, "Klimt");
// Build the name of the image files
std::string filename = vpIoTools::createFilePath(dirname, "/Klimt.png");
vpImageIo::read(Iinput, filename);
Iinput.quarterSizeImage(I);
#if defined VISP_HAVE_X11
vpDisplayX display;
#elif defined VISP_HAVE_GTK
vpDisplayGTK display;
#elif defined VISP_HAVE_GDI
vpDisplayGDI display;
#else
vpDisplayOpenCV display;
#endif
if (opt_display) {
display.init(I, 0, 0, "KeyPoints detection.");
}
vpKeyPoint keyPoints;
std::vector<std::string> descriptorNames;
#if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D) || \
(VISP_HAVE_OPENCV_VERSION >= 0x030411 && CV_MAJOR_VERSION < 4) || (VISP_HAVE_OPENCV_VERSION >= 0x040400)
descriptorNames.push_back("SIFT");
#endif
#if defined(VISP_HAVE_OPENCV_NONFREE) || defined(VISP_HAVE_OPENCV_XFEATURES2D)
descriptorNames.push_back("SURF");
#endif
descriptorNames.push_back("ORB");
#if (VISP_HAVE_OPENCV_VERSION >= 0x020403)
descriptorNames.push_back("BRISK");
#endif
#if defined(VISP_HAVE_OPENCV_XFEATURES2D) || (VISP_HAVE_OPENCV_VERSION < 0x030000)
descriptorNames.push_back("BRIEF");
#if (VISP_HAVE_OPENCV_VERSION >= 0x020402)
descriptorNames.push_back("FREAK");
#endif
#endif
#if defined(VISP_HAVE_OPENCV_XFEATURES2D)
descriptorNames.push_back("DAISY");
descriptorNames.push_back("LATCH");
#endif
#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
descriptorNames.push_back("VGG");
descriptorNames.push_back("BoostDesc");
#endif
#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
descriptorNames.push_back("KAZE");
descriptorNames.push_back("AKAZE");
#endif
std::string detectorName = "FAST";
keyPoints.setDetector(detectorName);
std::vector<cv::KeyPoint> kpts;
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
keyPoints.setExtractor(*itd);
if (*itd == "KAZE") {
detectorName = "KAZE";
keyPoints.setDetector(detectorName);
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
} else if (*itd == "AKAZE") {
detectorName = "AKAZE";
keyPoints.setDetector(detectorName);
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
} else if (*itd == "BoostDesc") {
#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
// Init BIN BOOST descriptor for FAST keypoints
boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
#endif
}
double t = vpTime::measureTimeMs();
cv::Mat descriptor;
keyPoints.extract(I, kpts, descriptor);
std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
<< " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for " << *itd << " method in " << t
<< " ms." << std::endl;
if (descriptor.empty()) {
std::stringstream ss;
ss << "No descriptor extracted with " << *itd << " and image:" << filename << "." << std::endl;
}
if (opt_display) {
for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
imPt.set_uv(it->pt.x, it->pt.y);
}
if (opt_click_allowed) {
}
}
}
std::cout << "\n\n";
std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.getExtractorNames();
for (int i = 0; i < vpKeyPoint::DESCRIPTOR_TYPE_SIZE; i++) {
if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "KAZE") {
detectorName = "KAZE";
keyPoints.setDetector(detectorName);
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
} else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "AKAZE") {
detectorName = "AKAZE";
keyPoints.setDetector(detectorName);
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
} else if (mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] == "BoostDesc") {
#if (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(VISP_HAVE_OPENCV_XFEATURES2D)
detectorName = "FAST";
keyPoints.setDetector(detectorName);
keyPoints.detect(I, kpts);
std::cout << "Nb keypoints detected: " << kpts.size() << " for " << detectorName << " method." << std::endl;
if (kpts.empty()) {
std::stringstream ss;
ss << "No keypoints detected with " << detectorName << " and image:" << filename << "." << std::endl;
}
cv::Ptr<cv::Feature2D> boostDesc = keyPoints.getExtractor("BoostDesc");
// Init BIN BOOST descriptor for FAST keypoints
boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256, true, 5.0f);
#endif
}
double t = vpTime::measureTimeMs();
cv::Mat descriptor;
keyPoints.extract(I, kpts, descriptor);
std::cout << "Descriptor: " << descriptor.rows << "x" << descriptor.cols
<< " (rows x cols) ; type=" << getOpenCVType(descriptor.type()) << " for "
<< mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i] << " method in " << t << " ms."
<< std::endl;
if (descriptor.empty()) {
std::stringstream ss;
ss << "No descriptor extracted with " << mapOfDescriptorNames[(vpKeyPoint::vpFeatureDescriptorType)i]
<< " and image:" << filename << "." << std::endl;
}
if (opt_display) {
for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
imPt.set_uv(it->pt.x, it->pt.y);
}
if (opt_click_allowed) {
}
}
}
}
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(EXIT_FAILURE);
}
// 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 EXIT_FAILURE;
}
{
std::cout << "-- Test on gray level images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
}
{
vpImage<vpRGBa> Iinput, I;
std::cout << "-- Test on color images" << std::endl;
run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
}
} catch (const vpException &e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
std::cout << "testKeyPoint-6 is ok !" << std::endl;
return EXIT_SUCCESS;
}
#else
#include <cstdlib>
int main()
{
std::cerr << "You need OpenCV library." << std::endl;
return EXIT_SUCCESS;
}
#endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Definition: vpDisplayGTK.h:135
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:135
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
static void flush(const vpImage< unsigned char > &I)
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96
const char * what() const
Definition: vpException.cpp:99
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:148
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:89
void set_uv(double u, double v)
Definition: vpImagePoint.h:357
Definition of the vpImage class member functions.
Definition: vpImage.h:73
void quarterSizeImage(vpImage< Type > &res) const
Definition: vpImage.h:1253
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1368
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1687
Class that allows keypoints detection (and descriptors extraction) and matching thanks to OpenCV libr...
Definition: vpKeyPoint.h:223
void setExtractor(const vpFeatureDescriptorType &extractorType)
Definition: vpKeyPoint.h:834
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=NULL)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
vpFeatureDescriptorType
Definition: vpKeyPoint.h:289
@ DESCRIPTOR_TYPE_SIZE
Definition: vpKeyPoint.h:317
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
Definition: vpKeyPoint.h:587
void setDetector(const vpFeatureDetectorType &detectorType)
Definition: vpKeyPoint.h:776
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
Definition: vpKeyPoint.h:547
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
VISP_EXPORT double measureTimeMs()