JeVois  1.3
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
tutorial3.C
Go to the documentation of this file.
1 #include <jevois/Core/Module.H>
3 #include <opencv2/core/core.hpp>
4 #include <opencv2/imgproc/imgproc.hpp>
5 
6 // Parameters for our module:
7 static jevois::ParameterCategory const ParamCateg("Edge Detection Options");
8 
9 JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg);
10 JEVOIS_DECLARE_PARAMETER(thresh2, double, "Second threshold for hysteresis", 150.0, ParamCateg);
11 JEVOIS_DECLARE_PARAMETER(aperture, int, "Aperture size for the Sobel operator", 3, jevois::Range<int>(3, 53), ParamCateg);
12 JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(l2grad, bool, "Use more accurate L2 gradient norm if true, L1 if false", false, ParamCateg);
13 
14 // Simple module to detect edges using the Canny algorithm from OpenCV
16  public jevois::Parameter<thresh1, thresh2, aperture, l2grad>
17 {
18  public:
19  // Default base class constructor ok
21 
22  // Virtual destructor for safe inheritance
23  virtual ~TutorialEdgeDetection() { }
24 
25  // Processing function
26  virtual void process(jevois::InputFrame && inframe, jevois::OutputFrame && outframe) override
27  {
28  // Wait for next available camera image:
29  jevois::RawImage inimg = inframe.get();
30 
31  // Convert to OpenCV grayscale:
32  cv::Mat grayimg = jevois::rawimage::convertToCvGray(inimg);
33 
34  // Let camera know we are done processing the input image:
35  inframe.done();
36 
37  // Wait for an image from our gadget driver into which we will put our results. Require that it must have same
38  // image size as the input image, and greyscale pixels:
39  jevois::RawImage outimg = outframe.get();
40  outimg.require("output", inimg.width, inimg.height, V4L2_PIX_FMT_GREY);
41 
42  // Compute Canny edges directly into the output image:
43  cv::Mat edges = jevois::rawimage::cvImage(outimg); // Pixel data of "edges" shared with "outimg", no copy
44  cv::Canny(grayimg, edges, thresh1::get(), thresh2::get(), aperture::get(), l2grad::get());
45 
46  // Send the output image with our processing results to the host over USB:
47  outframe.send();
48  }
49 
50  // Callback function for parameter l2grad
51  void onParamChange(l2grad const & param, bool const & newval) override
52  {
53  LINFO("you changed l2grad to be " + std::to_string(newval));
54  }
55 };
56 
57 // Allow the module to be loaded as a shared object (.so) file:
cv::Mat cvImage(RawImage const &src)
Create an OpenCV image from the existing RawImage data, sharing the pixel memory rather than copying ...
Definition: RawImageOps.C:30
Exception-safe wrapper around a raw camera input frame.
Definition: Module.H:56
void require(char const *info, unsigned int w, unsigned int h, unsigned int f) const
Require a particular image size and format, issue a fatal error message and throw if no match...
Definition: RawImage.C:68
JEVOIS_DECLARE_PARAMETER_WITH_CALLBACK(loglevel, LogLevel,"Set the minimum log level to display", LogLevel::info, LogLevel_Values, ParamCateg)
Parameter.
unsigned int height
Image height in pixels.
Definition: RawImage.H:146
Generic variadic template class template definition for Component Parameters.
void onParamChange(l2grad const &param, bool const &newval) override
Definition: tutorial3.C:51
Module(std::string const &instance)
Constructor.
Definition: Module.C:181
A category to which multiple ParameterDef definitions can belong.
Definition: ParameterDef.H:33
JEVOIS_DECLARE_PARAMETER(help, bool,"Print this help message", false, ParamCateg)
Parameter.
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
Definition: RawImage.H:110
cv::Mat convertToCvGray(RawImage const &src)
Convert RawImage to OpenCV doing color conversion from any RawImage source pixel to OpenCV gray byte...
Definition: RawImageOps.C:203
JEVOIS_REGISTER_MODULE(TutorialEdgeDetection)
static ParameterCategory const ParamCateg("General Options")
Parameter category.
A generic range class.
Definition: Range.H:80
std::string to_string(T const &val)
Convert from type to string.
Definition: UtilsImpl.H:51
Virtual base class for a vision processing module.
Definition: Module.H:279
Exception-safe wrapper around a raw image to be sent over USB.
Definition: Module.H:144
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
Definition: Log.H:176
unsigned int width
Image width in pixels.
Definition: RawImage.H:145
virtual void process(jevois::InputFrame &&inframe, jevois::OutputFrame &&outframe) override
Processing function, version that receives a frame from camera and sends a frame out over USB...
Definition: tutorial3.C:26
virtual ~TutorialEdgeDetection()
Definition: tutorial3.C:23