35#include <visp3/core/vpConfig.h>
37#if (VISP_HAVE_OPENCV_VERSION >= 0x020408) && \
38 (VISP_HAVE_OPENCV_VERSION < 0x030000)
40#include <visp3/core/vpColor.h>
41#include <visp3/core/vpDisplay.h>
42#include <visp3/core/vpImageConvert.h>
43#include <visp3/core/vpImageTools.h>
44#include <visp3/vision/vpFernClassifier.h>
50vpFernClassifier::vpFernClassifier()
52 gen(0, 256, 5, true, 0.6, 1.5, -CV_PI / 2, CV_PI / 2, -CV_PI / 2, CV_PI / 2), hasLearn(false), threshold(20),
53 nbView(2000), dist(2), nbClassfier(100), ClassifierSize(11), nbOctave(2), patchSize(32), radius(7), nbPoints(200),
54 blurImage(true), radiusBlur(7), sigmaBlur(1), nbMinPoint(10),
55 curImg(), objKeypoints(), modelROI_Ref(), modelROI(), modelPoints(), imgKeypoints(), refPt(), curPt()
69vpFernClassifier::vpFernClassifier(
const std::string &_dataFile,
const std::string &_objectName)
71 gen(0, 256, 5, true, 0.6, 1.5, -CV_PI / 2, CV_PI / 2, -CV_PI / 2, CV_PI / 2), hasLearn(false), threshold(20),
72 nbView(2000), dist(2), nbClassfier(100), ClassifierSize(11), nbOctave(2), patchSize(32), radius(7), nbPoints(200),
73 blurImage(true), radiusBlur(7), sigmaBlur(1), nbMinPoint(10),
74 curImg(), objKeypoints(), modelROI_Ref(), modelROI(), modelPoints(), imgKeypoints(), refPt(), curPt()
76 this->load(_dataFile, _objectName);
83vpFernClassifier::~vpFernClassifier()
92void vpFernClassifier::init()
113void vpFernClassifier::train()
116 cv::LDetector d(radius, threshold, nbOctave, nbView, patchSize, dist);
119 cv::Mat obj = (cv::Mat)curImg;
121 if (this->getBlurSetting()) {
122 cv::GaussianBlur(obj, obj, cv::Size(getBlurSize(), getBlurSize()), getBlurSigma(), getBlurSigma());
126 std::vector<cv::Mat> objpyr;
127 cv::buildPyramid(obj, objpyr, d.nOctaves - 1);
130 d.getMostStable2D(obj, objKeypoints, 100, gen);
135 modelROI = cv::Rect(0, 0, objpyr[0].cols, objpyr[0].rows);
136 ldetector.getMostStable2D(objpyr[0], modelPoints, 100, gen);
138 fernClassifier.trainFromSingleView(objpyr[0], modelPoints, patchSize, (
int)modelPoints.size(), 100, 11, 10000,
139 cv::FernClassifier::COMPRESSION_NONE, gen);
142 referenceImagePointsList.resize(0);
143 for (
unsigned int i = 0; i < modelPoints.size(); i += 1) {
144 vpImagePoint ip(modelPoints[i].pt.y + modelROI_Ref.y, modelPoints[i].pt.x + modelROI_Ref.x);
145 referenceImagePointsList.push_back(ip);
171 _reference_computed =
true;
172 return (
unsigned int)objKeypoints.size();
194 const unsigned int _height,
const unsigned int _width)
197 vpTRACE(
"Bad size for the subimage");
203 this->setImage(subImage);
207 modelROI_Ref.x = (int)_iP.
get_u();
208 modelROI_Ref.y = (int)_iP.
get_v();
209 modelROI_Ref.width = (int)_width;
210 modelROI_Ref.height = (int)_height;
214 return (
unsigned int)objKeypoints.size();
238 return (this->buildReference(_I, iP, (
unsigned int)_rectangle.
getHeight(), (
unsigned int)_rectangle.
getWidth()));
264 cv::Mat img = this->curImg;
266 if (this->getBlurSetting()) {
267 cv::GaussianBlur(img, img, cv::Size(this->getBlurSize(), this->getBlurSize()), this->getBlurSigma(),
268 this->getBlurSigma());
271 std::vector<cv::Mat> imgPyr;
272 cv::buildPyramid(img, imgPyr, ldetector.nOctaves - 1);
274 ldetector(imgPyr, imgKeypoints, 500);
276 unsigned int m = (
unsigned int)modelPoints.size();
277 unsigned int n = (
unsigned int)imgKeypoints.size();
278 std::vector<int> bestMatches(m, -1);
279 std::vector<float> maxLogProb(m, -FLT_MAX);
280 std::vector<float> signature;
281 unsigned int totalMatch = 0;
284 currentImagePointsList.resize(0);
285 matchedReferencePoints.resize(0);
287 for (
unsigned int i = 0; i < n; i++) {
288 cv::KeyPoint kpt = imgKeypoints[i];
289 kpt.pt.x /= (float)(1 << kpt.octave);
290 kpt.pt.y /= (float)(1 << kpt.octave);
291 int k = fernClassifier(imgPyr[(
unsigned int)kpt.octave], kpt.pt, signature);
292 if (k >= 0 && (bestMatches[(
unsigned int)k] < 0 || signature[(
unsigned int)k] > maxLogProb[(
unsigned int)k])) {
293 maxLogProb[(
unsigned int)k] = signature[(
unsigned int)k];
294 bestMatches[(
unsigned int)k] = (
int)i;
297 vpImagePoint ip_cur(imgKeypoints[i].pt.y, imgKeypoints[i].pt.x);
299 currentImagePointsList.push_back(ip_cur);
300 matchedReferencePoints.push_back((
unsigned int)k);
306 for (
unsigned int i = 0; i < m; i++) {
307 if (bestMatches[i] >= 0) {
308 refPt.push_back(modelPoints[i].pt);
309 curPt.push_back(imgKeypoints[(
unsigned int)bestMatches[i]].pt);
330 const unsigned int _height,
const unsigned int _width)
333 vpTRACE(
"Bad size for the subimage");
341 return this->matchPoint(subImage);
360 return (this->matchPoint(_I, iP, (
unsigned int)_rectangle.
getHeight(), (
unsigned int)_rectangle.
getWidth()));
383 for (
unsigned int i = 0; i < matchedReferencePoints.size(); i++) {
402 for (
unsigned int i = 0; i < matchedReferencePoints.size(); i++) {
418void vpFernClassifier::load(
const std::string &_dataFile,
const std::string & )
420 std::cout <<
" > Load data for the planar object detector..." << std::endl;
423 cv::FileStorage fs(_dataFile, cv::FileStorage::READ);
424 cv::FileNode node = fs.getFirstTopLevelNode();
426 cv::FileNodeIterator it = node[
"model-roi"].begin(), it_end;
427 it >> modelROI.x >> modelROI.y >> modelROI.width >> modelROI.height;
429 ldetector.read(node[
"detector"]);
430 fernClassifier.read(node[
"fern-classifier"]);
432 const cv::FileNode node_ = node[
"model-points"];
433 cv::read(node_, modelPoints);
435 cv::LDetector d(radius, threshold, nbOctave, nbView, patchSize, dist);
447void vpFernClassifier::record(
const std::string &_objectName,
const std::string &_dataFile)
450 cv::FileStorage fs(_dataFile, cv::FileStorage::WRITE);
452 cv::WriteStructContext ws(fs, _objectName, CV_NODE_MAP);
455 cv::WriteStructContext wsroi(fs,
"model-roi", CV_NODE_SEQ + CV_NODE_FLOW);
456 cv::write(fs, modelROI_Ref.x);
457 cv::write(fs, modelROI_Ref.y);
458 cv::write(fs, modelROI_Ref.width);
459 cv::write(fs, modelROI_Ref.height);
462 ldetector.write(fs,
"detector");
463 cv::write(fs,
"model-points", modelPoints);
464 fernClassifier.write(fs,
"fern-classifier");
478#elif !defined(VISP_BUILD_SHARED_LIBS)
481void dummy_vpFernClassifier() { };
class that defines what is a keypoint. This class provides all the basic elements to implement classe...
Class to define RGB colors available for display functionalities.
static const vpColor green
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
error that can be emitted by ViSP classes.
@ notInitialized
Used to indicate that a parameter is not initialized.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
@ notInTheImage
Pixel not in the image.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition of the vpImage class member functions.
unsigned int getWidth() const
unsigned int getHeight() const
Defines a rectangle in the plane.