JeVoisBase  1.5
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
SuperPixelSeg.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 #include <jevois/Debug/Log.H>
20 #include <jevois/Util/Utils.H>
22 #include <jevois/Debug/Timer.H>
23 #include <jevois/Types/Enum.H>
24 
26 
27 #include <linux/videodev2.h>
28 
29 // icon by Freepik in interface at flaticon
30 
31 //! Segment an image using super-pixels
32 /*! Segment an image into regions with somewhat uniform appearance, using the SuperPixel algorithms of OpenCV.
33 
34  The result is an image with a reduced number of grey levels, where each grey level represents the label or ID of a
35  given region (so, all pixels with a given grey value belong to a given region in the image).
36 
37  How to use this module
38  ----------------------
39 
40  When you run this algorithm and obtain a low number of clusters, those may not be well visible to the human eye. For
41  example, if you end up with 10 clusters, they will be take grayscale value 0 to 9, which all look completely black
42  (on a standard monitor where 0 is black and 255 is white). To get started with this module, you may hence want to
43  change the parameters a bit. For example:
44 
45  \verbatim
46  setpar algo SEEDS
47  setpar output Labels
48  setpar numpix 255
49  \endverbatim
50  and you should now see something.
51 
52  To work with a smaller number of super-pixels, you would usually want to first create some software that runs on the
53  host computer and which will grab the greyscale frames from JeVois, then will assign colors to the regions somehow,
54  and finally will display the superpixels in color. For testing, you may want to just capture and save some frames
55  from JeVois (which may look all black) and then use some paint program to change color 0, color 1, etc to more
56  visible colors than very similar shades of black.
57 
58  @author Laurent Itti
59 
60  @videomapping GREY 320 240 30.0 YUYV 320 240 30.0 JeVois SuperPixelSeg
61  @email itti\@usc.edu
62  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
63  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
64  @mainurl http://jevois.org
65  @supporturl http://jevois.org/doc
66  @otherurl http://iLab.usc.edu
67  @license GPL v3
68  @distribution Unrestricted
69  @restrictions None
70  \ingroup modules */
72 {
73  public:
74  //! Constructor
75  SuperPixelSeg(std::string const & instance) : jevois::Module(instance)
76  { itsSuperPixel = addSubComponent<SuperPixel>("superpixel"); }
77 
78  //! Virtual destructor for safe inheritance
79  virtual ~SuperPixelSeg() { }
80 
81  //! Processing function
82  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
83  {
84  static jevois::Timer timer("processing");
85 
86  // Wait for next available camera image:
87  jevois::RawImage inimg = inframe.get(); unsigned int const w = inimg.width, h = inimg.height;
88  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV); // any image size but require YUYV pixels
89 
90  timer.start();
91 
92  // Convert to openCV RGB:
93  cv::Mat cvimg = jevois::rawimage::convertToCvRGB(inimg);
94 
95  // Let camera know we are done processing the input image:
96  inframe.done();
97 
98  // Get the output image and require size dims as input but grey pixels:
99  jevois::RawImage outimg = outframe.get();
100  outimg.require("output", w, h, V4L2_PIX_FMT_GREY);
101 
102  // Process ths input and get output:
103  cv::Mat grayimg = jevois::rawimage::cvImage(outimg); // grayimg's pixels point to outimg's, no copy
104  itsSuperPixel->process(cvimg, grayimg);
105 
106  // All done:
107  timer.stop();
108 
109  // Send the output image with our processing results to the host over USB:
110  outframe.send();
111  }
112 
113  private:
114  std::shared_ptr<SuperPixel> itsSuperPixel;
115 };
116 
117 // Allow the module to be loaded as a shared object (.so) file:
cv::Mat convertToCvRGB(RawImage const &src)
cv::Mat cvImage(RawImage const &src)
friend friend class Module
Segment an image using super-pixels.
Definition: SuperPixelSeg.C:71
unsigned int height
JEVOIS_REGISTER_MODULE(SuperPixelSeg)
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: SuperPixelSeg.C:82
virtual ~SuperPixelSeg()
Virtual destructor for safe inheritance.
Definition: SuperPixelSeg.C:79
SuperPixelSeg(std::string const &instance)
Constructor.
Definition: SuperPixelSeg.C:75
std::string const & stop()
unsigned int width
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const