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>
11 #if defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_THREADS)
16 #include <opencv2/highgui.hpp>
17 #include <opencv2/videoio.hpp>
19 #ifdef ENABLE_VISP_NAMESPACE
24 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
28 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
30 if (!cap.isOpened()) {
31 std::cout <<
"Unable to start capture" << std::endl;
37 while ((i++ < 100) && !cap.read(frame_)) {
40 bool stop_capture_ =
false;
49 std::lock_guard<std::mutex> lock(mutex_capture);
50 if (capture_state == capture_stopped)
53 capture_state = capture_started;
59 std::lock_guard<std::mutex> lock(mutex_capture);
60 capture_state = capture_stopped;
63 std::cout <<
"End of capture thread" << std::endl;
68 void displayFunction(std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
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)
82 capture_state_ = capture_state;
83 mutex_capture.unlock();
86 if (capture_state_ == capture_started) {
90 std::lock_guard<std::mutex> lock(mutex_capture);
95 if (!display_initialized_) {
97 #if defined(VISP_HAVE_X11)
98 d_ =
new vpDisplayX(I_);
99 display_initialized_ =
true;
100 #elif defined(VISP_HAVE_GDI)
102 display_initialized_ =
true;
112 std::lock_guard<std::mutex> lock(mutex_capture);
113 capture_state = capture_stopped;
122 }
while (capture_state_ != capture_stopped);
124 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
128 std::cout <<
"End of display thread" << std::endl;
133 int main(
int argc,
const char *argv[])
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;
148 cv::VideoCapture cap;
149 cap.open(opt_device);
152 t_CaptureState capture_state = capture_waiting;
154 std::mutex mutex_capture;
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));
160 thread_capture.join();
161 thread_display.join();
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__)))
173 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
175 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
Display for windows using GDI (available on any windows 32 platform).
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()