#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayFactory.h>
#include <visp3/sensor/vpV4l2Grabber.h>
#if defined(VISP_HAVE_V4L2) && defined(VISP_HAVE_THREADS) && defined(VISP_HAVE_DISPLAY)
#include <thread>
#include <mutex>
#ifdef ENABLE_VISP_NAMESPACE
#endif
typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
{
bool stop_capture_ = false;
{
std::lock_guard<std::mutex> lock(mutex_capture);
if (capture_state == capture_stopped)
stop_capture_ = true;
else
capture_state = capture_started;
frame = frame_;
}
}
{
std::lock_guard<std::mutex> lock(mutex_capture);
capture_state = capture_stopped;
}
std::cout << "End of capture thread" << std::endl;
}
{
t_CaptureState capture_state_;
bool display_initialized_ = false;
do {
mutex_capture.lock();
capture_state_ = capture_state;
mutex_capture.unlock();
if (capture_state_ == capture_started) {
{
std::lock_guard<std::mutex> lock(mutex_capture);
I_ = frame;
}
if (!display_initialized_) {
d_->init(I_);
display_initialized_ = true;
}
std::lock_guard<std::mutex> lock(mutex_capture);
capture_state = capture_stopped;
}
}
else {
}
} while (capture_state_ != capture_stopped);
delete d_;
std::cout << "End of display thread" << std::endl;
}
int main(int argc, const char *argv[])
{
unsigned int opt_device = 0;
unsigned int opt_scale = 2;
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--camera_device")
opt_device = (unsigned int)atoi(argv[i + 1]);
else if (std::string(argv[i]) == "--scale")
opt_scale = (unsigned int)atoi(argv[i + 1]);
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "--h") {
std::cout << "Usage: " << argv[0]
<< " [--camera_device <camera device (default: 0)>] [--scale <subsampling factor>]"
<< " [--help] [-h]" << std::endl;
return EXIT_SUCCESS;
}
}
std::ostringstream device;
device << "/dev/video" << opt_device;
std::mutex mutex_capture;
t_CaptureState capture_state = capture_waiting;
std::thread thread_capture(&captureFunction, std::ref(g), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
thread_capture.join();
thread_display.join();
return EXIT_SUCCESS;
}
#else
int main()
{
#ifndef VISP_HAVE_V4L2
std::cout << "You should enable V4L2 to make this example working..." << std::endl;
#elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
#elif !defined(VISP_HAVE_DISPLAY)
std::cout << "You should have at least one GUI library installed to use this example." << std::endl;
#else
std::cout << "Multi-threading seems not supported on this platform" << std::endl;
#endif
}
#endif
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()