Visual Servoing Platform  version 3.6.1 under development (2024-07-15)
tutorial-grabber-opencv-threaded.cpp
1 #include <iostream>
4 
5 #include <visp3/core/vpConfig.h>
6 #include <visp3/core/vpImageConvert.h>
7 #include <visp3/core/vpTime.h>
8 #include <visp3/gui/vpDisplayGDI.h>
9 #include <visp3/gui/vpDisplayX.h>
10 
11 #if defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_THREADS)
12 
13 #include <thread>
14 #include <mutex>
15 
16 #include <opencv2/highgui.hpp>
17 #include <opencv2/videoio.hpp>
18 
19 #ifdef ENABLE_VISP_NAMESPACE
20 using namespace VISP_NAMESPACE_NAME;
21 #endif
22 
23 // Possible capture states
24 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
26 
28 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
29 {
30  if (!cap.isOpened()) { // check if we succeeded
31  std::cout << "Unable to start capture" << std::endl;
32  return;
33  }
34 
35  cv::Mat frame_;
36  int i = 0;
37  while ((i++ < 100) && !cap.read(frame_)) {
38  }; // warm up camera by skipping unread frames
39 
40  bool stop_capture_ = false;
41 
42  double start_time = vpTime::measureTimeSecond();
43  while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
44  // Capture in progress
45  cap >> frame_; // get a new frame from camera
46 
47  // Update shared data
48  {
49  std::lock_guard<std::mutex> lock(mutex_capture);
50  if (capture_state == capture_stopped)
51  stop_capture_ = true;
52  else
53  capture_state = capture_started;
54  frame = frame_;
55  }
56  }
57 
58  {
59  std::lock_guard<std::mutex> lock(mutex_capture);
60  capture_state = capture_stopped;
61  }
62 
63  std::cout << "End of capture thread" << std::endl;
64 }
66 
68 void displayFunction(std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
69 {
71 
72  t_CaptureState capture_state_;
73  bool display_initialized_ = false;
74 #if defined(VISP_HAVE_X11)
75  vpDisplayX *d_ = nullptr;
76 #elif defined(VISP_HAVE_GDI)
77  vpDisplayGDI *d_ = nullptr;
78 #endif
79 
80  do {
81  mutex_capture.lock();
82  capture_state_ = capture_state;
83  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  std::lock_guard<std::mutex> lock(mutex_capture);
91  vpImageConvert::convert(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  std::lock_guard<std::mutex> lock(mutex_capture);
113  capture_state = capture_stopped;
114  }
115 
116  // Update the display
117  vpDisplay::flush(I_);
118  }
119  else {
120  vpTime::wait(2); // Sleep 2ms
121  }
122  } while (capture_state_ != capture_stopped);
123 
124 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
125  delete d_;
126 #endif
127 
128  std::cout << "End of display thread" << std::endl;
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]) == "--camera_device")
140  opt_device = atoi(argv[i + 1]);
141  else if (std::string(argv[i]) == "--help") {
142  std::cout << "Usage: " << argv[0] << " [--camera_device <camera device (default: 0)>] [--help]" << std::endl;
143  return EXIT_SUCCESS;
144  }
145  }
146 
147  // Instantiate the capture
148  cv::VideoCapture cap;
149  cap.open(opt_device);
150 
151  cv::Mat frame;
152  t_CaptureState capture_state = capture_waiting;
153  // Create a mutex for capture
154  std::mutex mutex_capture;
155  // Start the threads
156  std::thread thread_capture(&captureFunction, std::ref(cap), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
157  std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
158 
159  // Wait until thread ends up
160  thread_capture.join();
161  thread_display.join();
162 
163  return EXIT_SUCCESS;
164 }
166 
167 #else
168 int main()
169 {
170 #ifndef VISP_HAVE_OPENCV
171  std::cout << "You should install OpenCV videoio module to make this example working..." << std::endl;
172 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
173  std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
174 #else
175  std::cout << "Multi-threading seems not supported on this platform" << std::endl;
176 #endif
177  return EXIT_SUCCESS;
178 }
179 
180 #endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:130
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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 displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()