1 #include <visp3/core/vpConfig.h> 10 #if defined(VISP_HAVE_CPP11_COMPATIBILITY) && defined(VISP_HAVE_V4L2) && \ 11 (defined(VISP_HAVE_X11) || defined(VISP_HAVE_GTK)) 13 #include <condition_variable> 20 #include <visp3/core/vpDisplay.h> 21 #include <visp3/core/vpImageFilter.h> 22 #include <visp3/core/vpIoTools.h> 23 #include <visp3/core/vpTime.h> 24 #include <visp3/gui/vpDisplayGTK.h> 25 #include <visp3/gui/vpDisplayX.h> 26 #include <visp3/io/vpParseArgv.h> 27 #include <visp3/io/vpVideoWriter.h> 28 #include <visp3/sensor/vpV4l2Grabber.h> 30 #define GETOPTARGS "d:oh" 35 void usage(
const char *name,
const char *badparam)
39 %s [-d <device count>] [-o] [-h]\n\ 42 Capture multiple camera streams and save the stream without slowing down the acquisition.\n\ 46 Open the specified number of camera streams.\n\ 49 Save each stream in a dedicated folder.\n\ 52 Print the help.\n\n", name);
55 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
58 bool getOptions(
int argc,
char **argv,
unsigned int &deviceCount,
bool &saveVideo)
61 const char **argv1 = (
const char **)argv;
67 deviceCount = (
unsigned int)atoi(optarg);
78 usage(argv[0], optarg);
84 if ((c == 1) || (c == -1)) {
87 std::cerr <<
"ERROR: " << std::endl;
88 std::cerr <<
" Bad argument " << optarg << std::endl << std::endl;
105 : m_cancelled(false), m_cond(), m_queueColor(), m_maxQueueSize(
std::numeric_limits<size_t>::max()), m_mutex()
111 std::lock_guard<std::mutex> lock(m_mutex);
119 std::lock_guard<std::mutex> lock(m_mutex);
121 m_queueColor.push(image);
124 while (m_queueColor.size() > m_maxQueueSize) {
134 std::unique_lock<std::mutex> lock(m_mutex);
136 while (m_queueColor.empty()) {
154 void setMaxQueueSize(
const size_t max_queue_size) { m_maxQueueSize = max_queue_size; }
158 std::condition_variable m_cond;
159 std::queue<vpImage<vpRGBa> > m_queueColor;
160 size_t m_maxQueueSize;
168 StorageWorker(FrameQueue &queue,
const std::string &filename,
const unsigned int width,
const unsigned int height)
169 : m_queue(queue), m_filename(filename), m_width(width), m_height(height)
179 if (!m_filename.empty()) {
181 writer.
open(O_color);
188 if (!m_filename.empty()) {
192 }
catch (FrameQueue::cancelled &) {
198 std::string m_filename;
199 unsigned int m_width;
200 unsigned int m_height;
208 std::condition_variable m_cond;
210 unsigned char *m_pImgData;
211 unsigned int m_totalSize;
217 ShareImage() : m_cancelled(false), m_cond(), m_mutex(), m_pImgData(NULL), m_totalSize(0) {}
219 virtual ~ShareImage()
221 if (m_pImgData != NULL) {
228 std::lock_guard<std::mutex> lock(m_mutex);
234 void getImage(
unsigned char *
const imageData,
const unsigned int totalSize)
236 std::unique_lock<std::mutex> lock(m_mutex);
249 if (totalSize <= m_totalSize) {
250 memcpy(imageData, m_pImgData, totalSize *
sizeof(
unsigned char));
252 std::cerr <<
"totalSize <= m_totalSize !" << std::endl;
258 std::lock_guard<std::mutex> lock(m_mutex);
263 void setImage(
const unsigned char *
const imageData,
const unsigned int totalSize)
265 std::lock_guard<std::mutex> lock(m_mutex);
267 if (m_pImgData == NULL || m_totalSize != totalSize) {
268 m_totalSize = totalSize;
270 if (m_pImgData != NULL) {
274 m_pImgData =
new unsigned char[m_totalSize];
278 memcpy(m_pImgData, imageData, m_totalSize *
sizeof(
unsigned char));
284 void capture(
vpV4l2Grabber *
const pGrabber, ShareImage &share_image)
289 pGrabber->
open(local_img);
292 if (share_image.isCancelled()) {
299 share_image.setImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
303 void display(
const unsigned int width,
const unsigned int height,
const int win_x,
const int win_y,
304 const unsigned int deviceId, ShareImage &share_image, FrameQueue &queue,
const bool save)
308 #if defined VISP_HAVE_X11 310 #elif defined VISP_HAVE_GTK 315 std::stringstream ss;
316 ss <<
"Camera stream " << deviceId;
317 display.
init(local_img, win_x, win_y, ss.str());
322 vpImage<unsigned char> I_red(height, width), I_green(height, width), I_blue(height, width), I_alpha(height, width);
325 I_blue_gaussian(height, width);
326 vpImage<double> I_red_gaussian_double, I_green_gaussian_double, I_blue_gaussian_double;
328 bool exit =
false, gaussian_blur =
false;
333 share_image.getImage((
unsigned char *)local_img.
bitmap, local_img.
getSize() * 4);
355 std::stringstream ss;
356 ss <<
"Time: " << t <<
" ms";
366 queue.push(local_img);
372 gaussian_blur = !gaussian_blur;
381 }
catch (ShareImage::cancelled &) {
382 std::cout <<
"Cancelled!" << std::endl;
385 share_image.cancel();
390 int main(
int argc,
char *argv[])
392 unsigned int deviceCount = 1;
393 unsigned int cameraScale = 1;
394 bool saveVideo =
false;
397 if (!getOptions(argc, argv, deviceCount, saveVideo)) {
401 std::vector<vpV4l2Grabber *> grabbers;
403 const unsigned int offsetX = 100, offsetY = 100;
404 for (
unsigned int devicedId = 0; devicedId < deviceCount; devicedId++) {
407 std::stringstream ss;
408 ss <<
"/dev/video" << devicedId;
412 grabbers.push_back(pGrabber);
414 std::cerr <<
"Exception: " << e.
what() << std::endl;
418 std::cout <<
"Grabbers: " << grabbers.size() << std::endl;
420 std::vector<ShareImage> share_images(grabbers.size());
421 std::vector<std::thread> capture_threads;
422 std::vector<std::thread> display_threads;
425 std::vector<FrameQueue> save_queues(grabbers.size());
426 std::vector<StorageWorker> storages;
427 std::vector<std::thread> storage_threads;
430 for (
size_t deviceId = 0; deviceId < grabbers.size(); deviceId++) {
432 capture_threads.emplace_back(capture, grabbers[deviceId], std::ref(share_images[deviceId]));
433 int win_x = deviceId * offsetX, win_y = deviceId * offsetY;
436 display_threads.emplace_back(display, grabbers[deviceId]->getWidth(), grabbers[deviceId]->getHeight(), win_x, win_y,
437 deviceId, std::ref(share_images[deviceId]), std::ref(save_queues[deviceId]),
441 std::stringstream ss;
442 ss << parent_directory <<
"/Camera_Stream" << deviceId;
443 std::cout <<
"Create directory: " << ss.str() << std::endl;
446 std::string filename = ss.str();
448 storages.emplace_back(std::ref(save_queues[deviceId]), std::cref(filename), grabbers[deviceId]->getWidth(),
449 grabbers[deviceId]->getHeight());
454 for (
auto &s : storages) {
456 storage_threads.emplace_back(&StorageWorker::run, &s);
461 for (
auto &ct : capture_threads) {
465 for (
auto &dt : display_threads) {
471 for (
auto &g : grabbers) {
476 std::cout <<
"\nWaiting for finishing thread to write images..." << std::endl;
480 for (
auto &qu : save_queues) {
485 for (
auto &st : storage_threads) {
496 std::cout <<
"Warning: This example need to be build with cxx11 compiler " 497 "flags, v4l2 and x11 or gtk 3rd partiess. " 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)
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)
const char * what() const
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.
unsigned int getSize() const