JeVoisBase  1.5
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
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  This module is intended as a pre-processor, delivering edge maps to a host computer, which may then be in charge of
43  further processing them, for example to detect objects of various shapes.
44 
45  You should be able to run this module at 60 frames/s with resolution 320x240 on the JeVois camera.
46 
47  @author Laurent Itti
48 
49  @videomapping GREY 640 480 29.0 YUYV 640 480 29.0 JeVois EdgeDetection
50  @videomapping GREY 320 240 59.0 YUYV 320 240 59.0 JeVois EdgeDetection
51  @videomapping GREY 176 144 119.0 YUYV 176 144 119.0 JeVois EdgeDetection
52  @email itti\@usc.edu
53  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
54  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
55  @mainurl http://jevois.org
56  @supporturl http://jevois.org/doc
57  @otherurl http://iLab.usc.edu
58  @license GPL v3
59  @distribution Unrestricted
60  @restrictions None
61  \ingroup modules */
63  public jevois::Parameter<thresh1, thresh2, aperture, l2grad>
64 {
65  public:
66  //! Default base class constructor ok
68 
69  //! Virtual destructor for safe inheritance
70  virtual ~EdgeDetection() { }
71 
72  //! Processing function
73  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
74  {
75  // Wait for next available camera image:
76  jevois::RawImage inimg = inframe.get();
77 
78  // Convert to OpenCV grayscale:
79  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
80 
81  // Let camera know we are done processing the input image:
82  inframe.done();
83 
84  // Wait for an image from our gadget driver into which we will put our results. Require that it must have same
85  // image size as the input image, and greyscale pixels:
86  jevois::RawImage outimg = outframe.get();
87  outimg.require("output", inimg.width, inimg.height, V4L2_PIX_FMT_GREY);
88 
89  // Compute Canny edges directly into the output image:
90  cv::Mat edges = jevois::rawimage::cvImage(outimg); // Pixel data of "edges" shared with "outimg", no copy
91  cv::Canny(grayimg, edges, thresh1::get(), thresh2::get(), aperture::get(), l2grad::get());
92 
93  // Send the output image with our processing results to the host over USB:
94  outframe.send();
95  }
96 };
97 
98 // 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:73
Simple module to detect edges using the Canny algorithm from OpenCV.
Definition: EdgeDetection.C:62
unsigned int height
cv::Mat convertToCvGray(RawImage const &src)
JEVOIS_DECLARE_PARAMETER(camparams, std::string, "File stem of camera parameters, or empty. Camera resolution " "will be appended, as well as a .cfg 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.", "camera_para", ParamCateg)
Parameter.
virtual ~EdgeDetection()
Virtual destructor for safe inheritance.
Definition: EdgeDetection.C:70
unsigned int width
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const