42#include <visp3/core/vpConfig.h>
44#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC) && defined(HAVE_OPENCV_VIDEO) && \
45 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_CALIB3D) && defined(HAVE_OPENCV_FEATURES2D)) || \
46 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_3D) && defined(HAVE_OPENCV_FEATURES)))
48#include <visp3/core/vpImage.h>
49#include <visp3/core/vpIoTools.h>
50#include <visp3/gui/vpDisplayFactory.h>
51#include <visp3/io/vpImageIo.h>
52#include <visp3/io/vpParseArgv.h>
53#include <visp3/vision/vpKeyPoint.h>
56#define GETOPTARGS "cdh"
58#ifdef ENABLE_VISP_NAMESPACE
62void usage(
const char *name,
const char *badparam);
63bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display);
64std::string getOpenCVType(
int type);
72void usage(
const char *name,
const char *badparam)
75Test keypoint descriptor extraction.\n\
85 Disable the mouse click. Useful to automate the \n\
86 execution of this program without human intervention.\n\
89 Turn off the display.\n\
95 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
109bool getOptions(
int argc,
const char **argv,
bool &click_allowed,
bool &display)
117 click_allowed =
false;
123 usage(argv[0],
nullptr);
127 usage(argv[0], optarg_);
132 if ((c == 1) || (c == -1)) {
134 usage(argv[0],
nullptr);
135 std::cerr <<
"ERROR: " << std::endl;
136 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
151std::string getOpenCVType(
int type)
153 std::string type_string =
"";
157 type_string =
"CV_8U";
161 type_string =
"CV_8S";
165 type_string =
"CV_16U";
169 type_string =
"CV_16S";
173 type_string =
"CV_32S";
177 type_string =
"CV_32F";
181 type_string =
"CV_64F";
185 type_string =
"Problem with type !";
192template <
typename Type>
193void run_test(
const std::string &env_ipath,
bool opt_click_allowed,
bool opt_display,
vpImage<Type> &Iinput,
207#ifdef VISP_HAVE_DISPLAY
210 std::cout <<
"No image viewer is available..." << std::endl;
216 std::vector<std::string> descriptorNames;
217#if defined(VISP_HAVE_OPENCV) && \
218 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)) || \
219 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
220 descriptorNames.push_back(
"SIFT");
222#if defined(HAVE_OPENCV_NONFREE) && defined(HAVE_OPENCV_XFEATURES2D)
223 descriptorNames.push_back(
"SURF");
225#if defined(VISP_HAVE_OPENCV) && \
226 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
227 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
228 descriptorNames.push_back(
"ORB");
230#if defined(VISP_HAVE_OPENCV) && \
231 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
232 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)))
233 descriptorNames.push_back(
"BRISK");
235#if defined(HAVE_OPENCV_XFEATURES2D)
236 descriptorNames.push_back(
"BRIEF");
237 descriptorNames.push_back(
"FREAK");
238 descriptorNames.push_back(
"DAISY");
239 descriptorNames.push_back(
"LATCH");
241#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
242 descriptorNames.push_back(
"VGG");
243 descriptorNames.push_back(
"BoostDesc");
245#if defined(VISP_HAVE_OPENCV) && \
246 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
247 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_XFEATURES2D)))
248#if (VISP_HAVE_OPENCV_VERSION >= 0x030000)
249 descriptorNames.push_back(
"KAZE");
250 descriptorNames.push_back(
"AKAZE");
253#if defined(VISP_HAVE_OPENCV) && \
254 (((VISP_HAVE_OPENCV_VERSION < 0x050000) && defined(HAVE_OPENCV_FEATURES2D)) || \
255 ((VISP_HAVE_OPENCV_VERSION >= 0x050000) && defined(HAVE_OPENCV_FEATURES)))
256 std::string detectorName =
"FAST";
259 std::vector<cv::KeyPoint> kpts;
261 keyPoints.
detect(I, kpts);
262 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
264 std::stringstream ss;
265 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
269 for (std::vector<std::string>::const_iterator itd = descriptorNames.begin(); itd != descriptorNames.end(); ++itd) {
272 if (*itd ==
"KAZE") {
273 detectorName =
"KAZE";
275 keyPoints.
detect(I, kpts);
276 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
278 std::stringstream ss;
279 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
283 else if (*itd ==
"AKAZE") {
284 detectorName =
"AKAZE";
286 keyPoints.
detect(I, kpts);
287 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
289 std::stringstream ss;
290 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
294 else if (*itd ==
"BoostDesc") {
295#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
296 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.
getExtractor(
"BoostDesc");
298 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256,
true, 5.0f);
304 keyPoints.
extract(I, kpts, descriptor);
307 std::cout <<
"Descriptor: " << descriptor.rows <<
"x" << descriptor.cols
308 <<
" (rows x cols) ; type=" << getOpenCVType(descriptor.type()) <<
" for " << *itd <<
" method in " <<
t
309 <<
" ms." << std::endl;
310 if (descriptor.empty()) {
311 std::stringstream ss;
312 ss <<
"No descriptor extracted with " << *itd <<
" and image:" <<
filename <<
"." << std::endl;
319 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
321 imPt.
set_uv(
static_cast<double>(it->pt.x),
static_cast<double>(it->pt.y));
328 if (opt_click_allowed) {
336 std::map<vpKeyPoint::vpFeatureDescriptorType, std::string> mapOfDescriptorNames = keyPoints.
getExtractorNames();
342 detectorName =
"KAZE";
344 keyPoints.
detect(I, kpts);
345 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
347 std::stringstream ss;
348 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
353 detectorName =
"AKAZE";
355 keyPoints.
detect(I, kpts);
356 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
358 std::stringstream ss;
359 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
364#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030200) && defined(HAVE_OPENCV_XFEATURES2D)
365 detectorName =
"FAST";
367 keyPoints.
detect(I, kpts);
368 std::cout <<
"Nb keypoints detected: " << kpts.size() <<
" for " << detectorName <<
" method." << std::endl;
370 std::stringstream ss;
371 ss <<
"No keypoints detected with " << detectorName <<
" and image:" <<
filename <<
"." << std::endl;
375 cv::Ptr<cv::Feature2D> boostDesc = keyPoints.
getExtractor(
"BoostDesc");
377 boostDesc = cv::xfeatures2d::BoostDesc::create(cv::xfeatures2d::BoostDesc::BINBOOST_256,
true, 5.0f);
383 keyPoints.
extract(I, kpts, descriptor);
386 std::cout <<
"Descriptor: " << descriptor.rows <<
"x" << descriptor.cols
387 <<
" (rows x cols) ; type=" << getOpenCVType(descriptor.type()) <<
" for "
390 if (descriptor.empty()) {
391 std::stringstream ss;
393 <<
" and image:" << filename <<
"." << std::endl;
400 for (std::vector<cv::KeyPoint>::const_iterator it = kpts.begin(); it != kpts.end(); ++it) {
402 imPt.
set_uv(
static_cast<double>(it->pt.x),
static_cast<double>(it->pt.y));
409 if (opt_click_allowed) {
419int main(
int argc,
const char **argv)
422 std::string env_ipath;
423 bool opt_click_allowed =
true;
424 bool opt_display =
true;
427 if (getOptions(argc, argv, opt_click_allowed, opt_display) ==
false) {
435 if (env_ipath.empty()) {
436 std::cerr <<
"Please set the VISP_INPUT_IMAGE_PATH environment "
445 std::cout <<
"-- Test on gray level images" << std::endl;
446 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
452 std::cout <<
"-- Test on color images" << std::endl;
453 run_test(env_ipath, opt_click_allowed, opt_display, Iinput, I);
458 std::cerr <<
e.what() << std::endl;
462 std::cout <<
"testKeyPoint-6 is ok !" << std::endl;
470 std::cerr <<
"You need OpenCV library." << std::endl;
Class that defines generic functionalities for display.
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 emitted by ViSP classes.
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 ...
void set_uv(double u, double v)
Definition of the vpImage class member functions.
void quarterSizeImage(vpImage< Type > &res) const
Class that allows keypoints 2D features detection (and descriptors extraction) and matching thanks to...
void setExtractor(const vpFeatureDescriptorType &extractorType)
cv::Ptr< cv::DescriptorExtractor > getExtractor(const vpFeatureDescriptorType &type) const
void extract(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, cv::Mat &descriptors, std::vector< cv::Point3f > *trainPoints=nullptr)
void detect(const vpImage< unsigned char > &I, std::vector< cv::KeyPoint > &keyPoints, const vpRect &rectangle=vpRect())
@ DESCRIPTOR_TYPE_SIZE
Number of descriptors available.
std::map< vpFeatureDescriptorType, std::string > getExtractorNames() const
void setDetector(const vpFeatureDetectorType &detectorType)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()