JeVoisBase  1.20
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
EdgeDetectionX4.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>
21 
22 #include <linux/videodev2.h>
23 #include <opencv2/core/core.hpp>
24 #include <opencv2/imgproc/imgproc.hpp>
25 #include <future>
26 
27 // icon by Sergiu Bagrin in interface at flaticon
28 
29 static jevois::ParameterCategory const ParamCateg("Edge Detection Options");
30 //! Parameter \relates EdgeDetectionX4
31 JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 20.0, ParamCateg);
32 //! Parameter \relates EdgeDetectionX4
33 JEVOIS_DECLARE_PARAMETER(thresh2, double, "Second threshold for hysteresis", 60.0, ParamCateg);
34 //! Parameter \relates EdgeDetectionX4
35 JEVOIS_DECLARE_PARAMETER(aperture, int, "Aperture size for the Sobel operator", 3, ParamCateg);
36 //! Parameter \relates EdgeDetectionX4
37 JEVOIS_DECLARE_PARAMETER(l2grad, bool, "Use more accurate L2 gradient norm if true, L1 if false", false, ParamCateg);
38 //! Parameter \relates EdgeDetectionX4
39 JEVOIS_DECLARE_PARAMETER(thresh1delta, double, "First threshold delta over threads", 50.0, ParamCateg);
40 //! Parameter \relates EdgeDetectionX4
41 JEVOIS_DECLARE_PARAMETER(thresh2delta, double, "Second threshold delta over threads", 50.0, ParamCateg);
42 
43 //! Simple module to detect edges, running 4 filters in parallel with 4 different settings
44 /*! Compute 4 Canny edge detection filters with 4 different settings, in parallel.
45 
46  This module is useful as a pre-processor, feeding edge maps at 4 different levels of details to further processing
47  that may happen on a host computer. The 4 different levels of detail can be leveraged to first detect the gross
48  outlines of objects, and then focus on finer textures within these objects.
49 
50  This algorithm should easily run at 45 frames/s on the JeVois smart camera.
51 
52 
53  @author Laurent Itti
54 
55  @displayname Edge Detection X4
56  @videomapping GREY 320 960 45.0 YUYV 320 240 45.0 JeVois EdgeDetectionX4
57  @email itti\@usc.edu
58  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
59  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
60  @mainurl http://jevois.org
61  @supporturl http://jevois.org/doc
62  @otherurl http://iLab.usc.edu
63  @license GPL v3
64  @distribution Unrestricted
65  @restrictions None
66  \ingroup modules */
68  public jevois::Parameter<thresh1, thresh2, aperture, l2grad, thresh1delta, thresh2delta>
69 {
70  public:
71  //! Default base class constructor ok
73 
74  //! Virtual destructor for safe inheritance
75  virtual ~EdgeDetectionX4() { }
76 
77  //! Processing function
78  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
79  {
80  // Wait for next available camera image:
81  jevois::RawImage inimg = inframe.get();
82 
83  // Convert to grayscale:
84  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
85 
86  // Let camera know we are done processing the input image:
87  inframe.done();
88 
89  // Wait for an image from our gadget driver into which we will put our results:
90  jevois::RawImage outimg = outframe.get();
91  outimg.require("output", inimg.width, inimg.height * 4, V4L2_PIX_FMT_GREY);
92 
93  // Launch 4 Canny filters in parallel. We launch 3 threads and will do the fourth in the current thread:
94  std::vector<std::future<void> > fut;
95 
96  for (int i = 0; i < 3; ++i)
97  fut.push_back(jevois::async([&](int i) {
98  // Compute Canny edges directly into the output image, offset by i images down. The last argument of the
99  // cv::Mat constructor below is the address of an already-allocated pixel buffer for the cv::Mat:
100  cv::Mat edges(grayimg.rows, grayimg.cols, CV_8UC1, outimg.pixelsw<unsigned char>() + i * grayimg.total());
101 
102  cv::Canny(grayimg, edges, thresh1::get() + i * thresh1delta::get(),
103  thresh2::get() + i * thresh2delta::get(), aperture::get(), l2grad::get());
104  }, i));
105 
106  // Fourth one (same code as above except for the async, and for i=3):
107  cv::Mat edges(grayimg.rows, grayimg.cols, CV_8UC1, outimg.pixelsw<unsigned char>() + 3 * grayimg.total());
108  cv::Canny(grayimg, edges, thresh1::get() + 3 * thresh1delta::get(),
109  thresh2::get() + 3 * thresh2delta::get(), aperture::get(), l2grad::get());
110 
111  // The fourth one is done now, wait for all the threads to complete. Note: using async() is preferred to using
112  // std::thread, as get() below will throw if any exception was thrown by a thread, as opposed to std::thread
113  // violently terminating the program on exception. In case two or more threads threw, we can here avoid
114  // termination by catching the exceptions one by one. Here we just ignore (since we are done anyway) but could
115  // throw just once if any of the threads threw:
116  for (auto & f : fut) try { f.get(); } catch (...) { jevois::warnAndIgnoreException(); }
117 
118  // Send the output image with our processing results to the host over USB:
119  outframe.send();
120  }
121 };
122 
123 // Allow the module to be loaded as a shared object (.so) file:
EdgeDetectionX4::process
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: EdgeDetectionX4.C:78
jevois::OutputFrame
jevois::async
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async(Function &&f, Args &&... args)
Module.H
JEVOIS_REGISTER_MODULE
JEVOIS_REGISTER_MODULE(EdgeDetectionX4)
Log.H
jevois::RawImage
jevois::ParameterCategory
jevois::rawimage::convertToCvGray
cv::Mat convertToCvGray(RawImage const &src)
jevois::RawImage::require
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
EdgeDetectionX4::~EdgeDetectionX4
virtual ~EdgeDetectionX4()
Virtual destructor for safe inheritance.
Definition: EdgeDetectionX4.C:75
EdgeDetectionX4
Simple module to detect edges, running 4 filters in parallel with 4 different settings.
Definition: EdgeDetectionX4.C:67
jevois::RawImage::width
unsigned int width
jevois::RawImage::pixelsw
T * pixelsw()
jevois::warnAndIgnoreException
std::string warnAndIgnoreException(std::string const &prefix="")
jevois::Module
RawImageOps.H
jevois::RawImage::height
unsigned int height
jevois::InputFrame
ARtoolkit::JEVOIS_DECLARE_PARAMETER
JEVOIS_DECLARE_PARAMETER(camparams, std::string, "File stem of camera parameters, or empty. Camera resolution " "will be appended, as well as a .dat extension. For example, specifying 'camera_para' " "here and running the camera sensor at 320x240 will attempt to load " "camera_para320x240.dat from within the module's directory (if relative stem) or " "from the specified absolute location (if absolute stem).", JEVOIS_SHARE_PATH "/camera/camera_para", ParamCateg)
Parameter.
jevois::Component::Module
friend friend class Module