Visual Servoing Platform  version 3.1.0
vpPlanarObjectDetector.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Planar surface detection tool.
33  *
34  * Authors:
35  * Romain Tallonneau
36  *
37  *****************************************************************************/
38 
39 #ifndef VPPLANAROBJECTDETECTOR_H_
40 #define VPPLANAROBJECTDETECTOR_H_
41 
42 #include <visp3/core/vpConfig.h>
43 
44 #if (VISP_HAVE_OPENCV_VERSION >= 0x020000) && \
45  (VISP_HAVE_OPENCV_VERSION < 0x030000) // Require opencv >= 2.0.0 and < 3.0.0
46 
47 #if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
48 #include <opencv2/calib3d/calib3d.hpp>
49 #include <opencv2/features2d/features2d.hpp>
50 #include <opencv2/imgproc/imgproc.hpp>
51 #elif (VISP_HAVE_OPENCV_VERSION >= 0x020000) // Require opencv >= 2.0.0
52 #include <cv.h>
53 #include <cvaux.hpp>
54 #endif
55 
56 #include <visp3/core/vpCameraParameters.h>
57 #include <visp3/core/vpHomogeneousMatrix.h>
58 #include <visp3/core/vpImage.h>
59 #include <visp3/core/vpImagePoint.h>
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/core/vpRect.h>
62 #include <visp3/vision/vpFernClassifier.h>
63 #include <visp3/vision/vpHomography.h>
64 
141 class VISP_EXPORT vpPlanarObjectDetector
142 {
143 protected:
147 
151  cv::Mat H;
152 
155  std::vector<cv::Point2f> dst_corners;
156 
158  bool isCorrect;
159 
161  std::vector<cv::Point2f> ref_corners;
162 
164  cv::Rect modelROI;
165 
168  std::vector<vpImagePoint> currentImagePoints;
171  std::vector<vpImagePoint> refImagePoints;
172 
175  unsigned int minNbMatching;
176 
177 public:
178  // constructors and destructors
180  vpPlanarObjectDetector(const std::string &dataFile, const std::string &objectName);
181  virtual ~vpPlanarObjectDetector();
182 
183  // main functions
184  // creation of reference
185  unsigned int buildReference(const vpImage<unsigned char> &I);
186  unsigned int buildReference(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int height,
187  unsigned int width);
188  unsigned int buildReference(const vpImage<unsigned char> &I, const vpRect &rectangle);
189 
190  // matching
191  bool matchPoint(const vpImage<unsigned char> &I);
192  bool matchPoint(const vpImage<unsigned char> &I, const vpImagePoint &iP, const unsigned int height,
193  const unsigned int width);
194  bool matchPoint(const vpImage<unsigned char> &I, const vpRect &rectangle);
195  // database management
196  void recordDetector(const std::string &objectName, const std::string &dataFile);
197  void load(const std::string &dataFilename, const std::string &objName);
198 
199  // display
200  void display(vpImage<unsigned char> &I, bool displayKpts = false);
201  void display(vpImage<unsigned char> &Iref, vpImage<unsigned char> &Icurrent, bool displayKpts = false);
202 
209  std::vector<vpImagePoint> getDetectedCorners() const;
210 
216  vpFernClassifier &getFernClassifier() { return this->fern; }
217 
224  inline void getHomography(vpHomography &_H) const { _H = this->homography; }
225 
231  inline unsigned int getNbRefPoints() { return (unsigned int)currentImagePoints.size(); }
232 
241  void getReferencePoint(const unsigned int _i, vpImagePoint &_imPoint);
242 
254  void getMatchedPoints(const unsigned int _index, vpImagePoint &_referencePoint, vpImagePoint &_currentPoint);
255 
262  void setMinNbPointValidation(const unsigned int _min) { this->minNbMatching = _min; }
263 
270  unsigned int getMinNbPointValidation() const { return this->minNbMatching; }
271 
272 protected:
273  virtual void init();
274  void computeRoi(vpImagePoint *ip, const unsigned int nbpt);
275  void initialiseRefCorners(const cv::Rect &_modelROI);
276 };
277 
278 #endif
279 
280 #endif /* VPPLANAROBJECTDETECTOR_H_ */
vpFernClassifier & getFernClassifier()
std::vector< cv::Point2f > ref_corners
The corners in the reference image.
cv::Rect modelROI
The ROI for the reference image.
cv::Mat H
Computed homography in the OpenCV format.
void setMinNbPointValidation(const unsigned int _min)
Implementation of an homography and operations on homographies.
Definition: vpHomography.h:174
std::vector< cv::Point2f > dst_corners
std::vector< vpImagePoint > currentImagePoints
vpHomography homography
Computed homography in the ViSP format.
unsigned int getMinNbPointValidation() const
bool isCorrect
Flag to indicate wether the last computed homography is correct or not.
Defines a rectangle in the plane.
Definition: vpRect.h:78
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
Class used to detect a planar surface.
void getHomography(vpHomography &_H) const
std::vector< vpImagePoint > refImagePoints
Class that implements the Fern classifier and the YAPE detector thanks to the OpenCV library...