4 #include <visp3/core/vpImageConvert.h>
5 #include <visp3/core/vpTime.h>
6 #include <visp3/gui/vpDisplayX.h>
7 #include <visp3/sensor/vpV4l2Grabber.h>
9 #if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS)
15 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
22 bool stop_capture_ =
false;
33 std::lock_guard<std::mutex> lock(mutex_capture);
34 if (capture_state == capture_stopped)
37 capture_state = capture_started;
43 std::lock_guard<std::mutex> lock(mutex_capture);
44 capture_state = capture_stopped;
46 std::cout <<
"End of capture thread" << std::endl;
51 void displayFunction(std::mutex &mutex_capture,
vpImage<unsigned char> &frame, t_CaptureState &capture_state)
55 t_CaptureState capture_state_;
56 bool display_initialized_ =
false;
57 #if defined(VISP_HAVE_X11)
63 capture_state_ = capture_state;
64 mutex_capture.unlock();
67 if (capture_state_ == capture_started) {
70 std::lock_guard<std::mutex> lock(mutex_capture);
75 if (!display_initialized_) {
77 #if defined(VISP_HAVE_X11)
79 display_initialized_ =
true;
89 std::lock_guard<std::mutex> lock(mutex_capture);
90 capture_state = capture_stopped;
99 }
while (capture_state_ != capture_stopped);
101 #if defined(VISP_HAVE_X11)
105 std::cout <<
"End of display thread" << std::endl;
110 int main(
int argc,
const char *argv[])
112 unsigned int opt_device = 0;
113 unsigned int opt_scale = 2;
117 for (
int i = 0; i < argc; i++) {
118 if (std::string(argv[i]) ==
"--camera_device")
119 opt_device = (
unsigned int)atoi(argv[i + 1]);
120 else if (std::string(argv[i]) ==
"--scale")
121 opt_scale = (
unsigned int)atoi(argv[i + 1]);
122 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"--h") {
123 std::cout <<
"Usage: " << argv[0]
124 <<
" [--camera_device <camera device (default: 0)>] [--scale <subsampling factor>]"
125 <<
" [--help] [-h]" << std::endl;
132 std::ostringstream device;
133 device <<
"/dev/video" << opt_device;
138 std::mutex mutex_capture;
139 t_CaptureState capture_state = capture_waiting;
142 std::thread thread_capture(&captureFunction, std::ref(g), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
143 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
146 thread_capture.join();
147 thread_display.join();
156 #ifndef VISP_HAVE_V4L2
157 std::cout <<
"You should enable V4L2 to make this example working..." << std::endl;
158 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
159 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
161 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
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)
Class that is a wrapper over the Video4Linux2 (V4L2) driver.
void open(vpImage< unsigned char > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void setDevice(const std::string &devname)
void acquire(vpImage< unsigned char > &I)
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()