Example of using V4l2 backend to capture multiple camera streams with C++11 threads.
#include <iostream>
#include <visp3/core/vpConfig.h>
#if defined(VISP_HAVE_V4L2) && (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK)) && defined(VISP_HAVE_THREADS)
#include <condition_variable>
#include <iostream>
#include <limits>
#include <mutex>
#include <queue>
#include <thread>
#include <visp3/core/vpDisplay.h>
#include <visp3/core/vpImageFilter.h>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpTime.h>
#include <visp3/gui/vpDisplayGTK.h>
#include <visp3/gui/vpDisplayX.h>
#include <visp3/io/vpParseArgv.h>
#include <visp3/io/vpVideoWriter.h>
#include <visp3/sensor/vpV4l2Grabber.h>
#define GETOPTARGS "d:oh"
#ifdef ENABLE_VISP_NAMESPACE
#endif
namespace
{
void usage(const char *name, const char *badparam)
{
fprintf(stdout, "\n\
SYNOPSIS:\n\
%s [-d <device count>] [-o] [-h]\n\
\n\
DESCRIPTION:\n\
Capture multiple camera streams and save the stream without slowing down the acquisition.\n\
\n\
OPTIONS: \n\
-d <device count> \n\
Open the specified number of camera streams.\n\
\n\
-o \n\
Save each stream in a dedicated folder.\n\
\n\
-h \n\
Print the help.\n\n",
name);
if (badparam)
fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
}
bool getOptions(int argc, char **argv, unsigned int &deviceCount, bool &saveVideo)
{
const char *optarg;
const char **argv1 = (const char **)argv;
int c;
switch (c) {
case 'd':
deviceCount = (unsigned int)atoi(optarg);
break;
case 'o':
saveVideo = true;
break;
case 'h':
usage(argv[0], nullptr);
return false;
break;
default:
usage(argv[0], optarg);
return false;
break;
}
}
if ((c == 1) || (c == -1)) {
usage(argv[0], nullptr);
std::cerr << "ERROR: " << std::endl;
std::cerr << " Bad argument " << optarg << std::endl << std::endl;
return false;
}
return true;
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
class vpFrameQueue
{
public:
struct vpCancelled_t
{ };
vpFrameQueue()
: m_cancelled(false), m_cond(), m_queueColor(), m_maxQueueSize(std::numeric_limits<size_t>::max()), m_mutex()
{ }
void cancel()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_cancelled = true;
m_cond.notify_all();
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_queueColor.push(image);
while (m_queueColor.size() > m_maxQueueSize) {
m_queueColor.pop();
}
m_cond.notify_one();
}
{
std::unique_lock<std::mutex> lock(m_mutex);
while (m_queueColor.empty()) {
if (m_cancelled) {
throw vpCancelled_t();
}
m_cond.wait(lock);
if (m_cancelled) {
throw vpCancelled_t();
}
}
m_queueColor.pop();
return image;
}
void setMaxQueueSize(const size_t max_queue_size) { m_maxQueueSize = max_queue_size; }
private:
bool m_cancelled;
std::condition_variable m_cond;
std::queue<vpImage<vpRGBa> > m_queueColor;
size_t m_maxQueueSize;
std::mutex m_mutex;
};
class vpStorageWorker
{
public:
vpStorageWorker(vpFrameQueue &queue, const std::string &filename, unsigned int width, unsigned int height)
: m_queue(queue), m_filename(filename), m_width(width), m_height(height)
{ }
void run()
{
if (!m_filename.empty()) {
}
try {
for (;;) {
if (!m_filename.empty()) {
}
}
}
catch (vpFrameQueue::vpCancelled_t &) {
}
}
private:
vpFrameQueue &m_queue;
std::string m_filename;
unsigned int m_width;
unsigned int m_height;
};
class vpShareImage
{
private:
bool m_cancelled;
std::condition_variable m_cond;
std::mutex m_mutex;
unsigned char *m_pImgData;
unsigned int m_totalSize;
public:
struct vpCancelled_t
{ };
vpShareImage() : m_cancelled(false), m_cond(), m_mutex(), m_pImgData(nullptr), m_totalSize(0) { }
virtual ~vpShareImage()
{
if (m_pImgData != nullptr) {
delete[] m_pImgData;
}
}
void cancel()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_cancelled = true;
m_cond.notify_all();
}
void getImage(unsigned char *const imageData, const unsigned int totalSize)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_cancelled) {
throw vpCancelled_t();
}
m_cond.wait(lock);
if (m_cancelled) {
throw vpCancelled_t();
}
if (totalSize <= m_totalSize) {
memcpy(imageData, m_pImgData, totalSize * sizeof(unsigned char));
}
else {
std::cerr << "totalSize <= m_totalSize !" << std::endl;
}
}
bool isCancelled()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_cancelled;
}
void setImage(const unsigned char *const imageData, const unsigned int totalSize)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_pImgData == nullptr || m_totalSize != totalSize) {
m_totalSize = totalSize;
if (m_pImgData != nullptr) {
delete[] m_pImgData;
}
m_pImgData = new unsigned char[m_totalSize];
}
memcpy(m_pImgData, imageData, m_totalSize * sizeof(unsigned char));
m_cond.notify_one();
}
};
void capture(
vpV4l2Grabber *
const pGrabber, vpShareImage &share_image)
{
pGrabber->
open(local_img);
while (true) {
if (share_image.isCancelled()) {
break;
}
share_image.setImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
}
}
void display(unsigned int width, unsigned int height, int win_x, int win_y, unsigned int deviceId,
vpShareImage &share_image, vpFrameQueue &queue, bool save)
{
#if defined(VISP_HAVE_X11)
vpDisplayX display;
#elif defined(VISP_HAVE_GTK)
#endif
{
std::stringstream ss;
ss << "Camera stream " << deviceId;
display.init(local_img, win_x, win_y, ss.str());
}
try {
vpImage<unsigned char> I_red(height, width), I_green(height, width), I_blue(height, width), I_alpha(height, width);
I_blue_gaussian(height, width);
vpImage<double> I_red_gaussian_double, I_green_gaussian_double, I_blue_gaussian_double;
bool exit = false, gaussian_blur = false;
while (!exit) {
share_image.getImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
if (gaussian_blur) {
}
std::stringstream ss;
ss << "Time: " << t << " ms";
if (save) {
queue.push(local_img);
}
switch (button) {
gaussian_blur = !gaussian_blur;
break;
default:
exit = true;
break;
}
}
}
}
catch (vpShareImage::vpCancelled_t &) {
std::cout << "Cancelled!" << std::endl;
}
share_image.cancel();
}
}
#endif
int main(int argc, char *argv[])
{
unsigned int deviceCount = 1;
unsigned int cameraScale = 1;
bool saveVideo = false;
if (!getOptions(argc, argv, deviceCount, saveVideo)) {
return EXIT_FAILURE;
}
std::vector<vpV4l2Grabber *> grabbers;
const unsigned int offsetX = 100, offsetY = 100;
for (unsigned int devicedId = 0; devicedId < deviceCount; devicedId++) {
try {
std::stringstream ss;
ss << "/dev/video" << devicedId;
grabbers.push_back(pGrabber);
}
std::cerr <<
"Exception: " << e.
what() << std::endl;
}
}
std::cout << "Grabbers: " << grabbers.size() << std::endl;
std::vector<vpShareImage> share_images(grabbers.size());
std::vector<std::thread> capture_threads;
std::vector<std::thread> display_threads;
std::vector<vpFrameQueue> save_queues(grabbers.size());
std::vector<vpStorageWorker> storages;
std::vector<std::thread> storage_threads;
for (size_t deviceId = 0; deviceId < grabbers.size(); deviceId++) {
capture_threads.emplace_back(capture, grabbers[deviceId], std::ref(share_images[deviceId]));
int win_x = deviceId * offsetX, win_y = deviceId * offsetY;
display_threads.emplace_back(display, grabbers[deviceId]->getWidth(), grabbers[deviceId]->getHeight(), win_x, win_y,
deviceId, std::ref(share_images[deviceId]), std::ref(save_queues[deviceId]),
saveVideo);
if (saveVideo) {
std::stringstream ss;
ss << parent_directory << "/Camera_Stream" << deviceId;
std::cout << "Create directory: " << ss.str() << std::endl;
ss << "/%06d.png";
std::string filename = ss.str();
storages.emplace_back(std::ref(save_queues[deviceId]), std::cref(filename), grabbers[deviceId]->getWidth(),
grabbers[deviceId]->getHeight());
}
}
if (saveVideo) {
for (auto &s : storages) {
storage_threads.emplace_back(&vpStorageWorker::run, &s);
}
}
for (auto &ct : capture_threads) {
ct.join();
}
for (auto &dt : display_threads) {
dt.join();
}
for (auto &g : grabbers) {
delete g;
}
if (saveVideo) {
std::cout << "\nWaiting for finishing thread to write images..." << std::endl;
}
for (auto &qu : save_queues) {
qu.cancel();
}
for (auto &st : storage_threads) {
st.join();
}
return EXIT_SUCCESS;
}
#else
#if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK))
int main()
{
std::cout << "You do not have X11, or GTK functionalities to display images..." << std::endl;
std::cout << "Tip if you are on a unix-like system:" << std::endl;
std::cout << "- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
std::cout << "Tip if you are on a windows-like system:" << std::endl;
std::cout << "- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
return EXIT_SUCCESS;
}
#elif !defined(VISP_HAVE_V4L2)
int main()
{
std::cout << "You do not have Video 4 Linux 2 functionality enabled" << std::endl;
std::cout << "Tip if you are on a unix-like system:" << std::endl;
std::cout << "- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
return EXIT_SUCCESS;
}
#else
int main()
{
std::cout << "You do not build ViSP with c++11 or higher compiler flag" << std::endl;
std::cout << "Tip:" << std::endl;
std::cout << "- Configure ViSP again using cmake -DUSE_CXX_STANDARD=11, and build again this example" << std::endl;
return EXIT_SUCCESS;
}
#endif
#endif
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
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)
error that can be emitted by ViSP classes.
const char * what() const
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void gaussianBlur(const vpImage< ImageType > &I, vpImage< FilterType > &GI, unsigned int size=7, FilterType sigma=0., bool normalize=true, const vpImage< bool > *p_mask=nullptr)
unsigned int getSize() const
Type * bitmap
points toward the bitmap
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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)
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void open(vpImage< vpRGBa > &I)
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
VISP_EXPORT double measureTimeMs()