Visual Servoing Platform  version 3.0.0
tutorial-blob-tracker-live-v4l2.cpp
1 #include <visp3/core/vpConfig.h>
3 #ifdef VISP_HAVE_MODULE_SENSOR
4 #include <visp3/sensor/vpV4l2Grabber.h>
5 #endif
6 #include <visp3/gui/vpDisplayGDI.h>
7 #include <visp3/gui/vpDisplayGTK.h>
8 #include <visp3/gui/vpDisplayOpenCV.h>
9 #include <visp3/gui/vpDisplayX.h>
10 #include <visp3/blob/vpDot2.h>
11 
12 int main()
13 {
14 #if ((defined(VISP_HAVE_V4L2) || (VISP_HAVE_OPENCV_VERSION >= 0x020100)) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_OPENCV) || defined(VISP_HAVE_GTK)))
15  vpImage<unsigned char> I; // Create a gray level image container
16 
17 #if defined(VISP_HAVE_V4L2)
18  vpV4l2Grabber g;
19  g.open(I);
20 #elif defined(VISP_HAVE_OPENCV)
21  cv::VideoCapture g(0); // open the default camera
22  if(!g.isOpened()) { // check if we succeeded
23  std::cout << "Failed to open the camera" << std::endl;
24  return -1;
25  }
26  cv::Mat frame;
27  g >> frame; // get a new frame from camera
28  vpImageConvert::convert(frame, I);
29 #endif
30 
31 #if defined(VISP_HAVE_X11)
32  vpDisplayX d(I, 0, 0, "Camera view");
33 #elif defined(VISP_HAVE_GDI)
34  vpDisplayGDI d(I, 0, 0, "Camera view");
35 #elif defined(VISP_HAVE_OPENCV)
36  vpDisplayOpenCV d(I, 0, 0, "Camera view");
37 #elif defined(VISP_HAVE_GTK)
38  vpDisplayGTK d(I, 0, 0, "Camera view");
39 #endif
40 
41  vpDot2 blob;
42  blob.setGraphics(true);
43  blob.setGraphicsThickness(2);
44 
45  vpImagePoint germ;
46  bool init_done = false;
47  std::cout << "Click!!!" << std::endl;
48  while(1) {
49  try {
50 #if defined(VISP_HAVE_V4L2)
51  g.acquire(I);
52 #elif defined(VISP_HAVE_OPENCV)
53  g >> frame;
54  vpImageConvert::convert(frame, I);
55 #endif
57 
58  if (! init_done) {
59  vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
60  if (vpDisplay::getClick(I, germ, false)) {
61  blob.initTracking(I, germ);
62  init_done = true;
63  }
64  }
65  else {
66  blob.track(I);
67  }
69  }
70  catch(...) {
71  init_done = false;
72  }
73  }
74 #endif
75 }
void acquire(vpImage< unsigned char > &I)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:128
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:888
Define the X11 console to display images.
Definition: vpDisplayX.h:148
This tracker is meant to track a blob (connex pixels with same gray level) on a vpImage.
Definition: vpDot2.h:124
void track(const vpImage< unsigned char > &I)
Definition: vpDot2.cpp:461
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2233
static const vpColor red
Definition: vpColor.h:163
void setGraphicsThickness(unsigned int t)
Definition: vpDot2.h:315
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:141
Class for the Video4Linux2 video device.
void initTracking(const vpImage< unsigned char > &I, unsigned int size=0)
Definition: vpDot2.cpp:262
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 ...
Definition: vpImagePoint.h:88
void setGraphics(const bool activate)
Definition: vpDot2.h:309