Main MRPT website > C++ reference for MRPT 1.5.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-2017, 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 #include <mrpt/system/threads.h> // sleep()
17 
18 // Universal include for all versions of OpenCV
19 #include <mrpt/otherlibs/do_opencv_includes.h>
20 
21 using namespace mrpt::detectors;
22 using namespace mrpt::obs;
23 using namespace mrpt::utils;
24 using namespace std;
25 
26 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
27 using namespace cv;
28 #endif
29 
30 #define CASCADE (reinterpret_cast<CascadeClassifier*>(m_cascade))
31 #define CASCADE_CONST (reinterpret_cast<const CascadeClassifier*>(m_cascade))
32 
33 // ------------------------------------------------------
34 // CCascadeClassifierDetection
35 // ------------------------------------------------------
36 
38 {
39  // Check if MRPT is using OpenCV
40 #if !MRPT_HAS_OPENCV || MRPT_OPENCV_VERSION_NUM<0x200
41  THROW_EXCEPTION("CCascadeClassifierDetection class requires MRPT built against OpenCV >=2.0")
42 #endif
43 }
44 
45 // ------------------------------------------------------
46 // ~CCascadeClassifierDetection
47 // ------------------------------------------------------
48 
50 {
51 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
52  delete CASCADE;
53 #endif
54 }
55 
56 // ------------------------------------------------------
57 // init
58 // ------------------------------------------------------
59 
61 {
62 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
63  // load configuration values
64  m_options.cascadeFileName = config.read_string("CascadeClassifier","cascadeFilename","");
65  m_options.scaleFactor = config.read_double("DetectionOptions","scaleFactor",1.1);
66  m_options.minNeighbors = config.read_int("DetectionOptions","minNeighbors",3);
67  m_options.flags = config.read_int("DetectionOptions","flags",0);
68  m_options.minSize = config.read_int("DetectionOptions","minSize",30);
69 
70  m_cascade = new CascadeClassifier();
71 
72  // Load cascade classifier from file
73  CASCADE->load( m_options.cascadeFileName );
74 
75  // Check if cascade is empty
76  if ( CASCADE->empty() )
77  throw std::runtime_error("Incorrect cascade file.");
78 #endif
79 }
80 
81 // ------------------------------------------------------
82 // detectObjects (*CObservation)
83 // ------------------------------------------------------
84 
86 {
87 #if MRPT_HAS_OPENCV && MRPT_OPENCV_VERSION_NUM>=0x200
88  // Obtain image from generic observation
89  const mrpt::utils::CImage *img = NULL;
90 
91  if (IS_CLASS(obs,CObservationImage))
92  {
93  const CObservationImage* o = static_cast<const CObservationImage*>(obs);
94  img = &o->image;
95  }
96  else if ( IS_CLASS(obs,CObservationStereoImages) )
97  {
98  const CObservationStereoImages* o = static_cast<const CObservationStereoImages*>(obs);
99  img = &o->imageLeft;
100  }
101  else if (IS_CLASS(obs, CObservation3DRangeScan ) )
102  {
103  const CObservation3DRangeScan* o = static_cast<const CObservation3DRangeScan*>(obs);
104  img = &o->intensityImage;
105  }
106  if (!img)
107  {
109  return;
110  }
111 
112  vector<Rect> objects;
113 
114  // Some needed preprocessing
115  const CImage img_gray( *img, FAST_REF_OR_CONVERT_TO_GRAY );
116 
117  // Convert to IplImage and copy it
118  const IplImage *image = img_gray.getAs<IplImage>();
119 
120  // Detect objects
121  CASCADE->detectMultiScale( cv::cvarrToMat(image), objects, m_options.scaleFactor,
122  m_options.minNeighbors, m_options.flags,
123  Size(m_options.minSize,m_options.minSize) );
124 
125  unsigned int N = objects.size();
126  //detected.resize( N );
127 
128  // Convert from cv::Rect to vision::CDetectable2D
129  for ( unsigned int i = 0; i < N; i++ )
130  {
131  CDetectable2DPtr obj =
132  CDetectable2DPtr( new CDetectable2D( objects[i].x, objects[i].y, objects[i].height, objects[i].width ) );
133 
134  detected.push_back((CDetectableObjectPtr)obj);
135  }
136 #endif
137 }
Declares a class derived from "CObservation" that encapsules an image from a camera, whose relative pose to robot is also stored.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
mrpt::utils::CImage imageLeft
Image from the left camera (this image will be ALWAYS present)
A class for storing images as grayscale or RGB bitmaps.
Definition: CImage.h:101
#define THROW_EXCEPTION(msg)
std::vector< CDetectableObjectPtr > vector_detectable_object
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:3522
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
STL namespace.
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
GLenum GLsizei width
Definition: glext.h:3513
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: CImage.h:517
This class allows loading and storing values and vectors of different types from a configuration text...
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
GLint GLvoid * img
Definition: glext.h:3645
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.
virtual void init(const mrpt::utils::CConfigFileBase &cfg)
Initialize cascade classifier detection.
Declares a class that represents any robot&#39;s observation.
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
#define IS_CLASS(ptrObj, class_name)
Evaluates to true if the given pointer to an object (derived from mrpt::utils::CSerializable) is of t...
Definition: CObject.h:93
mrpt::utils::CImage image
The image captured by the camera, that is, the main piece of information of this observation.
GLenum GLint GLint y
Definition: glext.h:3516
GLenum GLint x
Definition: glext.h:3516
mrpt::utils::CImage intensityImage
If hasIntensityImage=true, a color or gray-level intensity image of the same size than "rangeImage"...
GLenum GLsizei GLsizei height
Definition: glext.h:3523



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020