Main MRPT website > C++ reference for 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 }
mrpt::detectors
Definition: CCascadeClassifierDetection.h:17
mrpt::config::CConfigFileBase::read_double
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:101
obj
GLsizei GLsizei GLuint * obj
Definition: glext.h:4070
CObservation3DRangeScan.h
mrpt::img::CImage::getAs
const T * getAs() const
Returns a pointer to a const T* containing the image - the idea is to call like "img....
Definition: img/CImage.h:599
mrpt::obs::CObservationImage::image
mrpt::img::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
Definition: CObservationImage.h:61
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::detectors::CCascadeClassifierDetection::~CCascadeClassifierDetection
virtual ~CCascadeClassifierDetection()
Definition: CCascadeClassifierDetection.cpp:52
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
CObservationStereoImages.h
mrpt::config::CConfigFileBase::read_string
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:169
CASCADE
#define CASCADE
Definition: CCascadeClassifierDetection.cpp:31
mrpt::config::CConfigFileBase::read_int
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:125
mrpt::img
Definition: CCanvas.h:17
mrpt::obs::CObservation3DRangeScan::intensityImage
mrpt::img::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage".
Definition: CObservation3DRangeScan.h:502
mrpt::obs::CObservationStereoImages::imageLeft
mrpt::img::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
Definition: CObservationStereoImages.h:68
mrpt::obs::CObservation3DRangeScan
Declares a class derived from "CObservation" that encapsules a 3D range scan measurement,...
Definition: CObservation3DRangeScan.h:224
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::detectors::CCascadeClassifierDetection::detectObjects_Impl
virtual void detectObjects_Impl(const mrpt::obs::CObservation *obs, vector_detectable_object &detected)
Detect objects in a *CObservation.
Definition: CCascadeClassifierDetection.cpp:91
IS_CLASS
#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:103
mrpt::detectors::CCascadeClassifierDetection::init
virtual void init(const mrpt::config::CConfigFileBase &cfg)
Initialize cascade classifier detection.
Definition: CCascadeClassifierDetection.cpp:63
mrpt::detectors::CDetectable2D::Ptr
std::shared_ptr< CDetectable2D > Ptr
Definition: CDetectableObject.h:48
height
GLenum GLsizei GLsizei height
Definition: glext.h:3554
mrpt::detectors::CDetectableObject::Ptr
std::shared_ptr< CDetectableObject > Ptr
Definition: CDetectableObject.h:30
mrpt::img::CImage
A class for storing images as grayscale or RGB bitmaps.
Definition: img/CImage.h:130
mrpt::img::FAST_REF_OR_CONVERT_TO_GRAY
@ FAST_REF_OR_CONVERT_TO_GRAY
Definition: img/CImage.h:51
mrpt::detectors::CDetectable2D
Definition: CDetectableObject.h:46
mrpt::detectors::vector_detectable_object
std::vector< CDetectableObject::Ptr > vector_detectable_object
Definition: CObjectDetection.h:21
detectors-precomp.h
CCascadeClassifierDetection.h
image
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glext.h:3552
mrpt::detectors::CCascadeClassifierDetection::CCascadeClassifierDetection
CCascadeClassifierDetection()
Definition: CCascadeClassifierDetection.cpp:38
img
GLint GLvoid * img
Definition: glext.h:3763
width
GLenum GLsizei width
Definition: glext.h:3531
mrpt::obs::CObservation
Declares a class that represents any robot's observation.
Definition: CObservation.h:43
cv
Definition: checkerboard_cam_calib.cpp:57
CObservationImage.h
mrpt::obs::CObservationImage
Declares a class derived from "CObservation" that encapsules an image from a camera,...
Definition: CObservationImage.h:35
y
GLenum GLint GLint y
Definition: glext.h:3538
mrpt::obs::CObservationStereoImages
Observation class for either a pair of left+right or left+disparity images from a stereo camera.
Definition: CObservationStereoImages.h:41
x
GLenum GLint x
Definition: glext.h:3538



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST