JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
EdgeDetection.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>
20 
21 #include <linux/videodev2.h>
22 #include <opencv2/core/core.hpp>
23 #include <opencv2/imgproc/imgproc.hpp>
24 
25 // icon by Sergiu Bagrin in interface at flaticon
26 
27 // Parameters for our module:
28 static jevois::ParameterCategory const ParamCateg("Edge Detection Options");
29 
30 //! Parameter \relates EdgeDetection
31 JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg);
32 //! Parameter \relates EdgeDetection
33 JEVOIS_DECLARE_PARAMETER(thresh2, double, "Second threshold for hysteresis", 150.0, ParamCateg);
34 //! Parameter \relates EdgeDetection
35 JEVOIS_DECLARE_PARAMETER(aperture, int, "Aperture size for the Sobel operator", 3, ParamCateg);
36 //! Parameter \relates EdgeDetection
37 JEVOIS_DECLARE_PARAMETER(l2grad, bool, "Use more accurate L2 gradient norm if true, L1 if false", false, ParamCateg);
38 
39 //! Simple module to detect edges using the Canny algorithm from OpenCV
40 /*! Compute edges in an image using the Canny edge detection algorithm.
41 
42  @author Laurent Itti
43 
44  @videomapping GREY 640 480 29.0 YUYV 640 480 29.0 JeVois EdgeDetection
45  @videomapping GREY 320 240 59.0 YUYV 320 240 59.0 JeVois EdgeDetection
46  @videomapping GREY 176 144 119.0 YUYV 176 144 119.0 JeVois EdgeDetection
47  @email itti\@usc.edu
48  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
49  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
50  @mainurl http://jevois.org
51  @supporturl http://jevois.org/doc
52  @otherurl http://iLab.usc.edu
53  @license GPL v3
54  @distribution Unrestricted
55  @restrictions None
56  \ingroup modules */
58  public jevois::Parameter<thresh1, thresh2, aperture, l2grad>
59 {
60  public:
61  //! Default base class constructor ok
63 
64  //! Virtual destructor for safe inheritance
65  virtual ~EdgeDetection() { }
66 
67  //! Processing function
68  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
69  {
70  // Wait for next available camera image:
71  jevois::RawImage inimg = inframe.get();
72 
73  // Convert to OpenCV grayscale:
74  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
75 
76  // Let camera know we are done processing the input image:
77  inframe.done();
78 
79  // Wait for an image from our gadget driver into which we will put our results. Require that it must have same
80  // image size as the input image, and greyscale pixels:
81  jevois::RawImage outimg = outframe.get();
82  outimg.require("output", inimg.width, inimg.height, V4L2_PIX_FMT_GREY);
83 
84  // Compute Canny edges directly into the output image:
85  cv::Mat edges = jevois::rawimage::cvImage(outimg); // Pixel data of "edges" shared with "outimg", no copy
86  cv::Canny(grayimg, edges, thresh1::get(), thresh2::get(), aperture::get(), l2grad::get());
87 
88  // Send the output image with our processing results to the host over USB:
89  outframe.send();
90  }
91 };
92 
93 // Allow the module to be loaded as a shared object (.so) file:
JEVOIS_REGISTER_MODULE(EdgeDetection)
cv::Mat cvImage(RawImage const &src)
friend friend class Module
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: EdgeDetection.C:68
Simple module to detect edges using the Canny algorithm from OpenCV.
Definition: EdgeDetection.C:57
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
unsigned int height
#define JEVOIS_DECLARE_PARAMETER(ParamName, ParamType,...)
Parameter.
cv::Mat convertToCvGray(RawImage const &src)
virtual ~EdgeDetection()
Virtual destructor for safe inheritance.
Definition: EdgeDetection.C:65
unsigned int width