#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_THREADS) && \
(((VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)) || ((VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)))
#include <thread>
#include <mutex>
#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && defined(HAVE_OPENCV_HIGHGUI)
#include <opencv2/highgui/highgui.hpp>
#elif (VISP_HAVE_OPENCV_VERSION >= 0x030000) && defined(HAVE_OPENCV_VIDEOIO)
#include <opencv2/videoio/videoio.hpp>
#endif
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayX.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
void captureFunction(cv::VideoCapture &cap, std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
{
if (!cap.isOpened()) {
std::cout << "Unable to start capture" << std::endl;
return;
}
cv::Mat frame_;
int i = 0;
while ((i++ < 100) && !cap.read(frame_)) {
};
bool stop_capture_ = false;
cap >> frame_;
{
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;
}
void displayFunction(std::mutex &mutex_capture, cv::Mat &frame, t_CaptureState &capture_state)
{
t_CaptureState capture_state_;
bool display_initialized_ = false;
#if defined(VISP_HAVE_X11)
vpDisplayX *d_ = nullptr;
#elif defined(VISP_HAVE_GDI)
#endif
do {
mutex_capture.lock();
capture_state_ = capture_state;
mutex_capture.unlock();
if (capture_state_ == capture_started) {
{
std::lock_guard<std::mutex> lock(mutex_capture);
}
if (!display_initialized_) {
#if defined(VISP_HAVE_X11)
d_ = new vpDisplayX(I_);
display_initialized_ = true;
#elif defined(VISP_HAVE_GDI)
display_initialized_ = true;
#endif
}
std::lock_guard<std::mutex> lock(mutex_capture);
capture_state = capture_stopped;
}
}
else {
}
} while (capture_state_ != capture_stopped);
#if defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI)
delete d_;
#endif
std::cout << "End of display thread" << std::endl;
}
int main(int argc, const char *argv[])
{
int opt_device = 0;
for (int i = 1; i < argc; i++) {
if (std::string(argv[i]) == "--camera-device" && i + 1 < argc) {
opt_device = atoi(argv[++i]);
}
else if (std::string(argv[i]) == "--help") {
std::cout << "Usage: " << argv[0] << " [--camera-device <camera device (default: 0)>] [--help]" << std::endl;
return EXIT_SUCCESS;
}
}
cv::VideoCapture cap;
cap.open(opt_device);
cv::Mat frame;
t_CaptureState capture_state = capture_waiting;
std::mutex mutex_capture;
std::thread thread_capture(&captureFunction, std::ref(cap), 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()
{
#if (VISP_HAVE_OPENCV_VERSION < 0x030000) && !defined(HAVE_OPENCV_HIGHGUI)
std::cout << "Install OpenCV highgui module, configure and build ViSP again to use this tutorial." << std::endl;
#endif
#if (VISP_HAVE_OPENCV_VERSION >= 0x030000) && !defined(HAVE_OPENCV_VIDEOIO)
std::cout << "Install OpenCV videoio module, configure and build ViSP again to use this tutorial." << std::endl;
#endif
#if !defined(HAVE_OPENCV_HIGHGUI)
std::cout << "Install OpenCV highgui module, configure and build ViSP again to use this tutorial." << std::endl;
#endif
#if !defined(VISP_HAVE_THREADS)
std::cout << "This tutorial cannot run without std::thread usage." << std::endl;
std::cout << "Multi-threading seems not supported on this platform" << std::endl;
#endif
return EXIT_SUCCESS;
}
#endif
Display for windows using GDI (available on any windows 32 platform).
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()