| ARX 1.0 The next-generation open source augmented reality toolkit.

Loading...
Searching...
No Matches
OCVUtils.h
Go to the documentation of this file.
1/*
2 * OCVUtils.h
3 * artoolkitX
4 *
5 * This file is part of artoolkitX.
6 *
7 * artoolkitX is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * artoolkitX is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with artoolkitX. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * As a special exception, the copyright holders of this library give you
21 * permission to link this library with independent modules to produce an
22 * executable, regardless of the license terms of these independent modules, and to
23 * copy and distribute the resulting executable under terms of your choice,
24 * provided that you also meet, for each linked independent module, the terms and
25 * conditions of the license of that module. An independent module is a module
26 * which is neither derived from nor based on this library. If you modify this
27 * library, you may extend this exception to your version of the library, but you
28 * are not obligated to do so. If you do not wish to do so, delete this exception
29 * statement from your version.
30 *
31 * Copyright 2018 Realmax, Inc.
32 * Copyright 2015 Daqri, LLC.
33 * Copyright 2010-2015 ARToolworks, Inc.
34 *
35 * Author(s): Philip Lamb, Daniel Bell.
36 *
37 */
38
39#ifndef OCV_UTILS_H
40#define OCV_UTILS_H
41#include "OCVConfig.h"
42
43std::vector<cv::Point2f> Points(std::vector<cv::KeyPoint> keypoints)
44{
45 std::vector<cv::Point2f> res;
46 for(unsigned i = 0; i < keypoints.size(); i++) {
47 res.push_back(keypoints[i].pt);
48 }
49 return res;
50}
51
52//Method for calculating and validating a homography matrix from a set of corresponding points.
53 HomographyInfo GetHomographyInliers(std::vector<cv::Point2f> pts1, std::vector<cv::Point2f> pts2)
54{
55 cv::Mat inlier_mask, homography;
56 std::vector<cv::DMatch> inlier_matches;
57 if(pts1.size() >= 4) {
58 homography = findHomography(pts1, pts2,
59 cv::RANSAC, ransac_thresh, inlier_mask);
60 }
61
62 //Failed to find a homography
63 if(pts1.size() < 4 || homography.empty()) {
64 return HomographyInfo();
65 }
66
67 const double det = homography.at<double>(0, 0) * homography.at<double>(1, 1) - homography.at<double>(1, 0) * homography.at<double>(0, 1);
68 if (det < 0)
69 return HomographyInfo();
70
71 const double N1 = sqrt(homography.at<double>(0, 0) * homography.at<double>(0, 0) + homography.at<double>(1, 0) * homography.at<double>(1, 0));
72 if (N1 > 4 || N1 < 0.1)
73 return HomographyInfo();
74
75 const double N2 = sqrt(homography.at<double>(0, 1) * homography.at<double>(0, 1) + homography.at<double>(1, 1) * homography.at<double>(1, 1));
76 if (N2 > 4 || N2 < 0.1)
77 return HomographyInfo();
78
79 const double N3 = sqrt(homography.at<double>(2, 0) * homography.at<double>(2, 0) + homography.at<double>(2, 1) * homography.at<double>(2, 1));
80 if (N3 > 0.002)
81 return HomographyInfo();
82
83 std::vector<uchar> status;
84 int linliers = 0;
85 for(int i = 0; i < pts1.size(); i++) {
86 if((int)inlier_mask.at<uchar>(i,0)==1) {
87 status.push_back((uchar)1);
88 inlier_matches.push_back(cv::DMatch(i, i, 0));
89 linliers++;
90 }
91 else {
92 status.push_back((uchar)0);
93 }
94 }
95 //Return homography and corresponding inlier point sets
96 return HomographyInfo(homography, status, inlier_matches);
97}
98
99#endif
ransac_thresh
const double ransac_thresh
RANSAC inlier threshold.
Definition: OCVConfig.cpp:51
OCVConfig.h
GetHomographyInliers
HomographyInfo GetHomographyInliers(std::vector< cv::Point2f > pts1, std::vector< cv::Point2f > pts2)
Definition: OCVUtils.h:53
Points
std::vector< cv::Point2f > Points(std::vector< cv::KeyPoint > keypoints)
Definition: OCVUtils.h:43
HomographyInfo
Definition: HomographyInfo.h:44