47#include <visp3/core/vpConfig.h>
48#include <visp3/core/vpDebug.h>
50#if ((defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && \
51 (VISP_HAVE_OPENCV_VERSION >= 0x020408) && (VISP_HAVE_OPENCV_VERSION < 0x030000))
56#include <visp3/core/vpConfig.h>
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/sensor/vp1394TwoGrabber.h>
66#include <visp3/sensor/vpV4l2Grabber.h>
67#include <visp3/vision/vpHomography.h>
68#include <visp3/vision/vpPlanarObjectDetector.h>
70#define GETOPTARGS "hlcdb:i:p"
72void usage(
const char *name,
const char *badparam);
73bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
74 bool &display,
bool &displayPoints, std::string &ipath);
84void usage(
const char *name,
const char *badparam)
86#if VISP_HAVE_DATASET_VERSION >= 0x030600
87 std::string ext(
"png");
89 std::string ext(
"pgm");
92Test of detection of planar surface using a Fern classifier. The object needs \
93 first to be learned (-l option). This learning process will create a file used\
94 to detect the object.\n\
97 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i] [-s]\n",
105 -i <input image path> \n\
106 Set image input path.\n\
107 From this path read \"line/image.%%04d.%s\"\n\
109 Setting the VISP_INPUT_IMAGE_PATH environment\n\
110 variable produces the same behaviour than using\n\
114 database filename to use (default is ./dataPlanar).\n\
117 Disable the mouse click. Useful to automate the \n\
118 execution of this program without human intervention.\n\
121 Turn off the display.\n\
124 Turn off the use of the sequence and use a webcam.\n\
127 display points of interest.\n\
130 Print this help.\n", ext.c_str());
133 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
152bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string &dataFile,
bool &click_allowed,
153 bool &display,
bool &displayPoints, std::string &ipath)
161 click_allowed =
false;
170 usage(argv[0], NULL);
177 displayPoints =
true;
183 usage(argv[0], optarg_);
189 if ((c == 1) || (c == -1)) {
191 usage(argv[0], NULL);
192 std::cerr <<
"ERROR: " << std::endl;
193 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
200int main(
int argc,
const char **argv)
203 bool isLearning =
false;
204 std::string dataFile(
"./dataPlanar");
205 bool opt_click_allowed =
true;
206 bool opt_display =
true;
207 std::string objectName(
"object");
208 bool displayPoints =
false;
209 std::string opt_ipath;
211 std::string env_ipath;
213 std::string filename;
215#if VISP_HAVE_DATASET_VERSION >= 0x030600
216 std::string ext(
"png");
218 std::string ext(
"pgm");
226 if (!env_ipath.empty()) {
231 if (getOptions(argc, argv, isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
237 if (!opt_ipath.empty()) {
243 if (!opt_ipath.empty() && !env_ipath.empty()) {
244 if (ipath != env_ipath) {
245 std::cout << std::endl <<
"WARNING: " << std::endl;
246 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
247 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
248 <<
" we skip the environment variable." << std::endl;
253 if (opt_ipath.empty() && env_ipath.empty()) {
254 usage(argv[0], NULL);
255 std::cerr << std::endl <<
"ERROR:" << std::endl;
256 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
257 <<
" environment variable to specify the location of the " << std::endl
258 <<
" image path where test images are located." << std::endl
272 std::ostringstream s;
273 s.setf(std::ios::right, std::ios::adjustfield);
274 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
279 std::cout <<
"Load: " << filename << std::endl;
284 std::cerr << std::endl <<
"ERROR:" << std::endl;
285 std::cerr <<
" Cannot read " << filename << std::endl;
286 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
287 <<
" or VISP_INPUT_IMAGE_PATH environment variable." << std::endl;
291#if defined(VISP_HAVE_X11)
293#elif defined(VISP_HAVE_GTK)
295#elif defined(VISP_HAVE_GDI)
299#if defined(VISP_HAVE_X11)
301#elif defined(VISP_HAVE_GTK)
303#elif defined(VISP_HAVE_GDI)
308 vpPlanarObjectDetector planar;
313 displayRef.
init(Iref, 100, 100,
"Reference image");
317 if (opt_display && opt_click_allowed) {
318 std::cout <<
"Click on the top left and the bottom right corners to "
319 "define the reference plane"
321 for (
int i = 0; i < 2; i++) {
323 std::cout << corners[i] << std::endl;
338 if (opt_click_allowed) {
339 std::cout <<
"Click on the image to continue" << std::endl;
343 vpRect roi(corners[0], corners[1]);
345 std::cout <<
"> train the classifier on the selected plane (may take "
346 "up to several minutes)."
353 planar.buildReference(Iref, roi);
356 planar.recordDetector(objectName, dataFile);
361 vpERROR_TRACE(
"cannot load the database with the specified name. Has "
362 "the object been learned with the -l option? ");
367 planar.load(dataFile, objectName);
370 vpERROR_TRACE(
"cannot load the database with the specified name. Has "
371 "the object been learned with the -l option? ");
377 display.init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image");
382 if (opt_display && opt_click_allowed) {
383 std::cout <<
"Click on the reference image to continue" << std::endl;
396 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
"." << ext;
407 bool isDetected = planar.matchPoint(I);
412 planar.getHomography(H);
413 std::cout <<
" > computed homography:" << std::endl << H << std::endl;
418 planar.display(Iref, I, displayPoints);
422 planar.display(I, displayPoints);
427 std::cout <<
" > reference is not detected in the image" << std::endl;
440 std::cout <<
"Catch an exception: " << e << std::endl;
448#if (!(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)))
449 std::cout <<
"You do not have X11, or GTK, or GDI (Graphical Device Interface) functionalities to display images..."
451 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
452 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
453 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
454 std::cout <<
"- Install GDI, configure again ViSP using cmake and build again this example" << std::endl;
456 std::cout <<
"You do not have OpenCV functionalities" << std::endl;
457 std::cout <<
"Tip:" << std::endl;
458 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.
Implementation of an homography and operations on homographies.
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()