5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/core/vpTime.h>
7 #include <visp3/gui/vpDisplayGDI.h>
8 #include <visp3/gui/vpDisplayX.h>
10 #if defined(HAVE_OPENCV_VIDEOIO) && defined(VISP_HAVE_THREADS)
15 #include <opencv2/highgui.hpp>
16 #include <opencv2/videoio.hpp>
19 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
23 void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
25 if (!cap.isOpened()) {
26 std::cout <<
"Unable to start capture" << std::endl;
32 while ((i++ < 100) && !cap.read(frame_)) {
35 bool stop_capture_ =
false;
44 std::lock_guard<std::mutex> lock(mutex_capture);
45 if (capture_state == capture_stopped)
48 capture_state = capture_started;
54 std::lock_guard<std::mutex> lock(mutex_capture);
55 capture_state = capture_stopped;
58 std::cout <<
"End of capture thread" << std::endl;
63 void displayFunction(std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
67 t_CaptureState capture_state_;
68 bool display_initialized_ =
false;
69 #if defined(VISP_HAVE_X11)
71 #elif defined(VISP_HAVE_GDI)
77 capture_state_ = capture_state;
78 mutex_capture.unlock();
81 if (capture_state_ == capture_started) {
85 std::lock_guard<std::mutex> lock(mutex_capture);
90 if (!display_initialized_) {
92 #if defined(VISP_HAVE_X11)
94 display_initialized_ =
true;
95 #elif defined(VISP_HAVE_GDI)
97 display_initialized_ =
true;
107 std::lock_guard<std::mutex> lock(mutex_capture);
108 capture_state = capture_stopped;
117 }
while (capture_state_ != capture_stopped);
119 #if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
123 std::cout <<
"End of display thread" << std::endl;
128 int main(
int argc,
const char *argv[])
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;
143 cv::VideoCapture cap;
144 cap.open(opt_device);
147 t_CaptureState capture_state = capture_waiting;
149 std::mutex mutex_capture;
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));
155 thread_capture.join();
156 thread_display.join();
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__)))
168 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
170 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
Display for windows using GDI (available on any windows 32 platform).
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
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()