49 #include <visp3/core/vpConfig.h> 50 #include <visp3/core/vpDebug.h> 52 #if ((defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) && (VISP_HAVE_OPENCV_VERSION >= 0x020000) && (VISP_HAVE_OPENCV_VERSION < 0x030000)) 56 #include <visp3/vision/vpPlanarObjectDetector.h> 57 #include <visp3/io/vpParseArgv.h> 58 #include <visp3/core/vpConfig.h> 59 #include <visp3/core/vpImage.h> 60 #include <visp3/gui/vpDisplayX.h> 61 #include <visp3/gui/vpDisplayGTK.h> 62 #include <visp3/gui/vpDisplayGDI.h> 63 #include <visp3/vision/vpHomography.h> 64 #include <visp3/io/vpImageIo.h> 65 #include <visp3/core/vpIoTools.h> 66 #include <visp3/core/vpTime.h> 68 #include <visp3/sensor/vpV4l2Grabber.h> 69 #include <visp3/sensor/vp1394TwoGrabber.h> 71 #define GETOPTARGS "hlcdb:i:p" 73 void usage(
const char *name,
const char *badparam);
74 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
75 bool& display,
bool& displayPoints, std::string& ipath);
85 void usage(
const char *name,
const char *badparam)
88 Test of detection of planar surface using a Fern classifier. The object needs \ 89 first to be learned (-l option). This learning process will create a file used\ 90 to detect the object.\n\ 93 %s [-l] [-h] [-b] [-c] [-d] [-p] [-i] [-s]\n", name);
100 -i <input image path> \n\ 101 Set image input path.\n\ 102 From this path read \"ViSP-images/line/image.%%04d.pgm\"\n\ 104 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 105 variable produces the same behaviour than using\n\ 109 database filename to use (default is ./dataPlanar).\n\ 112 Disable the mouse click. Useful to automaze the \n\ 113 execution of this program without humain intervention.\n\ 116 Turn off the display.\n\ 119 Turn off the use of the sequence and use a webcam.\n\ 122 display points of interest.\n\ 125 Print this help.\n");
128 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
148 bool getOptions(
int argc,
const char **argv,
bool &isLearning, std::string& dataFile,
bool& click_allowed,
149 bool& display,
bool& displayPoints, std::string& ipath)
156 case 'c': click_allowed =
false;
break;
157 case 'd': display =
false;
break;
158 case 'l': isLearning =
true;
break;
159 case 'h': usage(argv[0], NULL);
return false;
break;
160 case 'b': dataFile = optarg_;
break;
161 case 'p': displayPoints =
true;
break;
162 case 'i': ipath = optarg_;
break;
164 usage(argv[0], optarg_);
169 if ((c == 1) || (c == -1)) {
171 usage(argv[0], NULL);
172 std::cerr <<
"ERROR: " << std::endl;
173 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
183 main(
int argc,
const char** argv)
186 bool isLearning =
false;
187 std::string dataFile(
"./dataPlanar");
188 bool opt_click_allowed =
true;
189 bool opt_display =
true;
190 std::string objectName(
"object");
191 bool displayPoints =
false;
192 std::string opt_ipath;
194 std::string env_ipath;
196 std::string filename;
202 if (! env_ipath.empty()){
207 if (getOptions(argc, argv,
208 isLearning, dataFile, opt_click_allowed, opt_display, displayPoints, opt_ipath) ==
false) {
213 if (!opt_ipath.empty()){
219 if (!opt_ipath.empty() && !env_ipath.empty()) {
220 if (ipath != env_ipath) {
221 std::cout << std::endl
222 <<
"WARNING: " << std::endl;
223 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 224 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
225 <<
" we skip the environment variable." << std::endl;
230 if (opt_ipath.empty() && env_ipath.empty()){
231 usage(argv[0], NULL);
232 std::cerr << std::endl
233 <<
"ERROR:" << std::endl;
234 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " 236 <<
" environment variable to specify the location of the " << std::endl
237 <<
" image path where test images are located." << std::endl << std::endl;
250 std::ostringstream s;
251 s.setf(std::ios::right, std::ios::adjustfield);
252 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
262 std::cout <<
"Load: " << filename << std::endl;
272 std::cerr << std::endl
273 <<
"ERROR:" << std::endl;
274 std::cerr <<
" Cannot read " << filename << std::endl;
275 std::cerr <<
" Check your -i " << ipath <<
" option " << std::endl
276 <<
" or VISP_INPUT_IMAGE_PATH environment variable." 281 #if defined VISP_HAVE_X11 283 #elif defined VISP_HAVE_GTK 285 #elif defined VISP_HAVE_GDI 289 #if defined VISP_HAVE_X11 291 #elif defined VISP_HAVE_GTK 293 #elif defined VISP_HAVE_GDI 303 displayRef.
init(Iref, 100, 100,
"Reference image") ;
307 if (opt_display && opt_click_allowed){
308 std::cout <<
"Click on the top left and the bottom right corners to define the reference plane" << std::endl;
309 for (
int i=0 ; i < 2 ; i++){
311 std::cout << corners[i] << std::endl;
325 if (opt_click_allowed){
326 std::cout <<
"Click on the image to continue" << std::endl;
330 vpRect roi(corners[0], corners[1]);
332 std::cout <<
"> train the classifier on the selected plane (may take up to several minutes)." << std::endl;
346 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
351 planar.
load(dataFile, objectName);
354 vpERROR_TRACE(
"cannot load the database with the specified name. Has the object been learned with the -l option? ");
360 display.
init(I, 110 + (
int)Iref.
getWidth(), 100,
"Current image") ;
365 if (opt_display && opt_click_allowed){
366 std::cout <<
"Click on the reference image to continue" << std::endl;
368 "Click on the reference image to continue",
vpColor::red);
380 s <<
"image." << std::setw(4) << std::setfill(
'0') << iter <<
".pgm";
397 std::cout <<
" > computed homography:" << std::endl << H << std::endl;
402 planar.
display(Iref, I, displayPoints);
405 planar.
display(I, displayPoints);
410 std::cout <<
" > reference is not detected in the image" << std::endl;
423 std::cout <<
"Catch an exception: " << e << std::endl;
432 #if ( ! (defined (VISP_HAVE_X11) || defined(VISP_HAVE_GTK) || defined(VISP_HAVE_GDI)) ) 433 vpERROR_TRACE(
"You do not have X11, GTK or GDI display functionalities...");
435 vpERROR_TRACE(
"You do not have OpenCV-2.0.0 or a more recent release...");
unsigned int getWidth() const
void getHomography(vpHomography &_H) 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)
Define the X11 console to display images.
void load(const std::string &dataFilename, const std::string &objName)
Load the Fern classifier.
error that can be emited by ViSP classes.
static const vpColor green
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
bool matchPoint(const vpImage< unsigned char > &I)
Implementation of an homography and operations on homographies.
void display(vpImage< unsigned char > &I, bool displayKpts=false)
static void display(const vpImage< unsigned char > &I)
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
unsigned int buildReference(const vpImage< unsigned char > &I)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
VISP_EXPORT double measureTimeMs()
void recordDetector(const std::string &objectName, const std::string &dataFile)
Record the Ferns classifier in the text file.
unsigned int getHeight() const
Defines a rectangle in the plane.
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Class used to detect a planar surface.
static void read(vpImage< unsigned char > &I, const char *filename)
void set_ij(const double ii, const double jj)