4 #include <visp3/core/vpConfig.h>
5 #include <visp3/core/vpImageConvert.h>
6 #include <visp3/core/vpTime.h>
7 #include <visp3/gui/vpDisplayFactory.h>
8 #include <visp3/sensor/vpV4l2Grabber.h>
10 #if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS) && defined(VISP_HAVE_DISPLAY)
15 #ifdef ENABLE_VISP_NAMESPACE
20 typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
27 bool stop_capture_ =
false;
38 std::lock_guard<std::mutex> lock(mutex_capture);
39 if (capture_state == capture_stopped)
42 capture_state = capture_started;
48 std::lock_guard<std::mutex> lock(mutex_capture);
49 capture_state = capture_stopped;
51 std::cout <<
"End of capture thread" << std::endl;
56 void displayFunction(std::mutex &mutex_capture,
vpImage<unsigned char> &frame, t_CaptureState &capture_state)
60 t_CaptureState capture_state_;
61 bool display_initialized_ =
false;
66 capture_state_ = capture_state;
67 mutex_capture.unlock();
70 if (capture_state_ == capture_started) {
73 std::lock_guard<std::mutex> lock(mutex_capture);
78 if (!display_initialized_) {
81 display_initialized_ =
true;
90 std::lock_guard<std::mutex> lock(mutex_capture);
91 capture_state = capture_stopped;
100 }
while (capture_state_ != capture_stopped);
104 std::cout <<
"End of display thread" << std::endl;
109 int main(
int argc,
const char *argv[])
111 unsigned int opt_device = 0;
112 unsigned int opt_scale = 2;
116 for (
int i = 0; i < argc; i++) {
117 if (std::string(argv[i]) ==
"--camera_device")
118 opt_device = (
unsigned int)atoi(argv[i + 1]);
119 else if (std::string(argv[i]) ==
"--scale")
120 opt_scale = (
unsigned int)atoi(argv[i + 1]);
121 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"--h") {
122 std::cout <<
"Usage: " << argv[0]
123 <<
" [--camera_device <camera device (default: 0)>] [--scale <subsampling factor>]"
124 <<
" [--help] [-h]" << std::endl;
131 std::ostringstream device;
132 device <<
"/dev/video" << opt_device;
137 std::mutex mutex_capture;
138 t_CaptureState capture_state = capture_waiting;
141 std::thread thread_capture(&captureFunction, std::ref(g), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
142 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
145 thread_capture.join();
146 thread_display.join();
155 #ifndef VISP_HAVE_V4L2
156 std::cout <<
"You should enable V4L2 to make this example working..." << std::endl;
157 #elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
158 std::cout <<
"You should enable pthread usage and rebuild ViSP..." << std::endl;
159 #elif !defined(VISP_HAVE_DISPLAY)
160 std::cout <<
"You should have at least one GUI library installed to use this example." << std::endl;
162 std::cout <<
"Multi-threading seems not supported on this platform" << std::endl;
Class that defines generic functionalities for display.
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)
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()