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