JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
RoadNavigation.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>
24 
25 #include <opencv2/core/core.hpp>
26 #include <opencv2/imgproc/imgproc.hpp>
27 
28 #include <future>
29 #include <linux/videodev2.h> // for v4l2 pixel types
30 
31 // icon by Dave Gandy in transport at flaticon
32 
33 //! Road finder demo
34 /*! This algorithm detects road using a compination of edge detection and tracking, and texture analysis. The algorithm
35  is an implementation of Chang, Siagian and Itti, IROS 2012, available at
36  http://ilab.usc.edu/publications/doc/Chang_etal12iros.pdf
37 
38  @author Laurent Itti
39 
40  @videomapping NONE 0 0 0 YUYV 320 240 30.0 JeVois RoadNavigation
41  @videomapping NONE 0 0 0 YUYV 176 144 120.0 JeVois RoadNavigation
42  @videomapping YUYV 320 256 30.0 YUYV 320 240 30.0 JeVois RoadNavigation
43  @videomapping YUYV 176 160 120.0 YUYV 176 144 120.0 JeVois RoadNavigation
44  @email itti\@usc.edu
45  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
46  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
47  @mainurl http://jevois.org
48  @supporturl http://jevois.org/doc
49  @otherurl http://iLab.usc.edu
50  @license GPL v3
51  @distribution Unrestricted
52  @restrictions None
53  \ingroup modules */
55 {
56  public:
57  // ####################################################################################################
58  //! Constructor
59  // ####################################################################################################
60  RoadNavigation(std::string const & instance) :
61  jevois::Module(instance), itsProcessingTimer("Processing", 30, LOG_DEBUG)
62  {
63  itsRoadFinder = addSubComponent<RoadFinder>("roadfinder");
64  }
65 
66  // ####################################################################################################
67  //! Virtual destructor for safe inheritance
68  // ####################################################################################################
69  virtual ~RoadNavigation() { }
70 
71  // ####################################################################################################
72  //! Processing function, no video out
73  // ####################################################################################################
74  virtual void process(jevois::InputFrame && inframe) override
75  {
76  // Wait for next available camera image:
77  jevois::RawImage inimg = inframe.get();
78 
79  // Convert it to gray:
80  cv::Mat imggray = jevois::rawimage::convertToCvGray(inimg);
81 
82  // Compute the vanishing point, with no drawings:
83  jevois::RawImage visual; // unallocated pixels, will not draw anything
84  itsRoadFinder->process(imggray, visual);
85 
86  // Let camera know we are done processing the input image:
87  inframe.done();
88 
89  // Get the filtered target point x and send to serial:
90  int const tpx = int(itsRoadFinder->getFilteredTargetX() * 2000.0F / inimg.width - 1000.0F);
91  sendSerial("T1D " + std::to_string(tpx));
92  }
93 
94  // ####################################################################################################
95  //! Processing function with USB video out
96  // ####################################################################################################
97  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
98  {
99  // Wait for next available camera image:
100  jevois::RawImage inimg = inframe.get(); unsigned int const w = inimg.width, h = inimg.height;
101  inimg.require("input", w, h, V4L2_PIX_FMT_YUYV); // accept any image size but require YUYV pixels
102 
104 
105  // Convert it to gray:
106  cv::Mat imggray = jevois::rawimage::convertToCvGray(inimg);
107 
108  // Compute the vanishing point. Note: the results will be drawn into inimg, so that we don't have to wait for
109  // outimg to be ready. It's ok to modify the input image, its buffer will be sent back to the camera driver for
110  // capture once we are done here, and will be overwritten anyway:
111  itsRoadFinder->process(imggray, inimg);
112 
113  // Wait for an image from our gadget driver into which we will put our results:
114  jevois::RawImage outimg = outframe.get();
115 
116  // Enforce the correct output image size and format:
117  outimg.require("output", w, h + 16, V4L2_PIX_FMT_YUYV);
118 
119  // Paste the original image + drawings to the top-left corner of the display:
120  unsigned short const txtcol = 0xa0ff; //WHITE: 0x80ff;
121  jevois::rawimage::paste(inimg, outimg, 0, 0);
122  jevois::rawimage::writeText(outimg, "JeVois Road Navigation Demo", 3, 3, txtcol);
123 
124  // Let camera know we are done processing the raw YUV input image:
125  inframe.done();
126 
127  // Clear out the bottom section of the image:
128  jevois::rawimage::drawFilledRect(outimg, 0, h, w, outimg.height-h, jevois::yuyv::Black);
129 
130  // Get the filtered target point x and send to serial:
131  int const tpx = int(itsRoadFinder->getFilteredTargetX() * 2000.0F / w - 1000.0F);
132  sendSerial("T1D " + std::to_string(tpx));
133 
134  // Write some extra info about the vp:
135  std::ostringstream otxt; otxt << std::fixed << std::setprecision(3);
136  auto vp = itsRoadFinder->getCurrVanishingPoint();
137  otxt << "VP x=" << vp.first.i << " (" << vp.second << ") CTR=" << std::setprecision(1);
138  auto cp = itsRoadFinder->getCurrCenterPoint();
139  auto tp = itsRoadFinder->getCurrTargetPoint();
140  otxt << cp.i << " TGT=" << tp.i << " fTPX=" << tpx;
141  jevois::rawimage::writeText(outimg, otxt.str().c_str(), 3, h + 3, jevois::yuyv::White);
142 
143  // Show processing fps:
144  std::string const & fpscpu = itsProcessingTimer.stop();
145  jevois::rawimage::writeText(outimg, fpscpu, 3, h - 13, jevois::yuyv::White);
146 
147  // Send the output image with our processing results to the host over USB:
148  outframe.send();
149  }
150 
151  // ####################################################################################################
152  //! Module internals
153  // ####################################################################################################
154  protected:
156  std::shared_ptr<RoadFinder> itsRoadFinder;
157 };
158 
159 // Allow the module to be loaded as a shared object (.so) file:
RoadNavigation(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
unsigned int height
virtual void process(jevois::InputFrame &&inframe) override
Processing function, no video out.
std::shared_ptr< RoadFinder > itsRoadFinder
jevois::Timer itsProcessingTimer
Module internals.
JEVOIS_REGISTER_MODULE(RoadNavigation)
cv::Mat convertToCvGray(RawImage const &src)
virtual void sendSerial(std::string const &str)
void drawFilledRect(RawImage &img, int x, int y, unsigned int w, unsigned int h, unsigned int col)
std::string to_string(T const &val)
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function with USB video out.
virtual ~RoadNavigation()
Virtual destructor for safe inheritance.
unsigned int width
std::string const & stop()
void paste(RawImage const &src, RawImage &dest, int dx, int dy)
Road finder demo.