5#include <visp3/core/vpConfig.h>
7#if defined(VISP_HAVE_OPENCV) && defined(VISP_HAVE_THREADS) && \
8 (((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
13#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
14#include <opencv2/highgui/highgui.hpp>
15#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
16#include <opencv2/videoio/videoio.hpp>
19#include <visp3/core/vpImageConvert.h>
20#include <visp3/core/vpTime.h>
21#include <visp3/gui/vpDisplayFactory.h>
23#ifdef ENABLE_VISP_NAMESPACE
28typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
32void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
34 if (!cap.isOpened()) {
35 std::cout <<
"Unable to start capture" << std::endl;
41 while ((i++ < 100) && !cap.read(frame_)) {
44 bool stop_capture_ =
false;
53 std::lock_guard<std::mutex> lock(mutex_capture);
54 if (capture_state == capture_stopped)
57 capture_state = capture_started;
63 std::lock_guard<std::mutex> lock(mutex_capture);
64 capture_state = capture_stopped;
67 std::cout <<
"End of capture thread" << std::endl;
72void displayFunction(std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
76 t_CaptureState capture_state_;
77 bool display_initialized_ =
false;
78#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
79 std::shared_ptr<vpDisplay> display;
86 capture_state_ = capture_state;
87 mutex_capture.unlock();
90 if (capture_state_ == capture_started) {
94 std::lock_guard<std::mutex> lock(mutex_capture);
99 if (!display_initialized_) {
101#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
103 display_initialized_ =
true;
106 display_initialized_ =
true;
116 std::lock_guard<std::mutex> lock(mutex_capture);
117 capture_state = capture_stopped;
126 }
while (capture_state_ != capture_stopped);
128#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
129 if (display !=
nullptr) {
134 std::cout <<
"End of display thread" << std::endl;
139int main(
int argc,
const char *argv[])
144 for (
int i = 1;
i < argc;
i++) {
145 if (std::string(argv[i]) ==
"--camera-device" && i + 1 < argc) {
146 opt_device = atoi(argv[++i]);
148 else if (std::string(argv[i]) ==
"--help") {
149 std::cout <<
"Usage: " << argv[0] <<
" [--camera-device <camera device (default: 0)>] [--help]" << std::endl;
155 cv::VideoCapture cap;
156 cap.open(opt_device);
159 t_CaptureState capture_state = capture_waiting;
161 std::mutex mutex_capture;
163 std::thread thread_capture(&captureFunction, std::ref(cap), std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
164 std::thread thread_display(&displayFunction, std::ref(mutex_capture), std::ref(frame), std::ref(capture_state));
167 thread_capture.join();
168 thread_display.join();
177#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION < 0x030000) && !defined(HAVE_OPENCV_HIGHGUI)
178 std::cout <<
"Install OpenCV highgui module, configure and build ViSP again to use this tutorial." << std::endl;
180#if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000) && !defined(HAVE_OPENCV_VIDEOIO)
181 std::cout <<
"Install OpenCV videoio module, configure and build ViSP again to use this tutorial." << std::endl;
183#if !defined(HAVE_OPENCV_HIGHGUI)
184 std::cout <<
"Install OpenCV highgui module, configure and build ViSP again to use this tutorial." << std::endl;
186#if !defined(VISP_HAVE_THREADS)
187 std::cout <<
"This tutorial cannot run without std::thread usage." << std::endl;
188 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)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
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()