common_functions.h
Go to the documentation of this file.
1 
44 #ifndef APRILTAGS2_ROS_COMMON_FUNCTIONS_H
45 #define APRILTAGS2_ROS_COMMON_FUNCTIONS_H
46 
47 #include <string>
48 #include <sstream>
49 #include <vector>
50 #include <map>
51 
52 #include <ros/ros.h>
53 #include <ros/console.h>
54 #include <XmlRpcException.h>
55 #include <cv_bridge/cv_bridge.h>
56 #include <eigen3/Eigen/Dense>
57 #include <eigen3/Eigen/Geometry>
58 #include <opencv2/opencv.hpp>
59 #include <opencv2/imgproc/imgproc.hpp>
60 #include <opencv2/core/core.hpp>
64 
65 #include "apriltags2_ros/AprilTagDetection.h"
66 #include "apriltags2_ros/AprilTagDetectionArray.h"
67 #include "apriltag.h"
68 
69 namespace apriltags2_ros
70 {
71 
72 template<typename T>
74  const std::string& param_name, const T & default_val)
75 {
76  T param_val;
77  pnh.param<T>(param_name, param_val, default_val);
78  return param_val;
79 }
80 
81 // Stores the properties of a tag member of a bundle
83 {
84  int id; // Payload ID
85  double size; // [m] Side length
86  cv::Matx44d T_oi; // Rigid transform from tag i frame to bundle origin frame
87 };
88 
90 {
91  public:
94  std::string &frame_name) :
95  id_(id),
96  size_(size),
97  frame_name_(frame_name) {}
98 
99  double size() { return size_; }
100  int id() { return id_; }
101  std::string& frame_name() { return frame_name_; }
102 
103  private:
104  // Tag description
105  int id_;
106  double size_;
107  std::string frame_name_;
108 };
109 
111 {
112  public:
113  std::map<int, int > id2idx_; // (id2idx_[<tag ID>]=<index in tags_>) mapping
114 
115  TagBundleDescription(std::string name) :
116  name_(name) {}
117 
118  void addMemberTag(int id, double size, cv::Matx44d T_oi) {
119  TagBundleMember member;
120  member.id = id;
121  member.size = size;
122  member.T_oi = T_oi;
123  tags_.push_back(member);
124  id2idx_[id] = tags_.size()-1;
125  }
126 
127  std::string name () const { return name_; }
128  // Get IDs of bundle member tags
129  std::vector<int> bundleIds () {
130  std::vector<int> ids;
131  for (unsigned int i = 0; i < tags_.size(); i++) {
132  ids.push_back(tags_[i].id);
133  }
134  return ids;
135  }
136  // Get sizes of bundle member tags
137  std::vector<double> bundleSizes () {
138  std::vector<double> sizes;
139  for (unsigned int i = 0; i < tags_.size(); i++) {
140  sizes.push_back(tags_[i].size);
141  }
142  return sizes;
143  }
144  int memberID (int tagID) { return tags_[id2idx_[tagID]].id; }
145  double memberSize (int tagID) { return tags_[id2idx_[tagID]].size; }
146  cv::Matx44d memberT_oi (int tagID) { return tags_[id2idx_[tagID]].T_oi; }
147 
148  private:
149  // Bundle description
150  std::string name_;
151  std::vector<TagBundleMember > tags_;
152 };
153 
155 {
156  private:
157  // Detections sorting
158  static int idComparison(const void* first, const void* second);
159 
160  // Remove detections of tags with the same ID
161  void removeDuplicates();
162 
163  // AprilTags 2 code's attributes
164  std::string family_;
165  int border_;
166  int threads_;
167  double decimate_;
168  double blur_;
172  int debug_;
173 
174  // AprilTags 2 objects
178 
179  // Other members
180  std::map<int, StandaloneTagDescription> standalone_tag_descriptions_;
181  std::vector<TagBundleDescription > tag_bundle_descriptions_;
186  std::string camera_tf_frame_;
187 
188  public:
189 
191  ~TagDetector();
192 
193  // Store standalone and bundle tag descriptions
194  std::map<int, StandaloneTagDescription> parseStandaloneTags(
195  XmlRpc::XmlRpcValue& standalone_tag_descriptions);
196  std::vector<TagBundleDescription > parseTagBundles(
197  XmlRpc::XmlRpcValue& tag_bundles);
198  double xmlRpcGetDouble(
199  XmlRpc::XmlRpcValue& xmlValue, std::string field) const;
200  double xmlRpcGetDoubleWithDefault(
201  XmlRpc::XmlRpcValue& xmlValue, std::string field,
202  double defaultValue) const;
203 
204  bool findStandaloneTagDescription(
205  int id, StandaloneTagDescription*& descriptionContainer,
206  bool printWarning = true);
207 
208  geometry_msgs::PoseWithCovarianceStamped makeTagPose(
209  const Eigen::Matrix4d& transform,
210  const Eigen::Quaternion<double> rot_quaternion,
211  const std_msgs::Header& header);
212 
213  // Detect tags in an image
214  AprilTagDetectionArray detectTags(
215  const cv_bridge::CvImagePtr& image,
216  const sensor_msgs::CameraInfoConstPtr& camera_info);
217 
218  // Get the pose of the tag in the camera frame
219  // Returns homogeneous transformation matrix [R,t;[0 0 0 1]] which
220  // takes a point expressed in the tag frame to the same point
221  // expressed in the camera frame. As usual, R is the (passive)
222  // rotation from the tag frame to the camera frame and t is the
223  // vector from the camera frame origin to the tag frame origin,
224  // expressed in the camera frame.
225  Eigen::Matrix4d getRelativeTransform(
226  std::vector<cv::Point3d > objectPoints,
227  std::vector<cv::Point2d > imagePoints,
228  double fx, double fy, double cx, double cy) const;
229 
230  void addImagePoints(apriltag_detection_t *detection,
231  std::vector<cv::Point2d >& imagePoints) const;
232  void addObjectPoints(double s, cv::Matx44d T_oi,
233  std::vector<cv::Point3d >& objectPoints) const;
234 
235  // Draw the detected tags' outlines and payload values on the image
236  void drawDetections(cv_bridge::CvImagePtr image);
237 };
238 
239 } // namespace apriltags2_ros
240 
241 #endif // APRILTAGS2_ROS_COMMON_FUNCTIONS_H
std::map< int, StandaloneTagDescription > standalone_tag_descriptions_
std::vector< TagBundleMember > tags_
void addMemberTag(int id, double size, cv::Matx44d T_oi)
StandaloneTagDescription(int id, double size, std::string &frame_name)
std::vector< TagBundleDescription > tag_bundle_descriptions_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
T getAprilTagOption(ros::NodeHandle &pnh, const std::string &param_name, const T &default_val)
apriltag_detector_t * td_
tf::TransformBroadcaster tf_pub_


apriltags2_ros
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:45