combo_marker_estimator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
33 #include "pose_estimation_base.h"
34 
36 
37 }
38 
39 static cv::Mat getRTMatrix(const cv::Mat &_rvec, const cv::Mat &_tvec) {
40  if (_rvec.empty())
41  return cv::Mat();
42  cv::Mat m = cv::Mat::eye(4, 4, CV_32FC1);
43  cv::Mat R33 = cv::Mat(m, cv::Rect(0, 0, 3, 3));
44  cv::Rodrigues(_rvec, R33);
45  for (int i = 0; i < 3; i++)
46  m.at<float>(i, 3) = _tvec.ptr<float>(0)[i];
47  return m;
48 }
49 
50 static double max_search_pixel_radius = 10;
51 
52 void
53 ComboMarkerEstimator::estimatePose(std::vector <MarkerFiducials> &idFiducials, cv::Mat &camera_k, cv::Mat &camera_d,
54  std::vector <MarkerFiducials> &ellipseFiducials,
55  std::vector <MarkerPose> &markerPoses) {
56 
57  for (auto &fiducial:idFiducials) {
58  std::cout << "FiducialId - i: " << std::endl << fiducial.image_points << std::endl << " o: " << std::endl
59  << fiducial.object_points << std::endl;
60  }
61 
62  std::vector<MarkerPose> idMarkerPoses;
63  PoseEstimationBase idPoseEstimation;
64  idPoseEstimation.estimatePose(idFiducials, camera_k, camera_d, idMarkerPoses);
65 
66  // FIXME: Provide this via config file
67  std::vector<cv::Point3f> objectPoints;
68  objectPoints.push_back(cv::Point3f(0.055f, 0.0f, 0.0f));
69  objectPoints.push_back(cv::Point3f(0.00f, 0.055f, 0.0f));
70  objectPoints.push_back(cv::Point3f(-0.055f, 0.0f, 0.0f));
71  objectPoints.push_back(cv::Point3f(0.00f, -0.055f, 0.0f));
72 
73  /*
74  objectPoints.push_back(cv::Point3f(0.08f, 0.0f, 0.0f));
75  objectPoints.push_back(cv::Point3f(0.00f, 0.06f, 0.0f));
76  objectPoints.push_back(cv::Point3f(-0.08f, 0.0f, 0.0f));
77  */
78 
79 
80  std::vector<MarkerFiducials> combinedFiucials;
81 
82  for (auto &markerPose:idMarkerPoses) {
83  MarkerFiducials marker(markerPose.ids, markerPose.ids_confidence);
84 
85  std::vector<cv::Point2f> imagePoints;
86  cv::projectPoints(objectPoints, markerPose.getRVec(), markerPose.getTVec(), camera_k, camera_d, imagePoints);
87 
88  for (int i = 0; i < objectPoints.size(); i++) {
89  cv::Point2f ellipseCenter = cv::Point2f(-1.0f, -1.0f);
90 
91  // Iterate trough all ellipses found and try to match the closest
92  cv::Point2f searchCenter = imagePoints[i];
93  for (auto &ellipseFiducial:ellipseFiducials) {
94  cv::Point2f c = ellipseFiducial.image_points[0];
95  double dist = cv::norm(searchCenter - c);
96  if (dist < max_search_pixel_radius) {
97  std::cout << dist << std::endl;
98  ellipseCenter = c; // FIXME: This is just using the first point found in range. No match closest is done yet.
99  }
100  }
101 
102  // Check if a valid candidate was found
103  if (ellipseCenter.x >= 0 && ellipseCenter.y >= 0) {
104  marker.object_points.push_back(objectPoints[i]);
105  marker.image_points.push_back(ellipseCenter);
106  }
107  }
108 
109  // Check if there is a minimum of 3 image points
110  if (marker.image_points.size() > 3) {
111  combinedFiucials.push_back(marker);
112  }
113  }
114 
115  for (auto &fiducial:combinedFiucials) {
116  std::cout << "FiducialCombined - i: " << std::endl << fiducial.image_points << std::endl << " o: " << std::endl
117  << fiducial.object_points << std::endl;
118  }
119 
120  PoseEstimationBase combinedPoseEstimation;
121  combinedPoseEstimation.estimatePose(combinedFiucials, camera_k, camera_d, markerPoses);
122 
123  /*
124  for (auto &fiducial:combinedFiucials) {
125  // Estimate MarkerMap pose
126  if (fiducial.image_points.size() > 2 && fiducial.object_points.size() > 2) {
127  MarkerPose pose(fiducial.ids, fiducial.ids_confidence);
128  {
129  cv::Mat rv, tv;
130  cv::solvePnP(fiducial.object_points, fiducial.image_points, camera_k, camera_d, rv, tv, cv::SOLVEPNP_DLS);
131  //cv::solvePnPRansac(fiducial.object_points, fiducial.image_points, camera_k, camera_d, rv, tv, cv::SOLVEPNP_P3P);
132 
133  cv::Mat rvec, tvec;
134  rv.convertTo(rvec, CV_32F);
135  tv.convertTo(tvec, CV_32F);
136 
137  pose.rt_matrix = getRTMatrix(rvec, tvec);
138  }
139  if (!pose.rt_matrix.empty())
140  markerPoses.push_back(pose);
141  }
142  }
143  */
144 }
f
void estimatePose(std::vector< MarkerFiducials > &idFiducials, cv::Mat &camera_k, cv::Mat &camera_d, std::vector< MarkerFiducials > &ellipseFiducials, std::vector< MarkerPose > &markerPoses)
static double max_search_pixel_radius
void estimatePose(std::vector< MarkerFiducials > &markers, cv::Mat &camera_k, cv::Mat &camera_d, std::vector< MarkerPose > &markerPoses)
static cv::Mat getRTMatrix(const cv::Mat &_rvec, const cv::Mat &_tvec)
std::vector< cv::Point3f > object_points
std::vector< cv::Point2f > image_points


tuw_marker_pose_estimation
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:42:13