48#include <visp3/core/vpConfig.h>
49#include <visp3/core/vpDebug.h>
51#if ((defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && \
52 (VISP_HAVE_OPENCV_VERSION >= 0x020408) && (VISP_HAVE_OPENCV_VERSION < 0x030000))
57#include <visp3/core/vpImage.h>
58#include <visp3/core/vpIoTools.h>
59#include <visp3/core/vpTime.h>
60#include <visp3/gui/vpDisplayGDI.h>
61#include <visp3/gui/vpDisplayGTK.h>
62#include <visp3/gui/vpDisplayX.h>
63#include <visp3/io/vpImageIo.h>
64#include <visp3/io/vpParseArgv.h>
65#include <visp3/vision/vpFernClassifier.h>
66#include <visp3/vision/vpHomography.h>
68#define GETOPTARGS "hlcdb:i:p"
78void usage(
const char *name,
const char *badparam)
80#if VISP_HAVE_DATASET_VERSION >= 0x030600
81 std::string ext(
"png");
83 std::string ext(
"pgm");
86Detection of points of interests and matching using the Ferns classifier. The \
87object needs first to be learned (-l option). This learning process will create\
88a file used to detect the object.\n\
91 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n",
99 -i <input image path> \n\
100 Set image input path.\n\
101 From this path read \"line/image.%%04d.%s\"\n\
103 Setting the VISP_INPUT_IMAGE_PATH environment\n\
104 variable produces the same behaviour than using\n\
108 database filename to use (default is ./dataFern).\n\
111 Disable the mouse click. Useful to automate the \n\
112 execution of this program without human intervention.\n\
115 Turn off the display.\n\
118 display points of interest.\n\
125 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
144bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
145 bool &display,
bool &displayPoints, std::string &ipath)
153 click_allowed =
false;
162 usage(argv[0], NULL);
169 displayPoints =
true;
175 usage(argv[0], optarg_);
181 if ((c == 1) || (c == -1)) {
183 usage(argv[0], NULL);
184 std::cerr <<
"ERROR: " << std::endl;
185 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
192int main(
int argc,
const char **argv)
195 bool isLearning =
false;
196 std::string dataFile(
"./dataFern");
197 bool opt_click_allowed =
true;
198 bool opt_display =
true;
199 std::string objectName(
"object");
200 bool displayPoints =
false;
201 std::string opt_ipath;
203 std::string env_ipath;
205 std::string filename;
207#if VISP_HAVE_DATASET_VERSION >= 0x030600
208 std::string ext(
"png");
210 std::string ext(
"pgm");
218 if (!env_ipath.empty()) {
223 if (getOptions(argc, argv, isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
229 if (!opt_ipath.empty()) {
235 if (!opt_ipath.empty() && !env_ipath.empty()) {
236 if (ipath != env_ipath) {
237 std::cout << std::endl <<
"WARNING: " << std::endl;
238 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
239 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
240 <<
" we skip the environment variable." << std::endl;
245 if (opt_ipath.empty() && env_ipath.empty()) {
246 usage(argv[0], NULL);
247 std::cerr << std::endl <<
"ERROR:" << std::endl;
248 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
249 <<
" environment variable to specify the location of the " << std::endl
250 <<
" image path where test images are located." << std::endl
264 std::ostringstream s;
265 s.setf(std::ios::right, std::ios::adjustfield);
266 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
271 std::cout <<
"Load: " << filename << std::endl;
276 std::cerr << std::endl <<
"ERROR:" << std::endl;
277 std::cerr <<
" Cannot read " << filename << std::endl;
278 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
279 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
283#if defined(VISP_HAVE_X11)
285#elif defined(VISP_HAVE_GTK)
287#elif defined(VISP_HAVE_GDI)
291#if defined(VISP_HAVE_X11)
293#elif defined(VISP_HAVE_GTK)
295#elif defined(VISP_HAVE_GDI)
300 vpFernClassifier fern;
304 displayRef.
init(Iref, 100, 100,
"Reference image");
309 if (opt_display && opt_click_allowed) {
310 std::cout <<
"Click on the top left and the bottom right corners to "
311 "define the reference plane"
313 for (
int i = 0; i < 2; i++) {
315 std::cout << corners[i] << std::endl;
330 if (opt_click_allowed) {
331 std::cout <<
"Click on the image to continue" << std::endl;
335 vpRect roi(corners[0], corners[1]);
337 std::cout <<
"> train the classifier on the selected plane. (may take "
338 "up to several minutes)."
346 fern.buildReference(Iref, roi);
352 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
355 fern.record(objectName, dataFile);
361 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
363 std::cout << __LINE__ << std::endl;
367 vpERROR_TRACE(
"cannot load the database with the specified name. Has "
368 "the object been learned with the -l option? ");
373 fern.load(dataFile, objectName);
376 vpERROR_TRACE(
"cannot load the database with the specified name. Has "
377 "the object been learned with the -l option? ");
383 display.init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image");
388 if (opt_display && opt_click_allowed) {
389 std::cout <<
"Click on the current image to continue" << std::endl;
402 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
417 nbpts = fern.matchPoint(I);
424 std::cout <<
"unknown error line " << __LINE__ << std::endl;
427 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs() - t0 <<
" ms" << std::endl;
430 fern.display(Iref, I, 7);
443 std::cout <<
"Catch an exception: " << e << std::endl;
451#if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
452 std::cout <<
"You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
454 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
455 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
456 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
457 std::cout <<
"- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
459 std::cout <<
"You do not have OpenCV functionalities" << std::endl;
460 std::cout <<
"Tip:" << std::endl;
461 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
static const vpColor green
Display for windows using GDI (available on any windows 32 platform).
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
void init(vpImage< unsigned char > &I, int win_x=-1, int win_y=-1, const std::string &win_title="")
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
const char * getMessage() const
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_ij(double ii, double jj)
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getHeight() const
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Defines a rectangle in the plane.
VISP_EXPORT double measureTimeMs()