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) 2014, OpenCV Foundation, all rights reserved.
14  // Third party copyrights are property of their respective owners.
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_CCALIB_HPP__
43 #define __OPENCV_CCALIB_HPP__
44 
45 #include <opencv2/core.hpp>
46 #include <opencv2/features2d.hpp>
47 #include <opencv2/imgproc.hpp>
48 #include <opencv2/calib3d.hpp>
49 
50 #include <vector>
51 
52 /** @defgroup ccalib Custom Calibration Pattern for 3D reconstruction
53 */
54 
55 namespace cv{ namespace ccalib{
56 
57 //! @addtogroup ccalib
58 //! @{
59 
60 class CV_EXPORTS CustomPattern : public Algorithm
61 {
62 public:
63 	CustomPattern();
64 	virtual ~CustomPattern();
65 
66 	bool create(InputArray pattern, const Size2f boardSize, OutputArray output = noArray());
67 
68 	bool findPattern(InputArray image, OutputArray matched_features, OutputArray pattern_points, const double ratio = 0.7,
69 					 const double proj_error = 8.0, const bool refine_position = false, OutputArray out = noArray(),
70 					 OutputArray H = noArray(), OutputArray pattern_corners = noArray());
71 
72 	bool isInitialized();
73 
74 	void getPatternPoints(std::vector<KeyPoint>& original_points);
75     /**<
76 		Returns a vector<Point> of the original points.
77 	*/
78 	double getPixelSize();
79     /**<
80 		Get the pixel size of the pattern
81 	*/
82 
83 	bool setFeatureDetector(Ptr<FeatureDetector> featureDetector);
84 	bool setDescriptorExtractor(Ptr<DescriptorExtractor> extractor);
85 	bool setDescriptorMatcher(Ptr<DescriptorMatcher> matcher);
86 
87 	Ptr<FeatureDetector> getFeatureDetector();
88 	Ptr<DescriptorExtractor> getDescriptorExtractor();
89 	Ptr<DescriptorMatcher> getDescriptorMatcher();
90 
91 	double calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
92 				Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
93 				OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags = 0,
94 				TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
95     /**<
96 		Calls the calirateCamera function with the same inputs.
97 	*/
98 
99     bool findRt(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs,
100                 InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE);
101     bool findRt(InputArray image, InputArray cameraMatrix, InputArray distCoeffs,
102                 InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int flags = SOLVEPNP_ITERATIVE);
103     /**<
104 		Uses solvePnP to find the rotation and translation of the pattern
105 		with respect to the camera frame.
106 	*/
107 
108     bool findRtRANSAC(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs,
109                       InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int iterationsCount = 100,
110                       float reprojectionError = 8.0, int minInliersCount = 100, OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE);
111     bool findRtRANSAC(InputArray image, InputArray cameraMatrix, InputArray distCoeffs,
112                       InputOutputArray rvec, InputOutputArray tvec, bool useExtrinsicGuess = false, int iterationsCount = 100,
113                       float reprojectionError = 8.0, int minInliersCount = 100, OutputArray inliers = noArray(), int flags = SOLVEPNP_ITERATIVE);
114         /**<
115 		Uses solvePnPRansac()
116 	*/
117 
118 	void drawOrientation(InputOutputArray image, InputArray tvec, InputArray rvec, InputArray cameraMatrix,
119 						 InputArray distCoeffs, double axis_length = 3, int axis_width = 2);
120     /**<
121 		pattern_corners -> projected over the image position of the edges of the pattern.
122 	*/
123 
124 private:
125 
126 	Mat img_roi;
127 	std::vector<Point2f> obj_corners;
128 	double pxSize;
129 
130 	bool initialized;
131 
132 	Ptr<FeatureDetector> detector;
133 	Ptr<DescriptorExtractor> descriptorExtractor;
134 	Ptr<DescriptorMatcher> descriptorMatcher;
135 
136 	std::vector<KeyPoint> keypoints;
137 	std::vector<Point3f> points3d;
138 	Mat descriptor;
139 
140 	bool init(Mat& image, const float pixel_size, OutputArray output = noArray());
141 	bool findPatternPass(const Mat& image, std::vector<Point2f>& matched_features, std::vector<Point3f>& pattern_points,
142 						 Mat& H, std::vector<Point2f>& scene_corners, const double pratio, const double proj_error,
143 						 const bool refine_position = false, const Mat& mask = Mat(), OutputArray output = noArray());
144 	void scaleFoundPoints(const double squareSize, const std::vector<KeyPoint>& corners, std::vector<Point3f>& pts3d);
145 	void check_matches(std::vector<Point2f>& matched, const std::vector<Point2f>& pattern, std::vector<DMatch>& good, std::vector<Point3f>& pattern_3d, const Mat& H);
146 
147 	void keypoints2points(const std::vector<KeyPoint>& in, std::vector<Point2f>& out);
148 	void updateKeypointsPos(std::vector<KeyPoint>& in, const std::vector<Point2f>& new_pos);
149 	void refinePointsPos(const Mat& img, std::vector<Point2f>& p);
150 	void refineKeypointsPos(const Mat& img, std::vector<KeyPoint>& kp);
151 };
152 
153 //! @}
154 
155 }} // namespace ccalib, cv
156 
157 #endif
158