JeVoisBase  1.20
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 #include <jevois/Debug/Timer.H>
21 
22 #include <linux/videodev2.h>
23 #include <opencv2/core/core.hpp>
24 #include <opencv2/imgproc/imgproc.hpp>
25 
26 // icon by Sergiu Bagrin in interface at flaticon
27 
28 // Parameters for our module:
29 static jevois::ParameterCategory const ParamCateg("Edge Detection Options");
30 
31 //! Parameter \relates EdgeDetection
32 JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg);
33 //! Parameter \relates EdgeDetection
34 JEVOIS_DECLARE_PARAMETER(thresh2, double, "Second threshold for hysteresis", 150.0, ParamCateg);
35 //! Parameter \relates EdgeDetection
36 JEVOIS_DECLARE_PARAMETER(aperture, int, "Aperture size for the Sobel operator", 3, ParamCateg);
37 //! Parameter \relates EdgeDetection
38 JEVOIS_DECLARE_PARAMETER(l2grad, bool, "Use more accurate L2 gradient norm if true, L1 if false", false, ParamCateg);
39 
40 //! Simple module to detect edges using the Canny algorithm from OpenCV
41 /*! Compute edges in an image using the Canny edge detection algorithm.
42 
43  This module is intended as a pre-processor, delivering edge maps to a host computer, which may then be in charge of
44  further processing them, for example to detect objects of various shapes.
45 
46  You should be able to run this module at 60 frames/s with resolution 320x240 on the JeVois camera.
47 
48  @author Laurent Itti
49 
50  @videomapping GREY 640 480 29.0 YUYV 640 480 29.0 JeVois EdgeDetection
51  @videomapping GREY 320 240 59.0 YUYV 320 240 59.0 JeVois EdgeDetection
52  @videomapping GREY 176 144 119.0 YUYV 176 144 119.0 JeVois EdgeDetection
53  @email itti\@usc.edu
54  @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
55  @copyright Copyright (C) 2016 by Laurent Itti, iLab and the University of Southern California
56  @mainurl http://jevois.org
57  @supporturl http://jevois.org/doc
58  @otherurl http://iLab.usc.edu
59  @license GPL v3
60  @distribution Unrestricted
61  @restrictions None
62  \ingroup modules */
64  public jevois::Parameter<thresh1, thresh2, aperture, l2grad>
65 {
66  public:
67  //! Default base class constructor ok
69 
70  //! Virtual destructor for safe inheritance
71  virtual ~EdgeDetection() { }
72 
73  //! Processing function
74  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
75  {
76  // Wait for next available camera image:
77  jevois::RawImage inimg = inframe.get();
78 
79  // Convert to OpenCV grayscale:
80  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
81 
82  // Let camera know we are done processing the input image:
83  inframe.done();
84 
85  // Wait for an image from our gadget driver into which we will put our results. Require that it must have same
86  // image size as the input image, and greyscale pixels:
87  jevois::RawImage outimg = outframe.get();
88  outimg.require("output", inimg.width, inimg.height, V4L2_PIX_FMT_GREY);
89 
90  // Compute Canny edges directly into the output image:
91  cv::Mat edges = jevois::rawimage::cvImage(outimg); // Pixel data of "edges" shared with "outimg", no copy
92  cv::Canny(grayimg, edges, thresh1::get(), thresh2::get(), aperture::get(), l2grad::get());
93 
94  // Send the output image with our processing results to the host over USB:
95  outframe.send();
96  }
97 
98 #ifdef JEVOIS_PRO
99  // ####################################################################################################
100  //! Processing function with zero-copy and GUI on JeVois-Pro
101  // ####################################################################################################
102  virtual void process(jevois::InputFrame && inframe, jevois::GUIhelper & helper) override
103  {
104  static jevois::Timer timer("processing", 100, LOG_DEBUG);
105 
106  // Start the GUI frame:
107  unsigned short winw, winh;
108  helper.startFrame(winw, winh);
109 
110  // Draw the camera frame:
111  int x = 0, y = 0; unsigned short iw = 0, ih = 0;
112  helper.drawInputFrame("camera", inframe, x, y, iw, ih);
113 
114  // Wait for next available camera image:
115  jevois::RawImage const inimg = inframe.getp();
116  helper.itext("JeVois-Pro Edge Detection");
117 
118  timer.start();
119 
120  // Convert the image to grayscale and process:
121  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg), edges;
122  cv::Canny(grayimg, edges, thresh1::get(), thresh2::get(), aperture::get(), l2grad::get());
123 
124  // Convert to RGBA, with zero alpha (transparent) in background and full alpha on edges:
125  cv::Mat chans[4] { edges, edges, edges, edges };
126  cv::Mat mask; cv::merge(chans, 4, mask);
127 
128  // Draw overlay:
129  helper.drawImage("edges", mask, true, x, y, iw, ih, false /* noalias */, true /* isoverlay */);
130 
131  // Show processing fps:
132  std::string const & fpscpu = timer.stop();
133  helper.iinfo(inframe, fpscpu, winw, winh);
134 
135  // Render the image and GUI:
136  helper.endFrame();
137  }
138 #endif
139 };
140 
141 // Allow the module to be loaded as a shared object (.so) file:
jevois::GUIhelper
jevois::GUIhelper::drawImage
void drawImage(char const *name, RawImage const &img, int &x, int &y, unsigned short &w, unsigned short &h, bool noalias=false, bool isoverlay=false)
jevois::OutputFrame
jevois::GUIhelper::startFrame
bool startFrame(unsigned short &w, unsigned short &h)
JEVOIS_REGISTER_MODULE
JEVOIS_REGISTER_MODULE(EdgeDetection)
Timer.H
jevois::GUIhelper::itext
void itext(char const *txt, ImU32 const &col=IM_COL32_BLACK_TRANS, int line=-1)
Module.H
jevois::GUIhelper::endFrame
void endFrame()
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
jevois::RawImage::width
unsigned int width
EdgeDetection
Simple module to detect edges using the Canny algorithm from OpenCV.
Definition: EdgeDetection.C:63
jevois::Module
RawImageOps.H
jevois::GUIhelper::drawInputFrame
void drawInputFrame(char const *name, InputFrame const &frame, int &x, int &y, unsigned short &w, unsigned short &h, bool noalias=false, bool casync=false)
jevois::RawImage::height
unsigned int height
jevois::GUIhelper::iinfo
void iinfo(jevois::InputFrame const &inframe, std::string const &fpscpu, unsigned short winw=0, unsigned short winh=0)
jevois::InputFrame
jevois::rawimage::cvImage
cv::Mat cvImage(RawImage const &src)
EdgeDetection::~EdgeDetection
virtual ~EdgeDetection()
Virtual destructor for safe inheritance.
Definition: EdgeDetection.C:71
EdgeDetection::process
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function.
Definition: EdgeDetection.C:74
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::Timer
jevois::Component::Module
friend friend class Module