Visual Servoing Platform  version 3.1.0
tutorial-grabber-opencv-threaded.cpp
1 #include <iostream>
4 
5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/core/vpMutex.h>
7 #include <visp3/core/vpThread.h>
8 #include <visp3/core/vpTime.h>
9 #include <visp3/gui/vpDisplayGDI.h>
10 #include <visp3/gui/vpDisplayX.h>
11 
12 #if (VISP_HAVE_OPENCV_VERSION >= 0x020100) && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32))
13 
14 #include <opencv2/highgui/highgui.hpp>
15 
16 // Shared vars
17 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
18 t_CaptureState s_capture_state = capture_waiting;
19 cv::Mat s_frame;
20 vpMutex s_mutex_capture;
22 
24 vpThread::Return captureFunction(vpThread::Args args)
25 {
26  cv::VideoCapture cap = *((cv::VideoCapture *)args);
27 
28  if (!cap.isOpened()) { // check if we succeeded
29  std::cout << "Unable to start capture" << std::endl;
30  return 0;
31  }
32 
33  cv::Mat frame_;
34  int i = 0;
35  while ((i++ < 100) && !cap.read(frame_)) {
36  }; // warm up camera by skiping unread frames
37 
38  bool stop_capture_ = false;
39 
40  double start_time = vpTime::measureTimeSecond();
41  while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
42  // Capture in progress
43  cap >> frame_; // get a new frame from camera
44 
45  // Update shared data
46  {
47  vpMutex::vpScopedLock lock(s_mutex_capture);
48  if (s_capture_state == capture_stopped)
49  stop_capture_ = true;
50  else
51  s_capture_state = capture_started;
52  s_frame = frame_;
53  }
54  }
55 
56  {
57  vpMutex::vpScopedLock lock(s_mutex_capture);
58  s_capture_state = capture_stopped;
59  }
60 
61  std::cout << "End of capture thread" << std::endl;
62  return 0;
63 }
65 
67 vpThread::Return displayFunction(vpThread::Args args)
68 {
69  (void)args; // Avoid warning: unused parameter args
71 
72  t_CaptureState capture_state_;
73  bool display_initialized_ = false;
74 #if defined(VISP_HAVE_X11)
75  vpDisplayX *d_ = NULL;
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI *d_ = NULL;
78 #endif
79 
80  do {
81  s_mutex_capture.lock();
82  capture_state_ = s_capture_state;
83  s_mutex_capture.unlock();
84 
85  // Check if a frame is available
86  if (capture_state_ == capture_started) {
87  // Get the frame and convert it to a ViSP image used by the display
88  // class
89  {
90  vpMutex::vpScopedLock lock(s_mutex_capture);
91  vpImageConvert::convert(s_frame, I_);
92  }
93 
94  // Check if we need to initialize the display with the first frame
95  if (!display_initialized_) {
96 // Initialize the display
97 #if defined(VISP_HAVE_X11)
98  d_ = new vpDisplayX(I_);
99  display_initialized_ = true;
100 #elif defined(VISP_HAVE_GDI)
101  d_ = new vpDisplayGDI(I_);
102  display_initialized_ = true;
103 #endif
104  }
105 
106  // Display the image
107  vpDisplay::display(I_);
108 
109  // Trigger end of acquisition with a mouse click
110  vpDisplay::displayText(I_, 10, 10, "Click to exit...", vpColor::red);
111  if (vpDisplay::getClick(I_, false)) {
112  vpMutex::vpScopedLock lock(s_mutex_capture);
113  s_capture_state = capture_stopped;
114  }
115 
116  // Update the display
117  vpDisplay::flush(I_);
118  } else {
119  vpTime::wait(2); // Sleep 2ms
120  }
121  } while (capture_state_ != capture_stopped);
122 
123 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
124  delete d_;
125 #endif
126 
127  std::cout << "End of display thread" << std::endl;
128  return 0;
129 }
131 
133 int main(int argc, const char *argv[])
134 {
135  int opt_device = 0;
136 
137  // Command line options
138  for (int i = 0; i < argc; i++) {
139  if (std::string(argv[i]) == "--device")
140  opt_device = atoi(argv[i + 1]);
141  else if (std::string(argv[i]) == "--help") {
142  std::cout << "Usage: " << argv[0] << " [--device <camera device>] [--help]" << std::endl;
143  return 0;
144  }
145  }
146 
147  // Instanciate the capture
148  cv::VideoCapture cap;
149  cap.open(opt_device);
150 
151  // Start the threads
152  vpThread thread_capture((vpThread::Fn)captureFunction, (vpThread::Args)&cap);
153  vpThread thread_display((vpThread::Fn)displayFunction);
154 
155  // Wait until thread ends up
156  thread_capture.join();
157  thread_display.join();
158 
159  return 0;
160 }
162 
163 #else
164 int main()
165 {
166 #ifndef VISP_HAVE_OPENCV
167  std::cout << "You should install OpenCV to make this example working..." << std::endl;
168 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
169  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
170 #else
171  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
172 #endif
173 }
174 
175 #endif
Class that allows protection by mutex.
Definition: vpMutex.h:170
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
void lock()
Definition: vpMutex.h:95
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void * Return
Definition: vpThread.h:41
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
VISP_EXPORT double measureTimeSecond()
Definition: vpTime.cpp:250
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
void unlock()
Definition: vpMutex.h:111
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
static void flush(const vpImage< unsigned char > &I)
static const vpColor red
Definition: vpColor.h:180
void *(* Fn)(Args)
Definition: vpThread.h:42
void * Args
Definition: vpThread.h:40
static void display(const vpImage< unsigned char > &I)