49 #include <visp3/core/vpConfig.h> 51 #if defined(VISP_HAVE_CPP11_COMPATIBILITY) && defined(VISP_HAVE_V4L2) && \ 52 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK)) 54 #include <condition_variable> 61 #include <visp3/core/vpDisplay.h> 62 #include <visp3/core/vpImageFilter.h> 63 #include <visp3/core/vpIoTools.h> 64 #include <visp3/core/vpTime.h> 65 #include <visp3/gui/vpDisplayGTK.h> 66 #include <visp3/gui/vpDisplayX.h> 67 #include <visp3/io/vpParseArgv.h> 68 #include <visp3/io/vpVideoWriter.h> 69 #include <visp3/sensor/vpV4l2Grabber.h> 71 #define GETOPTARGS "d:oh" 76 void usage(
const char *name,
const char *badparam)
80 %s [-d <device count>] [-o] [-h]\n\ 83 Capture multiple camera streams and save the stream without slowing down the acquisition.\n\ 87 Open the specified number of camera streams.\n\ 90 Save each stream in a dedicated folder.\n\ 93 Print the help.\n\n", name);
96 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
99 bool getOptions(
int argc,
char **argv,
unsigned int &deviceCount,
bool &saveVideo)
102 const char **argv1 = (
const char **)argv;
108 deviceCount = (
unsigned int)atoi(optarg);
114 usage(argv[0], NULL);
119 usage(argv[0], optarg);
125 if ((c == 1) || (c == -1)) {
127 usage(argv[0], NULL);
128 std::cerr <<
"ERROR: " << std::endl;
129 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
146 : m_cancelled(false), m_cond(), m_queueColor(), m_maxQueueSize(
std::numeric_limits<size_t>::max()), m_mutex()
152 std::lock_guard<std::mutex> lock(m_mutex);
160 std::lock_guard<std::mutex> lock(m_mutex);
162 m_queueColor.push(image);
165 while (m_queueColor.size() > m_maxQueueSize) {
175 std::unique_lock<std::mutex> lock(m_mutex);
177 while (m_queueColor.empty()) {
195 void setMaxQueueSize(
const size_t max_queue_size) { m_maxQueueSize = max_queue_size; }
199 std::condition_variable m_cond;
200 std::queue<vpImage<vpRGBa> > m_queueColor;
201 size_t m_maxQueueSize;
209 StorageWorker(FrameQueue &queue,
const std::string &filename,
const unsigned int width,
const unsigned int height)
210 : m_queue(queue), m_filename(filename), m_width(width), m_height(height)
220 if (!m_filename.empty()) {
222 writer.
open(O_color);
229 if (!m_filename.empty()) {
233 }
catch (FrameQueue::cancelled &) {
239 std::string m_filename;
240 unsigned int m_width;
241 unsigned int m_height;
249 std::condition_variable m_cond;
251 unsigned char *m_pImgData;
252 unsigned int m_totalSize;
258 ShareImage() : m_cancelled(false), m_cond(), m_mutex(), m_pImgData(NULL), m_totalSize(0) {}
260 virtual ~ShareImage()
262 if (m_pImgData != NULL) {
269 std::lock_guard<std::mutex> lock(m_mutex);
275 void getImage(
unsigned char *
const imageData,
const unsigned int totalSize)
277 std::unique_lock<std::mutex> lock(m_mutex);
290 if (totalSize <= m_totalSize) {
291 memcpy(imageData, m_pImgData, totalSize *
sizeof(
unsigned char));
293 std::cerr <<
"totalSize <= m_totalSize !" << std::endl;
299 std::lock_guard<std::mutex> lock(m_mutex);
304 void setImage(
const unsigned char *
const imageData,
const unsigned int totalSize)
306 std::lock_guard<std::mutex> lock(m_mutex);
308 if (m_pImgData == NULL || m_totalSize != totalSize) {
309 m_totalSize = totalSize;
311 if (m_pImgData != NULL) {
315 m_pImgData =
new unsigned char[m_totalSize];
319 memcpy(m_pImgData, imageData, m_totalSize *
sizeof(
unsigned char));
325 void capture(
vpV4l2Grabber *
const pGrabber, ShareImage &share_image)
330 pGrabber->
open(local_img);
333 if (share_image.isCancelled()) {
340 share_image.setImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
344 void display(
const unsigned int width,
const unsigned int height,
const int win_x,
const int win_y,
345 const unsigned int deviceId, ShareImage &share_image, FrameQueue &queue,
const bool save)
349 #if defined VISP_HAVE_X11 351 #elif defined VISP_HAVE_GTK 356 std::stringstream ss;
357 ss <<
"Camera stream " << deviceId;
358 display.
init(local_img, win_x, win_y, ss.str());
363 vpImage<unsigned char> I_red(height, width), I_green(height, width), I_blue(height, width), I_alpha(height, width);
366 I_blue_gaussian(height, width);
367 vpImage<double> I_red_gaussian_double, I_green_gaussian_double, I_blue_gaussian_double;
369 bool exit =
false, gaussian_blur =
false;
374 share_image.getImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
396 std::stringstream ss;
397 ss <<
"Time: " << t <<
" ms";
407 queue.push(local_img);
413 gaussian_blur = !gaussian_blur;
422 }
catch (ShareImage::cancelled &) {
423 std::cout <<
"Cancelled!" << std::endl;
426 share_image.cancel();
431 int main(
int argc,
char *argv[])
433 unsigned int deviceCount = 1;
434 unsigned int cameraScale = 1;
435 bool saveVideo =
false;
438 if (!getOptions(argc, argv, deviceCount, saveVideo)) {
442 std::vector<vpV4l2Grabber *> grabbers;
444 const unsigned int offsetX = 100, offsetY = 100;
445 for (
unsigned int devicedId = 0; devicedId < deviceCount; devicedId++) {
448 std::stringstream ss;
449 ss <<
"/dev/video" << devicedId;
453 grabbers.push_back(pGrabber);
455 std::cerr <<
"Exception: " << e.
what() << std::endl;
459 std::cout <<
"Grabbers: " << grabbers.size() << std::endl;
461 std::vector<ShareImage> share_images(grabbers.size());
462 std::vector<std::thread> capture_threads;
463 std::vector<std::thread> display_threads;
466 std::vector<FrameQueue> save_queues(grabbers.size());
467 std::vector<StorageWorker> storages;
468 std::vector<std::thread> storage_threads;
471 for (
size_t deviceId = 0; deviceId < grabbers.size(); deviceId++) {
473 capture_threads.emplace_back(capture, grabbers[deviceId], std::ref(share_images[deviceId]));
474 int win_x = deviceId * offsetX, win_y = deviceId * offsetY;
477 display_threads.emplace_back(display, grabbers[deviceId]->getWidth(), grabbers[deviceId]->getHeight(), win_x, win_y,
478 deviceId, std::ref(share_images[deviceId]), std::ref(save_queues[deviceId]),
482 std::stringstream ss;
483 ss << parent_directory <<
"/Camera_Stream" << deviceId;
484 std::cout <<
"Create directory: " << ss.str() << std::endl;
487 std::string filename = ss.str();
489 storages.emplace_back(std::ref(save_queues[deviceId]), std::cref(filename), grabbers[deviceId]->getWidth(),
490 grabbers[deviceId]->getHeight());
495 for (
auto &s : storages) {
497 storage_threads.emplace_back(&StorageWorker::run, &s);
502 for (
auto &ct : capture_threads) {
506 for (
auto &dt : display_threads) {
512 for (
auto &g : grabbers) {
517 std::cout <<
"\nWaiting for finishing thread to write images..." << std::endl;
521 for (
auto &qu : save_queues) {
526 for (
auto &st : storage_threads) {
533 #if !(defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK)) 536 std::cout <<
"You do not have X11, or GTK functionalities to display images..." << std::endl;
537 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
538 std::cout <<
"- Install X11, configure again ViSP using cmake and build again this example" << std::endl;
539 std::cout <<
"Tip if you are on a windows-like system:" << std::endl;
540 std::cout <<
"- Install GTK, configure again ViSP using cmake and build again this example" << std::endl;
543 #elif !defined(VISP_HAVE_V4L2) 546 std::cout <<
"You do not have Video 4 Linux 2 functionality enabled" << std::endl;
547 std::cout <<
"Tip if you are on a unix-like system:" << std::endl;
548 std::cout <<
"- Install libv4l2, configure again ViSP using cmake and build again this example" << std::endl;
554 std::cout <<
"You do not build ViSP with C++11 compiler flag" << std::endl;
555 std::cout <<
"Tip:" << std::endl;
556 std::cout <<
"- Configure ViSP again using cmake -DUSE_CPP11=ON, and build again this example" << std::endl;
void acquire(vpImage< unsigned char > &I)
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
void open(vpImage< unsigned char > &I)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Type * bitmap
points toward the bitmap
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
void setDevice(const std::string &devname)
error that can be emited by ViSP classes.
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=NULL)
static void flush(const vpImage< unsigned char > &I)
VISP_EXPORT double measureTimeMs()
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
const char * what() const
unsigned int getSize() const
static void display(const vpImage< unsigned char > &I)
static void gaussianBlur(const vpImage< unsigned char > &I, vpImage< double > &GI, unsigned int size=7, double sigma=0., bool normalize=true)
Class that enables to write easily a video file or a sequence of images.
The vpDisplayGTK allows to display image using the GTK 3rd party library. Thus to enable this class G...
void saveFrame(vpImage< vpRGBa > &I)
void setScale(unsigned scale=vpV4l2Grabber::DEFAULT_SCALE)
void open(vpImage< vpRGBa > &I)
void setFileName(const char *filename)
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)
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Class that is a wrapper over the Video4Linux2 (V4L2) driver.