JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
DemoBackgroundSubtract.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <jevois/Core/Module.H>
19 
20 #include <jevois/Debug/Log.H>
21 #include <jevois/Debug/Timer.H>
23 
24 #include <opencv2/core/core.hpp>
25 #include <opencv2/imgproc/imgproc.hpp>
26 #include <opencv2/video/background_segm.hpp>
27 
28 #include <future>
29 #include <linux/videodev2.h> // for v4l2 pixel types
30 
31 // icon by by Freepik in arrows at flaticon
32 
33 //! Simple background subtraction, pretty much straight from the OpenCV tutorials
34 /*! The background subtraction alorithm learns a statistical model of the appearance of a scene when the camera is not
35  moving. Any movin object entering the field of view will then be detected as significantly different from the
36  learned pixel model.
37 
38  Note that this class has internal state (it learns the statistics of the background over time). FIXME: Unclear how
39  it would react to input resolution changes, need to test.
40 
41  @author Laurent Itti
42 
43  @videomapping YUYV 640 240 15.0 YUYV 320 240 15.0 JeVois DemoBackgroundSubtract
44  @videomapping YUYV 320 120 30.0 YUYV 160 120 30.0 JeVois DemoBackgroundSubtract
45  @email itti\@usc.edu
46  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
47  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
48  @mainurl http://jevois.org
49  @supporturl http://jevois.org/doc
50  @otherurl http://iLab.usc.edu
51  @license GPL v3
52  @distribution Unrestricted
53  @restrictions None
54  \ingroup modules */
56 {
57  public:
58  //! Constructor
59  DemoBackgroundSubtract(std::string const & instance) :
60  jevois::Module(instance), itsProcessingTimer("Processing"), pMOG2(cv::createBackgroundSubtractorMOG2())
61  { }
62 
63  //! Virtual destructor for safe inheritance
65 
66  //! Processing function
67  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
68  {
69  // Wait for next available camera image:
70  jevois::RawImage inimg = inframe.get(); unsigned int const w = inimg.width, h = inimg.height;
71  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV); // accept any image size but require YUYV pixels
72 
74 
75  // Convert it to BGR:
76  cv::Mat imgbgr = jevois::rawimage::convertToCvBGR(inimg);
77 
78  // Compute the foreground mask in a thread:
79  cv::Mat fgmask;
80  auto fg_fut = std::async(std::launch::async, [&]() { pMOG2->apply(imgbgr, fgmask); });
81 
82  // While computing, wait for an image from our gadget driver into which we will put our results:
83  jevois::RawImage outimg = outframe.get();
84 
85  // Enforce the correct output image size and format:
86  outimg.require("output", w * 2, h, V4L2_PIX_FMT_YUYV);
87 
88  // Paste the original image to the top-left corner of the display:
89  jevois::rawimage::paste(inimg, outimg, 0, 0);
90  jevois::rawimage::writeText(outimg, "JeVois Background Subtraction Demo", 3, 3, jevois::yuyv::White);
91 
92  // Let camera know we are done processing the raw input image:
93  inframe.done();
94 
95  // Wait for the processing results:
96  fg_fut.get();
97 
98  // Paste the results into the output image:
99  jevois::rawimage::pasteGreyToYUYV(fgmask, outimg, w, 0);
100  jevois::rawimage::writeText(outimg, "Foreground Mask", w+3, 3, jevois::yuyv::White);
101 
102  // Show processing fps:
103  std::string const & fpscpu = itsProcessingTimer.stop();
104  jevois::rawimage::writeText(outimg, fpscpu, 3, h - 13, jevois::yuyv::White);
105 
106  // Send the output image with our processing results to the host over USB:
107  outframe.send();
108  }
109 
110  protected:
112  cv::Ptr<cv::BackgroundSubtractor> pMOG2;
113 };
114 
115 // Allow the module to be loaded as a shared object (.so) file:
DemoBackgroundSubtract(std::string const &instance)
Constructor.
friend friend class Module
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
cv::Mat convertToCvBGR(RawImage const &src)
unsigned int height
JEVOIS_REGISTER_MODULE(DemoBackgroundSubtract)
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
cv::Ptr< cv::BackgroundSubtractor > pMOG2
virtual ~DemoBackgroundSubtract()
Virtual destructor for safe inheritance.
void pasteGreyToYUYV(cv::Mat const &src, RawImage &dest, int dx, int dy)
Simple background subtraction, pretty much straight from the OpenCV tutorials.
unsigned int width
std::string const & stop()
void paste(RawImage const &src, RawImage &dest, int dx, int dy)