1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2015, Baisheng Lai (laibaisheng@gmail.com), Zhejiang University,
14 // all rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of the copyright holders may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41 
42 #ifndef __OPENCV_RANDOMPATTERN_HPP__
43 #define __OPENCV_RANDOMPATTERN_HPP__
44 
45 #include "opencv2/features2d.hpp"
46 #include "opencv2/highgui.hpp"
47 
48 namespace cv { namespace randpattern {
49 
50 
51 //! @addtogroup ccalib
52 //! @{
53 
54 /** @brief Class for finding features points and corresponding 3D in world coordinate of
55 a "random" pattern, which can be to be used in calibration. It is useful when pattern is
56 partly occluded or only a part of pattern can be observed in multiple cameras calibration.
57 The pattern can be generated by RandomPatternGenerator class described in this file.
58 
59 Please refer to paper
60     B. Li, L. Heng, K. Kevin  and M. Pollefeys, "A Multiple-Camera System
61     Calibration Toolbox Using A Feature Descriptor-Based Calibration
62     Pattern", in IROS 2013.
63 */
64 
65 class CV_EXPORTS RandomPatternCornerFinder
66 {
67 public:
68 
69     /* @brief Construct RandomPatternCornerFinder object
70 
71     @param patternWidth the real width of "random" pattern in a user defined unit.
72     @param patternHeight the real height of "random" pattern in a user defined unit.
73     @param nMiniMatch number of minimal matches, otherwise that image is abandoned
74     @depth depth of output objectPoints and imagePoints, set it to be CV_32F or CV_64F.
75     @showExtraction whether show feature extraction, 0 for no and 1 for yes.
76     @detector feature detector to detect feature points in pattern and images.
77     @descriptor feature descriptor.
78     @matcher feature matcher.
79     */
80     RandomPatternCornerFinder(float patternWidth, float patternHeight,
81         int nminiMatch = 20, int depth = CV_32F, int verbose = 0, int showExtraction = 0,
82         Ptr<FeatureDetector> detector = AKAZE::create(AKAZE::DESCRIPTOR_MLDB, 0, 3, 0.005f),
83         Ptr<DescriptorExtractor> descriptor = AKAZE::create(AKAZE::DESCRIPTOR_MLDB,0, 3, 0.005f),
84         Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-L1"));
85 
86     /* @brief Load pattern image and compute features for pattern
87     @param patternImage image for "random" pattern generated by RandomPatternGenerator, run it first.
88     */
89     void loadPattern(const cv::Mat &patternImage);
90 
91     /* @brief Load pattern and features
92 	@param patternImage image for "random" pattern generated by RandomPatternGenerator, run it first.
93 	@param patternKeyPoints keyPoints created from a FeatureDetector.
94 	@param patternDescriptors descriptors created from a DescriptorExtractor.
95 	*/
96     void loadPattern(const cv::Mat &patternImage, const std::vector<cv::KeyPoint> &patternKeyPoints, const cv::Mat &patternDescriptors);
97 
98     /* @brief Compute matched object points and image points which are used for calibration
99     The objectPoints (3D) and imagePoints (2D) are stored inside the class. Run getObjectPoints()
100     and getImagePoints() to get them.
101 
102     @param inputImages vector of 8-bit grayscale images containing "random" pattern
103     that are used for calibration.
104     */
105     void computeObjectImagePoints(std::vector<cv::Mat> inputImages);
106 
107     //void computeObjectImagePoints2(std::vector<cv::Mat> inputImages);
108 
109     /* @brief Compute object and image points for a single image. It returns a vector<Mat> that
110     the first element stores the imagePoints and the second one stores the objectPoints.
111 
112     @param inputImage single input image for calibration
113     */
114     std::vector<cv::Mat> computeObjectImagePointsForSingle(cv::Mat inputImage);
115 
116     /* @brief Get object(3D) points
117     */
118     const std::vector<cv::Mat> &getObjectPoints();
119 
120     /* @brief and image(2D) points
121     */
122     const std::vector<cv::Mat> &getImagePoints();
123 
124 private:
125 
126     std::vector<cv::Mat> _objectPonits, _imagePoints;
127     float _patternWidth, _patternHeight;
128     cv::Size _patternImageSize;
129     int _nminiMatch;
130     int _depth;
131 	int _verbose;
132 
133     Ptr<FeatureDetector> _detector;
134     Ptr<DescriptorExtractor> _descriptor;
135     Ptr<DescriptorMatcher> _matcher;
136     Mat _descriptorPattern;
137     std::vector<cv::KeyPoint> _keypointsPattern;
138     Mat _patternImage;
139     int _showExtraction;
140 
141     void keyPoints2MatchedLocation(const std::vector<cv::KeyPoint>& imageKeypoints,
142         const std::vector<cv::KeyPoint>& patternKeypoints, const std::vector<cv::DMatch> matchces,
143         cv::Mat& matchedImagelocation, cv::Mat& matchedPatternLocation);
144     void getFilteredLocation(cv::Mat& imageKeypoints, cv::Mat& patternKeypoints, const cv::Mat mask);
145     void getObjectImagePoints(const cv::Mat& imageKeypoints, const cv::Mat& patternKeypoints);
146     void crossCheckMatching( cv::Ptr<DescriptorMatcher>& descriptorMatcher,
147         const Mat& descriptors1, const Mat& descriptors2,
148         std::vector<DMatch>& filteredMatches12, int knn=1 );
149     void drawCorrespondence(const Mat& image1, const std::vector<cv::KeyPoint> keypoint1,
150         const Mat& image2, const std::vector<cv::KeyPoint> keypoint2, const std::vector<cv::DMatch> matchces,
151         const Mat& mask1, const Mat& mask2, const int step);
152 };
153 
154 /* @brief Class to generate "random" pattern image that are used for RandomPatternCornerFinder
155 Please refer to paper
156 B. Li, L. Heng, K. Kevin  and M. Pollefeys, "A Multiple-Camera System
157 Calibration Toolbox Using A Feature Descriptor-Based Calibration
158 Pattern", in IROS 2013.
159 */
160 class CV_EXPORTS RandomPatternGenerator
161 {
162 public:
163     /* @brief Construct RandomPatternGenerator
164 
165     @param imageWidth image width of the generated pattern image
166     @param imageHeight image height of the generated pattern image
167     */
168     RandomPatternGenerator(int imageWidth, int imageHeight);
169 
170     /* @brief Generate pattern
171     */
172     void generatePattern();
173     /* @brief Get pattern
174     */
175     cv::Mat getPattern();
176 private:
177     cv::Mat _pattern;
178     int _imageWidth, _imageHeight;
179 };
180 
181 //! @}
182 
183 }} //namespace randpattern, cv
184 #endif