JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
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. The
33  result is an image with a reduced number of grey levels, where each grey level represents the label or ID of a given
34  region (so, all pixels with a given grey value belong to a given region in the image).
35 
36  @author Laurent Itti
37 
38  @videomapping GREY 320 240 30.0 YUYV 320 240 30.0 JeVois SuperPixelSeg
39  @email itti\@usc.edu
40  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
41  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
42  @mainurl http://jevois.org
43  @supporturl http://jevois.org/doc
44  @otherurl http://iLab.usc.edu
45  @license GPL v3
46  @distribution Unrestricted
47  @restrictions None
48  \ingroup modules */
50 {
51  public:
52  //! Constructor
53  SuperPixelSeg(std::string const & instance) : jevois::Module(instance)
54  { itsSuperPixel = addSubComponent<SuperPixel>("superpixel"); }
55 
56  //! Virtual destructor for safe inheritance
57  virtual ~SuperPixelSeg() { }
58 
59  //! Processing function
60  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
61  {
62  static jevois::Timer timer("processing");
63 
64  // Wait for next available camera image:
65  jevois::RawImage inimg = inframe.get(); unsigned int const w = inimg.width, h = inimg.height;
66  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV); // any image size but require YUYV pixels
67 
68  timer.start();
69 
70  // Convert to openCV RGB:
71  cv::Mat cvimg = jevois::rawimage::convertToCvRGB(inimg);
72 
73  // Let camera know we are done processing the input image:
74  inframe.done();
75 
76  // Get the output image and require size dims as input but grey pixels:
77  jevois::RawImage outimg = outframe.get();
78  outimg.require("output", w, h, V4L2_PIX_FMT_GREY);
79 
80  // Process ths input and get output:
81  cv::Mat grayimg = jevois::rawimage::cvImage(outimg); // grayimg's pixels point to outimg's, no copy
82  itsSuperPixel->process(cvimg, grayimg);
83 
84  // All done:
85  timer.stop();
86 
87  // Send the output image with our processing results to the host over USB:
88  outframe.send();
89  }
90 
91  private:
92  std::shared_ptr<SuperPixel> itsSuperPixel;
93 };
94 
95 // 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:49
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
unsigned int height
JEVOIS_REGISTER_MODULE(SuperPixelSeg)
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: SuperPixelSeg.C:60
virtual ~SuperPixelSeg()
Virtual destructor for safe inheritance.
Definition: SuperPixelSeg.C:57
SuperPixelSeg(std::string const &instance)
Constructor.
Definition: SuperPixelSeg.C:53
unsigned int width
std::string const & stop()