MRPT  1.9.9
CCascadeClassifierDetection.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "detectors-precomp.h" // Precompiled headers
11 
16 
17 // Universal include for all versions of OpenCV
18 #include <mrpt/otherlibs/do_opencv_includes.h>
19 
20 #include <thread>
21 
22 using namespace mrpt::detectors;
23 using namespace mrpt::obs;
24 using namespace mrpt::img;
25 using namespace std;
26 
27 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
28 using namespace cv;
29 #endif
30 
31 #define CASCADE (reinterpret_cast<CascadeClassifier*>(m_cascade))
32 #define CASCADE_CONST (reinterpret_cast<const CascadeClassifier*>(m_cascade))
33 
34 // ------------------------------------------------------
35 // CCascadeClassifierDetection
36 // ------------------------------------------------------
37 
39 {
40 // Check if MRPT is using OpenCV
41 #if !MRPT_HAS_OPENCV || MRPT_OPENCV_VERSION_NUM < 0x200
43  "CCascadeClassifierDetection class requires MRPT built against OpenCV "
44  ">=2.0")
45 #endif
46 }
47 
48 // ------------------------------------------------------
49 // ~CCascadeClassifierDetection
50 // ------------------------------------------------------
51 
53 {
54 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
55  delete CASCADE;
56 #endif
57 }
58 
59 // ------------------------------------------------------
60 // init
61 // ------------------------------------------------------
62 
64  const mrpt::config::CConfigFileBase& config)
65 {
66 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
67  // load configuration values
68  m_options.cascadeFileName =
69  config.read_string("CascadeClassifier", "cascadeFilename", "");
70  m_options.scaleFactor =
71  config.read_double("DetectionOptions", "scaleFactor", 1.1);
72  m_options.minNeighbors =
73  config.read_int("DetectionOptions", "minNeighbors", 3);
74  m_options.flags = config.read_int("DetectionOptions", "flags", 0);
75  m_options.minSize = config.read_int("DetectionOptions", "minSize", 30);
76 
77  m_cascade = new CascadeClassifier();
78 
79  // Load cascade classifier from file
80  CASCADE->load(m_options.cascadeFileName);
81 
82  // Check if cascade is empty
83  if (CASCADE->empty()) throw std::runtime_error("Incorrect cascade file.");
84 #endif
85 }
86 
87 // ------------------------------------------------------
88 // detectObjects (*CObservation)
89 // ------------------------------------------------------
90 
92  const CObservation* obs, vector_detectable_object& detected)
93 {
94 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM >= 0x200
95  // Obtain image from generic observation
96  const mrpt::img::CImage* img = nullptr;
97 
98  if (IS_CLASS(obs, CObservationImage))
99  {
100  const CObservationImage* o = static_cast<const CObservationImage*>(obs);
101  img = &o->image;
102  }
103  else if (IS_CLASS(obs, CObservationStereoImages))
104  {
105  const CObservationStereoImages* o =
106  static_cast<const CObservationStereoImages*>(obs);
107  img = &o->imageLeft;
108  }
109  else if (IS_CLASS(obs, CObservation3DRangeScan))
110  {
111  const CObservation3DRangeScan* o =
112  static_cast<const CObservation3DRangeScan*>(obs);
113  img = &o->intensityImage;
114  }
115  if (!img)
116  {
117  std::this_thread::sleep_for(2ms);
118  return;
119  }
120 
121  vector<Rect> objects;
122 
123  // Some needed preprocessing
124  const CImage img_gray(*img, FAST_REF_OR_CONVERT_TO_GRAY);
125 
126  // Convert to IplImage and copy it
127  const IplImage* image = img_gray.getAs<IplImage>();
128 
129  // Detect objects
130  CASCADE->detectMultiScale(
131  cv::cvarrToMat(image), objects, m_options.scaleFactor,
132  m_options.minNeighbors, m_options.flags,
133  Size(m_options.minSize, m_options.minSize));
134 
135  unsigned int N = objects.size();
136  // detected.resize( N );
137 
138  // Convert from cv::Rect to vision::CDetectable2D
139  for (unsigned int i = 0; i < N; i++)
140  {
142  new CDetectable2D(
143  objects[i].x, objects[i].y, objects[i].height,
144  objects[i].width));
145 
146  detected.push_back((CDetectableObject::Ptr)obj);
147  }
148 #endif
149 }
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img.getAs<IplImage>()" so we can avoid here including OpenCV&#39;s headers.
Definition: img/CImage.h:599
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement, as from a time-of-flight range camera or any other RGBD sensor.
virtual void detectObjects_Impl(const mrpt::obs::CObservation *obs, vector_detectable_object &detected)
Detect objects in a *CObservation.
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3551
virtual void init(const mrpt::config::CConfigFileBase &cfg)
Initialize cascade classifier detection.
STL namespace.
std::vector< CDetectableObject::Ptr > vector_detectable_object
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
GLenum GLsizei width
Definition: glext.h:3531
std::shared_ptr< CDetectable2D > Ptr
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
This class allows loading and storing values and vectors of different types from a configuration text...
mrpt::img::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
GLint GLvoid * img
Definition: glext.h:3763
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
Observation class for either a pair of left+right or left+disparity images from a stereo camera...
This namespace contains representation of robot actions and observations.
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::rtti::CObject) is of the give...
Definition: CObject.h:102
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
GLenum GLsizei GLsizei height
Definition: glext.h:3554
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020