laser_cb_detector_unittest.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #include <gtest/gtest.h>
36 
37 
38 #include <laser_cb_detector/ConfigGoal.h>
40 #include <opencv2/opencv.hpp>
41 #include <opencv2/highgui/highgui.hpp>
42 #include <opencv2/core/types_c.h>
43 
44 #include <ros/console.h>
45 
46 using namespace laser_cb_detector;
47 using namespace std;
48 
49 static const double pix_eps_ = .6 ;
50 static const double eps = 1e-6;
51 
52 static const bool DEBUG=false;
53 
54 // The path to our test data comes in from CMake as TEST_PATH;
55 // stringify it and stuff it into a variable for later use.
56 #define xstr(s) str(s)
57 #define str(s) #s
58 static const string test_path = xstr(TEST_PATH);
59 
60 calibration_msgs::DenseLaserSnapshot getSnapshot(const string& filename)
61 {
62  //IplImage* image;
63  cv::Mat image;
64  image = cv::imread(filename.c_str(), 0); // 0 -> Force image to grayscale
65  EXPECT_TRUE(!image.empty()) << "could not open image file [" << filename << "]" << endl;
66 
67 
68  cv::Mat float_image;
69  //float_image = cv::createImage( cvSize(image->width, image->height), IPL_DEPTH_32F, 1);
70  //cv::convert(image, float_image);
71  image.convertTo(float_image, CV_32FC1, 1/255.0);
72 
73 
74  calibration_msgs::DenseLaserSnapshot snapshot;
75  snapshot.readings_per_scan = image.cols;
76  snapshot.num_scans = image.rows;
77  snapshot.intensities.resize(0);
78  //snapshot.intensities.resize(image.rows * image.cols);
79 
80  //snapshot.intensities.assign(float_image.data, float_image.data + float_image.total());
81  for (int i=0; i<float_image.rows; i++)
82  {
83  snapshot.intensities.insert(snapshot.intensities.end(), float_image.ptr<float>(i), float_image.ptr<float>(i)+float_image.cols);
84  //snapshot.intensities.assign(float_image.data, float_image.data + float_image.total());
85  //memcpy(&snapshot.intensities[i*snapshot.readings_per_scan],
86  // (float_image.imageData + i*float_image.widthStep),
87  // sizeof(float)*snapshot.readings_per_scan);
88  }
89 
90  if (DEBUG)
91  {
92  for (unsigned int i=0; i<snapshot.num_scans; i++)
93  {
94  for (unsigned int j=0; j<snapshot.readings_per_scan; j++)
95  {
96  if (snapshot.intensities[i*snapshot.readings_per_scan + j] < 100.0)
97  printf(" ");
98  else
99  printf("X");
100  }
101  printf("\n");
102  }
103  }
104 
105  // since statically declared, does not need release, but this could cause a memory leak
106  //cv::releaseImage(&float_image);
107  //cv::releaseImage(&image);
108 
109  return snapshot;
110 }
111 
112 ConfigGoal config3x4()
113 {
114  ConfigGoal config;
115 
116  config.num_x = 3;
117  config.num_y = 4;
118  config.spacing_x = 1.0;
119  config.spacing_y = 1.0;
120 
121  config.width_scaling = 1.0;
122  config.height_scaling = 1.0;
123 
124  config.min_intensity = 0.0;
125  config.max_intensity = 100.0;
126 
127  config.subpixel_window = 3;
128  config.subpixel_zero_zone = 1;
129 
130  config.flip_horizontal = 0;
131 
132  return config;
133 }
134 
135 
136 void check3x4(const calibration_msgs::CalibrationPattern& result, double pix_eps, bool flipped)
137 {
138  EXPECT_TRUE(result.success);
139 
140 
141  EXPECT_NEAR(result.object_points[0].x, 0.0, eps);
142  EXPECT_NEAR(result.object_points[0].y, 0.0, eps);
143  EXPECT_NEAR(result.object_points[0].z, 0.0, eps);
144 
145  EXPECT_NEAR(result.object_points[2].x, 2.0, eps);
146  EXPECT_NEAR(result.object_points[2].y, 0.0, eps);
147  EXPECT_NEAR(result.object_points[2].z, 0.0, eps);
148 
149  EXPECT_NEAR(result.object_points[5].x, 2.0, eps);
150  EXPECT_NEAR(result.object_points[5].y, 1.0, eps);
151  EXPECT_NEAR(result.object_points[5].z, 0.0, eps);
152 
153  // Detected corners
154  if (flipped)
155  {
156  EXPECT_NEAR(result.image_points[0].x, 81.5, pix_eps);
157  EXPECT_NEAR(result.image_points[0].y, 72.0, pix_eps);
158 
159  EXPECT_NEAR(result.image_points[1].x, 81.5, pix_eps);
160  EXPECT_NEAR(result.image_points[1].y, 118.5, pix_eps);
161 
162  EXPECT_NEAR(result.image_points[11].x, 223.5, pix_eps);
163  EXPECT_NEAR(result.image_points[11].y, 166.5, pix_eps);
164  }
165  else
166  {
167  EXPECT_NEAR(result.image_points[0].x, 223.0, pix_eps);
168  EXPECT_NEAR(result.image_points[0].y, 72.0, pix_eps);
169 
170  EXPECT_NEAR(result.image_points[1].x, 223.0, pix_eps);
171  EXPECT_NEAR(result.image_points[1].y, 119.0, pix_eps);
172 
173  EXPECT_NEAR(result.image_points[11].x, 81.0, pix_eps);
174  EXPECT_NEAR(result.image_points[11].y, 166.0, pix_eps);
175  }
176 }
177 
178 TEST(LaserCbDetector, easy_cb_3x4)
179 {
180  LaserCbDetector detector;
181  calibration_msgs::DenseLaserSnapshot snapshot;
182 
183  snapshot = getSnapshot( test_path + "/data/cb_3x4.png");
184 
185  ROS_INFO( "test_path: [%s]/[/data/cb_3x4.png]", test_path.c_str());
186 
187  ASSERT_EQ(snapshot.readings_per_scan, (unsigned int) 303);
188  ASSERT_EQ(snapshot.num_scans, (unsigned int) 325);
189 
190  detector.configure(config3x4());
191 
192  bool return_result;
193  calibration_msgs::CalibrationPattern result;
194  return_result = detector.detect(snapshot, result);
195  EXPECT_TRUE(return_result);
196 
197  check3x4(result, pix_eps_, false);
198 }
199 
200 TEST(LaserCbDetector, scaled_cb_3x4)
201 {
202  LaserCbDetector detector;
203  calibration_msgs::DenseLaserSnapshot snapshot;
204 
205  snapshot = getSnapshot( test_path + "/data/cb_3x4.png");
206 
207  ASSERT_EQ(snapshot.readings_per_scan, (unsigned int) 303);
208  ASSERT_EQ(snapshot.num_scans, (unsigned int) 325);
209 
210  ConfigGoal config = config3x4();
211  config.width_scaling = 2.0;
212  config.height_scaling = 4.0;
213  detector.configure(config);
214 
215  bool return_result;
216  calibration_msgs::CalibrationPattern result;
217  return_result = detector.detect(snapshot, result);
218  EXPECT_TRUE(return_result);
219 
221  check3x4(result, 1.0, false);
222 }
223 
224 TEST(LaserCbDetector, reflected_cb_3x4)
225 {
226  LaserCbDetector detector;
227  calibration_msgs::DenseLaserSnapshot snapshot;
228 
229  snapshot = getSnapshot( test_path + "/data/cb_3x4.png");
230 
231  ASSERT_EQ(snapshot.readings_per_scan, (unsigned int) 303);
232  ASSERT_EQ(snapshot.num_scans, (unsigned int) 325);
233 
234  ConfigGoal config = config3x4();
235  config.flip_horizontal = 1;
236  detector.configure(config);
237 
238  bool return_result;
239  calibration_msgs::CalibrationPattern result;
240  return_result = detector.detect(snapshot, result);
241  EXPECT_TRUE(return_result);
242 
244  check3x4(result, 1.0, true);
245 }
246 
247 
248 TEST(LaserCbDetector, malformedTest)
249 {
250  LaserCbDetector detector;
251 
252  calibration_msgs::DenseLaserSnapshot snapshot;
253 
254  detector.configure(config3x4());
255  snapshot = getSnapshot( test_path + "/data/cb_3x4.png");
256  snapshot.num_scans = 10;
257 
258  bool return_result;
259  calibration_msgs::CalibrationPattern result;
260  return_result = detector.detect(snapshot, result);
261 
262  EXPECT_FALSE(return_result);
263 }
264 
265 TEST(LaserCbDetector, noCheckerboardTest)
266 {
267  LaserCbDetector detector;
268 
269  calibration_msgs::DenseLaserSnapshot snapshot;
270 
271  ConfigGoal config = config3x4();
272  config.num_x = 4;
273  detector.configure(config);
274  snapshot = getSnapshot( test_path + "/data/cb_3x4.png");
275 
276  bool return_result;
277  calibration_msgs::CalibrationPattern result;
278  return_result = detector.detect(snapshot, result);
279 
280  EXPECT_TRUE(return_result);
281  EXPECT_FALSE(result.success);
282 }
283 
284 
285 
286 int main(int argc, char **argv){
287  testing::InitGoogleTest(&argc, argv);
288  return RUN_ALL_TESTS();
289 }
xstr
#define xstr(s)
Definition: laser_cb_detector_unittest.cpp:56
laser_cb_detector.h
DEBUG
static const bool DEBUG
Definition: laser_cb_detector_unittest.cpp:52
laser_cb_detector
Definition: cv_laser_bridge.h:43
config3x4
ConfigGoal config3x4()
Definition: laser_cb_detector_unittest.cpp:112
console.h
test_path
static const string test_path
Definition: laser_cb_detector_unittest.cpp:58
eps
static const double eps
Definition: laser_cb_detector_unittest.cpp:50
pix_eps_
static const double pix_eps_
Definition: laser_cb_detector_unittest.cpp:49
laser_cb_detector::LaserCbDetector::configure
bool configure(const ConfigGoal &config)
Definition: laser_cb_detector.cpp:47
laser_cb_detector::LaserCbDetector::detect
bool detect(const calibration_msgs::DenseLaserSnapshot &snapshot, calibration_msgs::CalibrationPattern &result)
Definition: laser_cb_detector.cpp:68
check3x4
void check3x4(const calibration_msgs::CalibrationPattern &result, double pix_eps, bool flipped)
Definition: laser_cb_detector_unittest.cpp:136
TEST
TEST(LaserCbDetector, easy_cb_3x4)
Definition: laser_cb_detector_unittest.cpp:178
main
int main(int argc, char **argv)
Definition: laser_cb_detector_unittest.cpp:286
std
ROS_INFO
#define ROS_INFO(...)
getSnapshot
calibration_msgs::DenseLaserSnapshot getSnapshot(const string &filename)
Definition: laser_cb_detector_unittest.cpp:60
laser_cb_detector::LaserCbDetector
Definition: laser_cb_detector.h:83


laser_cb_detector
Author(s): Vijay Pradeep
autogenerated on Tue Mar 1 2022 23:58:55