forked from MasteringOpenCV/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatternDetector.hpp
executable file
·97 lines (80 loc) · 3.41 KB
/
PatternDetector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*****************************************************************************
* Markerless AR desktop application.
******************************************************************************
* by Khvedchenia Ievgen, 5th Dec 2012
* http://computer-vision-talks.com
******************************************************************************
* Ch3 of the book "Mastering OpenCV with Practical Computer Vision Projects"
* Copyright Packt Publishing 2012.
* http://www.packtpub.com/cool-projects-with-opencv/book
*****************************************************************************/
#ifndef EXAMPLE_MARKERLESS_AR_PATTERNDETECTOR_HPP
#define EXAMPLE_MARKERLESS_AR_PATTERNDETECTOR_HPP
////////////////////////////////////////////////////////////////////
// File includes:
#include "Pattern.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/nonfree/features2d.hpp>
class PatternDetector
{
public:
/**
* Initialize a pattern detector with specified feature detector, descriptor extraction and matching algorithm
*/
PatternDetector
(
cv::Ptr<cv::FeatureDetector> detector = new cv::ORB(1000),
cv::Ptr<cv::DescriptorExtractor> extractor = new cv::FREAK(false, false),
cv::Ptr<cv::DescriptorMatcher> matcher = new cv::BFMatcher(cv::NORM_HAMMING, true),
bool enableRatioTest = false
);
/**
*
*/
void train(const Pattern& pattern);
/**
* Initialize Pattern structure from the input image.
* This function finds the feature points and extract descriptors for them.
*/
void buildPatternFromImage(const cv::Mat& image, Pattern& pattern) const;
/**
* Tries to find a @pattern object on given @image.
* The function returns true if succeeded and store the result (pattern 2d location, homography) in @info.
*/
bool findPattern(const cv::Mat& image, PatternTrackingInfo& info);
bool enableRatioTest;
bool enableHomographyRefinement;
float homographyReprojectionThreshold;
protected:
bool extractFeatures(const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, cv::Mat& descriptors) const;
void getMatches(const cv::Mat& queryDescriptors, std::vector<cv::DMatch>& matches);
/**
* Get the gray image from the input image.
* Function performs necessary color conversion if necessary
* Supported input images types - 1 channel (no conversion is done), 3 channels (assuming BGR) and 4 channels (assuming BGRA).
*/
static void getGray(const cv::Mat& image, cv::Mat& gray);
/**
*
*/
static bool refineMatchesWithHomography(
const std::vector<cv::KeyPoint>& queryKeypoints,
const std::vector<cv::KeyPoint>& trainKeypoints,
float reprojectionThreshold,
std::vector<cv::DMatch>& matches,
cv::Mat& homography);
private:
std::vector<cv::KeyPoint> m_queryKeypoints;
cv::Mat m_queryDescriptors;
std::vector<cv::DMatch> m_matches;
std::vector< std::vector<cv::DMatch> > m_knnMatches;
cv::Mat m_grayImg;
cv::Mat m_warpedImg;
cv::Mat m_roughHomography;
cv::Mat m_refinedHomography;
Pattern m_pattern;
cv::Ptr<cv::FeatureDetector> m_detector;
cv::Ptr<cv::DescriptorExtractor> m_extractor;
cv::Ptr<cv::DescriptorMatcher> m_matcher;
};
#endif