aruco_images_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <ros/ros.h>
5 #include <sensor_msgs/CameraInfo.h>
6 #include <opencv2/highgui/highgui.hpp>
7 #include <cv_bridge/cv_bridge.h>
8 
9 #include <fiducial_msgs/FiducialArray.h>
10 #include <fiducial_msgs/FiducialTransformArray.h>
11 
12 class ArucoImagesTest : public ::testing::Test {
13 protected:
14  virtual void SetUp() {
16  image_pub = it->advertise("camera/image", 1);
17 
18  CameraInfoPub = nh.advertise<sensor_msgs::CameraInfo>("camera_info", 5);
19 
20  c_info.height = 960;
21  c_info.width = 1280;
22  c_info.distortion_model = "plumb_bob";
23 
24  double data_D[] = {0.1349735087283542, -0.2335869827451621, 0.0006697030315075139, 0.004846737465872353, 0.0};
25  c_info.D = std::vector<double> (data_D, data_D + sizeof(data_D)/ sizeof(double));
26 
27  c_info.K = {1006.126285753055, 0.0, 655.8639244150409, 0.0, 1004.015433012594, 490.6140221242933, 0.0, 0.0, 1.0};
28  c_info.R = {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
29  c_info.P = {1021.54345703125, 0.0, 661.9091982335958, 0.0, 0.0, 1025.251953125, 490.6380671707448, 0.0, 0.0, 0.0, 1.0, 0.0};
30 
31  ros::NodeHandle nh_priv("~");
32  nh_priv.getParam("image_directory", image_directory);
33 
34  vertices_sub = nh.subscribe("/fiducial_vertices", 1, &ArucoImagesTest::vertices_callback, this);
35  got_vertices = false;
36 
37  transforms_sub = nh.subscribe("/fiducial_transforms", 1, &ArucoImagesTest::transforms_callback, this);
38  got_transforms = false;
39  }
40 
41  virtual void TearDown() { delete it;}
42 
43  void publish_image(std::string file) {
44  cv::Mat image = cv::imread(image_directory+file, CV_LOAD_IMAGE_COLOR);
45  cv::waitKey(30);
46  sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
47  image_pub.publish(msg);
48  c_info.header.stamp = ros::Time::now();
50  }
51 
52  void vertices_callback(const fiducial_msgs::FiducialArray f) {
53  got_vertices = true;
54  fiducials = f;
55  }
56 
57  void transforms_callback(const fiducial_msgs::FiducialTransformArray f) {
58  got_transforms = true;
59  fiducial_tfs = f;
60  }
61 
63 
64  // Set up Publishing of static images
67 
68  sensor_msgs::CameraInfo c_info;
70 
71  std::string image_directory;
72 
73  // Set up subscribing
75  fiducial_msgs::FiducialArray fiducials;
77 
79  fiducial_msgs::FiducialTransformArray fiducial_tfs;
81 };
82 
83 TEST_F(ArucoImagesTest, tag_01_d7_14cm) {
84  int loop_count = 0;
85  ros::Rate loop_rate(5);
86  while (nh.ok() && (!got_vertices || !got_transforms)) {
87  publish_image("tag_01_d7_14cm.png");
88  ros::spinOnce();
89  loop_rate.sleep();
90  loop_count++;
91  if (loop_count > 10) {
92  FAIL() << "Did not receive estimate within 10 frames";
93  }
94  }
95 
96  ASSERT_EQ(1, fiducials.fiducials.size());
97  ASSERT_EQ(1, fiducial_tfs.transforms.size());
98 
99  const fiducial_msgs::Fiducial& vertices = fiducials.fiducials[0];
100  ASSERT_EQ(1, vertices.fiducial_id);
101 
102  ASSERT_FLOAT_EQ(569.89917, vertices.x0);
103  ASSERT_FLOAT_EQ(201.55890, vertices.y0);
104  ASSERT_FLOAT_EQ(777.42560, vertices.x1);
105  ASSERT_FLOAT_EQ(206.85025, vertices.y1);
106  ASSERT_FLOAT_EQ(767.95856, vertices.x2);
107  ASSERT_FLOAT_EQ(415.37830, vertices.y2);
108  ASSERT_FLOAT_EQ(565.75311, vertices.x3);
109  ASSERT_FLOAT_EQ(409.24496, vertices.y3);
110 }
111 
112 TEST_F(ArucoImagesTest, tag_245_246_d7_14cm) {
113  int loop_count = 0;
114  ros::Rate loop_rate(5);
115  while (nh.ok() && (!got_vertices || !got_transforms)) {
116  publish_image("tag_245-246_d7_14cm.png");
117  ros::spinOnce();
118  loop_rate.sleep();
119  loop_count++;
120  if (loop_count > 10) {
121  FAIL() << "Did not receive estimate within 10 frames";
122  }
123  }
124 
125  ASSERT_EQ(2, fiducials.fiducials.size());
126  ASSERT_EQ(2, fiducial_tfs.transforms.size());
127 
128  for (auto& vertices: fiducials.fiducials) {
129  if (vertices.fiducial_id == 245) {
130  ASSERT_FLOAT_EQ(307.68246, vertices.x0);
131  ASSERT_FLOAT_EQ(157.38346, vertices.y0);
132  ASSERT_FLOAT_EQ(545.10131, vertices.x1);
133  ASSERT_FLOAT_EQ(167.04420, vertices.y1);
134  ASSERT_FLOAT_EQ(540.11614, vertices.x2);
135  ASSERT_FLOAT_EQ(403.27578, vertices.y2);
136  ASSERT_FLOAT_EQ(305.64746, vertices.x3);
137  ASSERT_FLOAT_EQ(395.01422, vertices.y3);
138  }
139  else if (vertices.fiducial_id == 246) {
140  ASSERT_FLOAT_EQ(671.51892, vertices.x0);
141  ASSERT_FLOAT_EQ(173.46070, vertices.y0);
142  ASSERT_FLOAT_EQ(900.29650, vertices.x1);
143  ASSERT_FLOAT_EQ(178.44973, vertices.y1);
144  ASSERT_FLOAT_EQ(895.06933, vertices.x2);
145  ASSERT_FLOAT_EQ(407.39855, vertices.y2);
146  ASSERT_FLOAT_EQ(666.39910, vertices.x3);
147  ASSERT_FLOAT_EQ(403.12911, vertices.y3);
148  }
149  else {
150  FAIL();
151  }
152  }
153 }
154 
155 int main(int argc, char** argv)
156 {
157 
158  testing::InitGoogleTest(&argc, argv);
159  ros::init(argc, argv, "ArucoImagesTest");
160  return RUN_ALL_TESTS();
161 }
ros::Publisher CameraInfoPub
image_transport::Publisher image_pub
ros::NodeHandle nh
fiducial_msgs::FiducialArray fiducials
void transforms_callback(const fiducial_msgs::FiducialTransformArray f)
void publish(const boost::shared_ptr< M > &message) const
sensor_msgs::CameraInfo c_info
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::string image_directory
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Publisher advertise(const std::string &base_topic, uint32_t queue_size, bool latch=false)
fiducial_msgs::FiducialTransformArray fiducial_tfs
TEST_F(ArucoImagesTest, tag_01_d7_14cm)
ros::Subscriber vertices_sub
void vertices_callback(const fiducial_msgs::FiducialArray f)
int main(int argc, char **argv)
void publish_image(std::string file)
void publish(const sensor_msgs::Image &message) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
virtual void TearDown()
bool sleep()
virtual void SetUp()
bool getParam(const std::string &key, std::string &s) const
static Time now()
image_transport::ImageTransport * it
bool ok() const
ROSCPP_DECL void spinOnce()
ros::Subscriber transforms_sub
sensor_msgs::ImagePtr toImageMsg() const


aruco_detect
Author(s): Jim Vaughan
autogenerated on Tue Jun 1 2021 03:03:28