Visual Servoing Platform  version 3.5.1 under development (2022-07-05)
tutorial-grabber-opencv-threaded.cpp

[capture-multi-threaded declaration]

#include <iostream>
#include <visp3/core/vpImageConvert.h>
#include <visp3/core/vpMutex.h>
#include <visp3/core/vpThread.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGDI.h>
#include <visp3/gui/vpDisplayX.h>
#if (VISP_HAVE_OPENCV_VERSION >= 0x020100) && (defined(VISP_HAVE_PTHREAD) || defined(_WIN32))
#include <opencv2/highgui/highgui.hpp>
// Shared vars
typedef enum { capture_waiting, capture_started, capture_stopped } t_CaptureState;
t_CaptureState s_capture_state = capture_waiting;
cv::Mat s_frame;
vpMutex s_mutex_capture;
vpThread::Return captureFunction(vpThread::Args args)
{
cv::VideoCapture cap = *((cv::VideoCapture *)args);
if (!cap.isOpened()) { // check if we succeeded
std::cout << "Unable to start capture" << std::endl;
return 0;
}
cv::Mat frame_;
int i = 0;
while ((i++ < 100) && !cap.read(frame_)) {
}; // warm up camera by skiping unread frames
bool stop_capture_ = false;
double start_time = vpTime::measureTimeSecond();
while ((vpTime::measureTimeSecond() - start_time) < 30 && !stop_capture_) {
// Capture in progress
cap >> frame_; // get a new frame from camera
// Update shared data
{
vpMutex::vpScopedLock lock(s_mutex_capture);
if (s_capture_state == capture_stopped)
stop_capture_ = true;
else
s_capture_state = capture_started;
s_frame = frame_;
}
}
{
vpMutex::vpScopedLock lock(s_mutex_capture);
s_capture_state = capture_stopped;
}
std::cout << "End of capture thread" << std::endl;
return 0;
}
vpThread::Return displayFunction(vpThread::Args args)
{
(void)args; // Avoid warning: unused parameter args
t_CaptureState capture_state_;
bool display_initialized_ = false;
#if defined(VISP_HAVE_X11)
vpDisplayX *d_ = NULL;
#elif defined(VISP_HAVE_GDI)
vpDisplayGDI *d_ = NULL;
#endif
do {
s_mutex_capture.lock();
capture_state_ = s_capture_state;
s_mutex_capture.unlock();
// Check if a frame is available
if (capture_state_ == capture_started) {
// Get the frame and convert it to a ViSP image used by the display
// class
{
vpMutex::vpScopedLock lock(s_mutex_capture);
}
// Check if we need to initialize the display with the first frame
if (!display_initialized_) {
// Initialize the display
#if defined(VISP_HAVE_X11)
d_ = new vpDisplayX(I_);
display_initialized_ = true;
#elif defined(VISP_HAVE_GDI)
d_ = new vpDisplayGDI(I_);
display_initialized_ = true;
#endif
}
// Display the image
// Trigger end of acquisition with a mouse click
vpDisplay::displayText(I_, 10, 10, "Click to exit...", vpColor::red);
if (vpDisplay::getClick(I_, false)) {
vpMutex::vpScopedLock lock(s_mutex_capture);
s_capture_state = capture_stopped;
}
// Update the display
} else {
vpTime::wait(2); // Sleep 2ms
}
} 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;
return 0;
}
int main(int argc, const char *argv[])
{
int opt_device = 0;
// Command line options
for (int i = 0; i < argc; i++) {
if (std::string(argv[i]) == "--camera_device")
opt_device = atoi(argv[i + 1]);
else if (std::string(argv[i]) == "--help") {
std::cout << "Usage: " << argv[0] << " [--camera_device <camera device (default: 0)>] [--help]" << std::endl;
return 0;
}
}
// Instanciate the capture
cv::VideoCapture cap;
cap.open(opt_device);
// Start the threads
vpThread thread_capture((vpThread::Fn)captureFunction, (vpThread::Args)&cap);
vpThread thread_display((vpThread::Fn)displayFunction);
// Wait until thread ends up
thread_capture.join();
thread_display.join();
return 0;
}
#else
int main()
{
#ifndef VISP_HAVE_OPENCV
std::cout << "You should install OpenCV to make this example working..." << std::endl;
#elif !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
std::cout << "You should enable pthread usage and rebuild ViSP..." << std::endl;
#else
std::cout << "Multi-threading seems not supported on this platform" << std::endl;
#endif
}
#endif
static const vpColor red
Definition: vpColor.h:217
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:129
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:135
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)
Class that allows protection by mutex.
Definition: vpMutex.h:171
void unlock()
Definition: vpMutex.h:111
void lock()
Definition: vpMutex.h:95
void *(* Fn)(Args)
Definition: vpThread.h:79
void * Return
Definition: vpThread.h:78
void * Args
Definition: vpThread.h:77
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()