51 #include <visp3/core/vpConfig.h> 52 #include <visp3/core/vpDebug.h> 54 #if ((defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && \ 55 (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000)) 60 #include <visp3/core/vpConfig.h> 61 #include <visp3/core/vpImage.h> 62 #include <visp3/core/vpIoTools.h> 63 #include <visp3/core/vpTime.h> 64 #include <visp3/gui/vpDisplayGDI.h> 65 #include <visp3/gui/vpDisplayGTK.h> 66 #include <visp3/gui/vpDisplayX.h> 67 #include <visp3/io/vpImageIo.h> 68 #include <visp3/io/vpParseArgv.h> 69 #include <visp3/vision/vpFernClassifier.h> 70 #include <visp3/vision/vpHomography.h> 72 #define GETOPTARGS "hlcdb:i:p" 74 void usage(
const char *name,
const char *badparam);
75 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
76 bool &display,
bool &displayPoints, std::string &ipath);
86 void usage(
const char *name,
const char *badparam)
89 Detection of points of interests and matching using the Ferns classifier. The \ 90 object needs first to be learned (-l option). This learning process will create\ 91 a file used to detect the object.\n\ 94 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i]\n", name);
101 -i <input image path> \n\ 102 Set image input path.\n\ 103 From this path read \"line/image.%%04d.pgm\"\n\ 105 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 106 variable produces the same behaviour than using\n\ 110 database filename to use (default is ./dataFern).\n\ 113 Disable the mouse click. Useful to automaze the \n\ 114 execution of this program without humain intervention.\n\ 117 Turn off the display.\n\ 120 display points of interest.\n\ 123 Print this help.\n");
126 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
145 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
146 bool &display,
bool &displayPoints, std::string &ipath)
154 click_allowed =
false;
163 usage(argv[0], NULL);
170 displayPoints =
true;
176 usage(argv[0], optarg_);
182 if ((c == 1) || (c == -1)) {
184 usage(argv[0], NULL);
185 std::cerr <<
"ERROR: " << std::endl;
186 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
193 int main(
int argc,
const char **argv)
196 bool isLearning =
false;
197 std::string dataFile(
"./dataFern");
198 bool opt_click_allowed =
true;
199 bool opt_display =
true;
200 std::string objectName(
"object");
201 bool displayPoints =
false;
202 std::string opt_ipath;
204 std::string env_ipath;
206 std::string filename;
213 if (!env_ipath.empty()) {
218 if (getOptions(argc, argv, isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
224 if (!opt_ipath.empty()) {
230 if (!opt_ipath.empty() && !env_ipath.empty()) {
231 if (ipath != env_ipath) {
232 std::cout << std::endl <<
"WARNING: " << std::endl;
233 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 234 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
235 <<
" we skip the environment variable." << std::endl;
240 if (opt_ipath.empty() && env_ipath.empty()) {
241 usage(argv[0], NULL);
242 std::cerr << std::endl <<
"ERROR:" << std::endl;
243 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
244 <<
" environment variable to specify the location of the " << std::endl
245 <<
" image path where test images are located." << std::endl
259 std::ostringstream s;
260 s.setf(std::ios::right, std::ios::adjustfield);
261 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
271 std::cout <<
"Load: " << filename << std::endl;
279 std::cerr << std::endl <<
"ERROR:" << std::endl;
280 std::cerr <<
" Cannot read " << filename << std::endl;
281 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
282 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
286 #if defined VISP_HAVE_X11 288 #elif defined VISP_HAVE_GTK 290 #elif defined VISP_HAVE_GDI 294 #if defined VISP_HAVE_X11 296 #elif defined VISP_HAVE_GTK 298 #elif defined VISP_HAVE_GDI 303 vpFernClassifier fern;
307 displayRef.
init(Iref, 100, 100,
"Reference image");
312 if (opt_display && opt_click_allowed) {
313 std::cout <<
"Click on the top left and the bottom right corners to " 314 "define the reference plane" 316 for (
int i = 0; i < 2; i++) {
318 std::cout << corners[i] << std::endl;
332 if (opt_click_allowed) {
333 std::cout <<
"Click on the image to continue" << std::endl;
337 vpRect roi(corners[0], corners[1]);
339 std::cout <<
"> train the classifier on the selected plane. (may take " 340 "up to several minutes)." 348 fern.buildReference(Iref, roi);
352 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
355 fern.record(objectName, dataFile);
359 std::cout <<
"unknown error, line " << __LINE__ << std::endl;
361 std::cout << __LINE__ << std::endl;
364 vpERROR_TRACE(
"cannot load the database with the specified name. Has " 365 "the object been learned with the -l option? ");
370 fern.load(dataFile, objectName);
372 vpERROR_TRACE(
"cannot load the database with the specified name. Has " 373 "the object been learned with the -l option? ");
379 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image");
384 if (opt_display && opt_click_allowed) {
385 std::cout <<
"Click on the current image to continue" << std::endl;
398 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
413 nbpts = fern.matchPoint(I);
418 std::cout <<
"unknown error line " << __LINE__ << std::endl;
421 std::cout <<
"matching " << nbpts <<
" points : " <<
vpTime::measureTimeMs() - t0 <<
" ms" << std::endl;
424 fern.display(Iref, I, 7);
436 std::cout <<
"Catch an exception: " << e << std::endl;
444 #if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI))) 445 std::cout <<
"You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..." << std::endl;
446 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
447 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
448 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
449 std::cout <<
"- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
451 std::cout <<
"You do not have OpenCV functionalities" << std::endl;
452 std::cout <<
"Tip:" << std::endl;
453 std::cout <<
"- Install OpenCV, configure again ViSP using cmake and build again this example" << std::endl;
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
unsigned int getWidth() const
Display for windows using GDI (available on any windows 32 platform).
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
error that can be emited by ViSP classes.
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void display(const vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
const char * getMessage(void) const
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 read(vpImage< unsigned char > &I, const std::string &filename)
unsigned int getHeight() const
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Defines a rectangle in the plane.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
void set_ij(const double ii, const double jj)