MRPT  2.0.4
CCascadeClassifierDetection.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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/3rdparty/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 auto& o = static_cast<const CObservationImage&>(obs);
101  img = &o.image;
102  }
103  else if (IS_CLASS(obs, CObservationStereoImages))
104  {
105  const auto& o = static_cast<const CObservationStereoImages&>(obs);
106  img = &o.imageLeft;
107  }
108  else if (IS_CLASS(obs, CObservation3DRangeScan))
109  {
110  const auto& o = static_cast<const CObservation3DRangeScan&>(obs);
111  img = &o.intensityImage;
112  }
113  if (!img)
114  {
115  std::this_thread::sleep_for(2ms);
116  return;
117  }
118 
119  vector<Rect> objects;
120 
121  // Some needed preprocessing
122  const CImage img_gray(*img, FAST_REF_OR_CONVERT_TO_GRAY);
123 
124  // Convert to IplImage and copy it
125  const cv::Mat& image = img_gray.asCvMatRef();
126 
127  // Detect objects
128  CASCADE->detectMultiScale(
129  image, objects, m_options.scaleFactor, m_options.minNeighbors,
130  m_options.flags, Size(m_options.minSize, m_options.minSize));
131 
132  unsigned int N = objects.size();
133  // detected.resize( N );
134 
135  // Convert from cv::Rect to vision::CDetectable2D
136  for (unsigned int i = 0; i < N; i++)
137  {
138  auto obj = std::make_shared<CDetectable2D>(
139  objects[i].x, objects[i].y, objects[i].height, objects[i].width);
140 
141  detected.push_back((CDetectableObject::Ptr)obj);
142  }
143 #endif
144 }
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
cv::Mat & asCvMatRef()
Get a reference to the internal cv::Mat, which can be resized, etc.
Definition: CImage.cpp:230
A range or depth 3D scan measurement, as from a time-of-flight range camera or a structured-light dep...
STL namespace.
std::vector< CDetectableObject::Ptr > vector_detectable_object
Definition: img/CImage.h:23
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
void detectObjects_Impl(const mrpt::obs::CObservation &obs, vector_detectable_object &detected) override
Detect objects in a *CObservation.
This class allows loading and storing values and vectors of different types from a configuration text...
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.
#define IS_CLASS(obj, class_name)
True if the given reference to object (derived from mrpt::rtti::CObject) is of the given class...
Definition: CObject.h:146
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
void init(const mrpt::config::CConfigFileBase &cfg) override
Initialize cascade classifier detection.
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:148



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020